Skip to content

Commit 37d3a4a

Browse files
committed
improves baseUrl resolution in typescript monorepos
Typescript configuration can inherit from files above cwd in the filesystem. If a baseUrl was declared in such a file, it would not be picked up by the webpack config. This would force users to use the next-transpile-module and duplicate configuration with unintuitive path relations (see #13197 for a detailed analysis) If baseUrl is resolved it should be used instead of dir as the root include for babel-resolve-loader.
1 parent 8f888c9 commit 37d3a4a

File tree

17 files changed

+180
-1
lines changed

17 files changed

+180
-1
lines changed

packages/next/build/webpack-config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '../lib/constants'
1616
import { fileExists } from '../lib/file-exists'
1717
import { resolveRequest } from '../lib/resolve-request'
18+
import { getTypeScriptConfiguration } from '../lib/typescript/getTypeScriptConfiguration'
1819
import {
1920
CLIENT_STATIC_FILES_RUNTIME_MAIN,
2021
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS,
@@ -261,7 +262,9 @@ export default async function getBaseWebpackConfig(
261262
let jsConfig
262263
// jsconfig is a subset of tsconfig
263264
if (useTypeScript) {
264-
jsConfig = parseJsonFile(tsConfigPath)
265+
const ts = (await import(typeScriptPath)) as typeof import('typescript')
266+
const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath)
267+
jsConfig = { compilerOptions: tsConfig.options }
265268
}
266269

267270
const jsConfigPath = path.join(dir, 'jsconfig.json')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//this line uses typescript specific syntax
2+
//to demonstrate that transpilation occurs
3+
export type API = () => string
4+
const api: API = () => 'Hello from a'
5+
export default api
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { API } from '@lib/api'
2+
3+
const b: API = () => 'Hello from b'
4+
export default b
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { API } from '@lib/api'
2+
3+
const b: API = () => 'Hello from only b'
4+
export default b
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => any
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const Named = () => {
2+
return <>Not aliased to d.ts file</>
3+
}
4+
export default Named
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
export function Hello() {
4+
return <>Hello</>
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
export function World(): JSX.Element {
4+
return <>World</>
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const path = require('path')
2+
module.exports = {
3+
webpack: function (config, { defaultLoaders }) {
4+
const resolvedBaseUrl = path.resolve(config.context, '../../')
5+
config.module.rules = [
6+
...config.module.rules,
7+
{
8+
test: /\.(tsx|ts|js|mjs|jsx)$/,
9+
include: [resolvedBaseUrl],
10+
use: defaultLoaders.babel,
11+
exclude: (excludePath) => {
12+
return /node_modules/.test(excludePath)
13+
},
14+
},
15+
]
16+
return config
17+
},
18+
19+
onDemandEntries: {
20+
// Make sure entries are not getting disposed.
21+
maxInactiveAge: 1000 * 60 * 60,
22+
},
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import NotAliasedToDTS from 'd-ts-alias'
3+
4+
export default function HelloPage(): JSX.Element {
5+
return (
6+
<div>
7+
<NotAliasedToDTS />
8+
</div>
9+
)
10+
}

0 commit comments

Comments
 (0)