Skip to content

Commit 7d6713d

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 bad3761 commit 7d6713d

File tree

17 files changed

+158
-2
lines changed

17 files changed

+158
-2
lines changed

packages/next/build/webpack-config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import WebpackConformancePlugin, {
5252
} from './webpack/plugins/webpack-conformance-plugin'
5353
import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin'
5454
import { codeFrameColumns } from '@babel/code-frame'
55+
import { getTypeScriptConfiguration } from '../lib/typescript/getTypeScriptConfiguration'
5556

5657
type ExcludesFalse = <T>(x: T | false) => x is T
5758

@@ -250,7 +251,9 @@ export default async function getBaseWebpackConfig(
250251
let jsConfig
251252
// jsconfig is a subset of tsconfig
252253
if (useTypeScript) {
253-
jsConfig = parseJsonFile(tsConfigPath)
254+
const ts = (await import(typeScriptPath)) as typeof import('typescript')
255+
const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath)
256+
jsConfig = { compilerOptions: tsConfig.options }
254257
}
255258

256259
const jsConfigPath = path.join(dir, 'jsconfig.json')
@@ -734,7 +737,9 @@ export default async function getBaseWebpackConfig(
734737
rules: [
735738
{
736739
test: /\.(tsx|ts|js|mjs|jsx)$/,
737-
include: [dir, ...babelIncludeRegexes],
740+
include: resolvedBaseUrl
741+
? [resolvedBaseUrl, dir, ...babelIncludeRegexes]
742+
: [dir, ...babelIncludeRegexes],
738743
exclude: (excludePath: string) => {
739744
if (babelIncludeRegexes.some((r) => r.test(excludePath))) {
740745
return false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => 'Hello from a'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => 'Hello from b'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => 'Hello from only b'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => any
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default () => {
2+
return <>Not aliased to d.ts file</>
3+
}
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
webpack: function (config) {
3+
return config
4+
},
5+
onDemandEntries: {
6+
// Make sure entries are not getting disposed.
7+
maxInactiveAge: 1000 * 60 * 60,
8+
},
9+
}
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)