Skip to content

Commit

Permalink
improves baseUrl resolution in typescript monorepos
Browse files Browse the repository at this point in the history
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 vercel#13197 for a detailed analysis)

If baseUrl is resolved it should be used instead of dir as the root
include for babel-resolve-loader.

An even nicer DX would auto detect the presence of a `paths` section in
the typescript config and automatically include
`tsconfig-paths-webpack-plugin`
  • Loading branch information
jeantil committed May 29, 2020
1 parent 619493a commit 75f326f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ import WebpackConformancePlugin, {
} from './webpack/plugins/webpack-conformance-plugin'
import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin'
import { codeFrameColumns } from '@babel/code-frame'
import { getTypeScriptConfiguration } from '../lib/typescript/getTypeScriptConfiguration'
import {
NecessaryDependencies,
hasNecessaryDependencies,
} from '../lib/typescript/hasNecessaryDependencies'

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

Expand Down Expand Up @@ -250,7 +255,18 @@ export default async function getBaseWebpackConfig(
let jsConfig
// jsconfig is a subset of tsconfig
if (useTypeScript) {
jsConfig = parseJsonFile(tsConfigPath)
// Ensure TypeScript and necessary `@types/*` are installed:
const deps: NecessaryDependencies = await hasNecessaryDependencies(dir)

// Load TypeScript after we're sure it exists:
const ts = (await import(
deps.resolvedTypeScript
)) as typeof import('typescript')
const tsConfig: import('typescript').ParsedCommandLine = await getTypeScriptConfiguration(
ts,
tsConfigPath
)
jsConfig = { compilerOptions: tsConfig.options }
}

const jsConfigPath = path.join(dir, 'jsconfig.json')
Expand Down Expand Up @@ -760,7 +776,9 @@ export default async function getBaseWebpackConfig(
rules: [
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
include: [dir, ...babelIncludeRegexes],
include: resolvedBaseUrl
? [resolvedBaseUrl, ...babelIncludeRegexes]
: [dir, ...babelIncludeRegexes],
exclude: (path: string) => {
if (babelIncludeRegexes.some((r) => r.test(path))) {
return false
Expand Down

0 comments on commit 75f326f

Please sign in to comment.