Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for baseUrl option in tsconfig and jsconfig #11203

Merged
merged 3 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ export default async function getBaseWebpackConfig(
? config.typescript?.ignoreDevErrors
: config.typescript?.ignoreBuildErrors

let jsConfig
// jsconfig is a subset of tsconfig
if (useTypeScript) {
jsConfig = require(tsConfigPath)
}

const jsConfigPath = path.join(dir, 'jsconfig.json')
if (!useTypeScript && (await fileExists(jsConfigPath))) {
jsConfig = require(jsConfigPath)
}

let resolvedBaseUrl
if (jsConfig?.compilerOptions?.baseUrl) {
resolvedBaseUrl = path.resolve(dir, jsConfig.compilerOptions.baseUrl)
}

const resolveConfig = {
// Disable .mjs for node_modules bundling
extensions: isServer
Expand Down Expand Up @@ -889,6 +905,11 @@ export default async function getBaseWebpackConfig(
].filter((Boolean as any) as ExcludesFalse),
}

// Support tsconfig and jsconfig baseUrl
if (resolvedBaseUrl) {
webpackConfig.resolve?.modules?.push(resolvedBaseUrl)
}

webpackConfig = await buildConfiguration(webpackConfig, {
rootDirectory: dir,
customAppFile,
Expand Down
5 changes: 5 additions & 0 deletions test/integration/jsconfig-baseurl/components/world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export function World() {
return <>World</>
}
5 changes: 5 additions & 0 deletions test/integration/jsconfig-baseurl/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "."
}
}
6 changes: 6 additions & 0 deletions test/integration/jsconfig-baseurl/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
}
9 changes: 9 additions & 0 deletions test/integration/jsconfig-baseurl/pages/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import { World } from 'components/world'
export default function HelloPage() {
return (
<div>
<World />
</div>
)
}
31 changes: 31 additions & 0 deletions test/integration/jsconfig-baseurl/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import cheerio from 'cheerio'
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2

const appDir = join(__dirname, '..')
let appPort
let app

async function get$(path, query) {
const html = await renderViaHTTP(appPort, path, query)
return cheerio.load(html)
}

describe('TypeScript Features', () => {
describe('default behavior', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {})
})
afterAll(() => killApp(app))

it('should render the page', async () => {
const $ = await get$('/hello')
expect($('body').text()).toMatch(/World/)
})
})
})
5 changes: 5 additions & 0 deletions test/integration/typescript-baseurl/components/world.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export function World(): JSX.Element {
return <>World</>
}
6 changes: 6 additions & 0 deletions test/integration/typescript-baseurl/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
}
9 changes: 9 additions & 0 deletions test/integration/typescript-baseurl/pages/hello.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import { World } from 'components/world'
export default function HelloPage(): JSX.Element {
return (
<div>
<World />
</div>
)
}
31 changes: 31 additions & 0 deletions test/integration/typescript-baseurl/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import cheerio from 'cheerio'
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2

const appDir = join(__dirname, '..')
let appPort
let app

async function get$(path, query) {
const html = await renderViaHTTP(appPort, path, query)
return cheerio.load(html)
}

describe('TypeScript Features', () => {
describe('default behavior', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {})
})
afterAll(() => killApp(app))

it('should render the page', async () => {
const $ = await get$('/hello')
expect($('body').text()).toMatch(/World/)
})
})
})
20 changes: 20 additions & 0 deletions test/integration/typescript-baseurl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"module": "esnext",
"jsx": "preserve",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "components", "pages"]
}