From b44445854eb066ffd0e8fe0b2c77cfbdc09e6147 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Tue, 1 Mar 2022 17:28:41 +0100 Subject: [PATCH 1/4] Implement tsconfig paths without baseurl --- packages/next/build/load-jsconfig.ts | 12 +- .../jsconfig-paths-without-baseurl/.gitignore | 1 + .../components/hello.js | 5 + .../components/world.js | 5 + .../jsconfig.json | 10 ++ .../lib/a/api.js | 5 + .../lib/b/api.js | 1 + .../lib/b/b-only.js | 1 + .../next.config.js | 6 + .../node_modules/mypackage/data.js | 3 + .../node_modules/mypackage/myfile.js | 3 + .../pages/basic-alias.js | 9 ++ .../pages/resolve-fallback.js | 5 + .../pages/resolve-order.js | 5 + .../pages/single-alias.js | 9 ++ .../pages/wildcard-alias.js | 5 + .../test/index.test.js | 144 ++++++++++++++++++ 17 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 test/integration/jsconfig-paths-without-baseurl/.gitignore create mode 100644 test/integration/jsconfig-paths-without-baseurl/components/hello.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/components/world.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/jsconfig.json create mode 100644 test/integration/jsconfig-paths-without-baseurl/lib/a/api.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/lib/b/api.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/next.config.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js create mode 100644 test/integration/jsconfig-paths-without-baseurl/test/index.test.js diff --git a/packages/next/build/load-jsconfig.ts b/packages/next/build/load-jsconfig.ts index 448c008f987696e..018827470ee71d9 100644 --- a/packages/next/build/load-jsconfig.ts +++ b/packages/next/build/load-jsconfig.ts @@ -49,6 +49,7 @@ export default async function loadJsConfig( typeScriptPath && (await fileExists(tsConfigPath)) ) + let implicitBaseurl let jsConfig // jsconfig is a subset of tsconfig if (useTypeScript) { @@ -65,17 +66,24 @@ export default async function loadJsConfig( )) as typeof import('typescript') const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath, true) jsConfig = { compilerOptions: tsConfig.options } + implicitBaseurl = path.dirname(tsConfigPath) } const jsConfigPath = path.join(dir, 'jsconfig.json') if (!useTypeScript && (await fileExists(jsConfigPath))) { jsConfig = parseJsonFile(jsConfigPath) + implicitBaseurl = path.dirname(jsConfigPath) } let resolvedBaseUrl - if (jsConfig?.compilerOptions?.baseUrl) { - resolvedBaseUrl = path.resolve(dir, jsConfig.compilerOptions.baseUrl) + if (jsConfig) { + if (jsConfig.compilerOptions?.baseUrl) { + resolvedBaseUrl = path.resolve(dir, jsConfig.compilerOptions.baseUrl) + } else { + resolvedBaseUrl = implicitBaseurl + } } + return { useTypeScript, jsConfig, diff --git a/test/integration/jsconfig-paths-without-baseurl/.gitignore b/test/integration/jsconfig-paths-without-baseurl/.gitignore new file mode 100644 index 000000000000000..736e8ae58ad87f0 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/.gitignore @@ -0,0 +1 @@ +!node_modules \ No newline at end of file diff --git a/test/integration/jsconfig-paths-without-baseurl/components/hello.js b/test/integration/jsconfig-paths-without-baseurl/components/hello.js new file mode 100644 index 000000000000000..49c8de26f6c5045 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/components/hello.js @@ -0,0 +1,5 @@ +import React from 'react' + +export function Hello() { + return <>Hello +} diff --git a/test/integration/jsconfig-paths-without-baseurl/components/world.js b/test/integration/jsconfig-paths-without-baseurl/components/world.js new file mode 100644 index 000000000000000..5a93b9838685c12 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/components/world.js @@ -0,0 +1,5 @@ +import React from 'react' + +export function World() { + return <>World +} diff --git a/test/integration/jsconfig-paths-without-baseurl/jsconfig.json b/test/integration/jsconfig-paths-without-baseurl/jsconfig.json new file mode 100644 index 000000000000000..397117fed3d56f5 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/jsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "paths": { + "@c/*": ["./components/*"], + "@lib/*": ["./lib/a/*", "./lib/b/*"], + "@mycomponent": ["./components/hello.js"], + "*": ["./node_modules/*"] + } + } +} diff --git a/test/integration/jsconfig-paths-without-baseurl/lib/a/api.js b/test/integration/jsconfig-paths-without-baseurl/lib/a/api.js new file mode 100644 index 000000000000000..1a2637b0c956d46 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/lib/a/api.js @@ -0,0 +1,5 @@ +import data from 'mypackage/data' + +console.log(data) + +export default () => 'Hello from a' diff --git a/test/integration/jsconfig-paths-without-baseurl/lib/b/api.js b/test/integration/jsconfig-paths-without-baseurl/lib/b/api.js new file mode 100644 index 000000000000000..24ee786bd1a95b2 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/lib/b/api.js @@ -0,0 +1 @@ +export default () => 'Hello from b' diff --git a/test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js b/test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js new file mode 100644 index 000000000000000..0f1c1ce795f53df --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js @@ -0,0 +1 @@ +export default () => 'Hello from only b' diff --git a/test/integration/jsconfig-paths-without-baseurl/next.config.js b/test/integration/jsconfig-paths-without-baseurl/next.config.js new file mode 100644 index 000000000000000..cc17cf48c578fd5 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/next.config.js @@ -0,0 +1,6 @@ +module.exports = { + onDemandEntries: { + // Make sure entries are not getting disposed. + maxInactiveAge: 1000 * 60 * 60, + }, +} diff --git a/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js b/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js new file mode 100644 index 000000000000000..f6ba735c283d676 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js @@ -0,0 +1,3 @@ +module.exports = { + hello: 'world', +} diff --git a/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js b/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js new file mode 100644 index 000000000000000..f6ba735c283d676 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js @@ -0,0 +1,3 @@ +module.exports = { + hello: 'world', +} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js b/test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js new file mode 100644 index 000000000000000..d2a31a937ae1142 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js @@ -0,0 +1,9 @@ +import React from 'react' +import { World } from '@c/world' +export default function HelloPage() { + return ( +
+ +
+ ) +} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js b/test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js new file mode 100644 index 000000000000000..148b4404d1fb592 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js @@ -0,0 +1,5 @@ +import React from 'react' +import api from '@lib/b-only' +export default function ResolveOrder() { + return
{api()}
+} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js b/test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js new file mode 100644 index 000000000000000..f7fbe6cee827882 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js @@ -0,0 +1,5 @@ +import React from 'react' +import api from '@lib/api' +export default function ResolveOrder() { + return
{api()}
+} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js b/test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js new file mode 100644 index 000000000000000..df2031b90ae62a9 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js @@ -0,0 +1,9 @@ +import { Hello } from '@mycomponent' + +export default function SingleAlias() { + return ( +
+ +
+ ) +} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js b/test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js new file mode 100644 index 000000000000000..4b7e0c2b53f43c1 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js @@ -0,0 +1,5 @@ +import myfile from 'mypackage/myfile' + +export default function WildcardAlias() { + return
{myfile.hello}
+} diff --git a/test/integration/jsconfig-paths-without-baseurl/test/index.test.js b/test/integration/jsconfig-paths-without-baseurl/test/index.test.js new file mode 100644 index 000000000000000..7037101d88735a7 --- /dev/null +++ b/test/integration/jsconfig-paths-without-baseurl/test/index.test.js @@ -0,0 +1,144 @@ +/* eslint-env jest */ + +import fs from 'fs-extra' +import { join } from 'path' +import cheerio from 'cheerio' +import { + renderViaHTTP, + findPort, + launchApp, + nextBuild, + killApp, + check, +} from 'next-test-utils' + +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', () => { + let output = '' + + beforeAll(async () => { + appPort = await findPort() + app = await launchApp(appDir, appPort, { + onStderr(msg) { + output += msg || '' + }, + onStdout(msg) { + output += msg || '' + }, + }) + }) + afterAll(() => killApp(app)) + + it('should alias components', async () => { + const $ = await get$('/basic-alias') + expect($('body').text()).toMatch(/World/) + }) + + it('should resolve the first item in the array first', async () => { + const $ = await get$('/resolve-order') + expect($('body').text()).toMatch(/Hello from a/) + }) + + it('should resolve the second item as fallback', async () => { + const $ = await get$('/resolve-fallback') + expect($('body').text()).toMatch(/Hello from only b/) + }) + + it('should resolve a single matching alias', async () => { + const $ = await get$('/single-alias') + expect($('body').text()).toMatch(/Hello/) + }) + + it('should resolve a wildcard alias', async () => { + const $ = await get$('/wildcard-alias') + expect($('body').text()).toMatch(/world/) + }) + + it('should have correct module not found error', async () => { + const basicPage = join(appDir, 'pages/basic-alias.js') + const contents = await fs.readFile(basicPage, 'utf8') + + await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd')) + await renderViaHTTP(appPort, '/basic-alias') + + const found = await check( + () => output, + /Module not found: Can't resolve '@c\/worldd'/, + false + ) + await fs.writeFile(basicPage, contents) + expect(found).toBe(true) + }) + }) + + describe('should build', () => { + beforeAll(async () => { + await nextBuild(appDir) + }) + it('should trace correctly', async () => { + const appTrace = await fs.readJSON( + join(appDir, '.next/server/pages/_app.js.nft.json') + ) + const singleAliasTrace = await fs.readJSON( + join(appDir, '.next/server/pages/single-alias.js.nft.json') + ) + const wildcardAliasTrace = await fs.readJSON( + join(appDir, '.next/server/pages/wildcard-alias.js.nft.json') + ) + const resolveOrderTrace = await fs.readJSON( + join(appDir, '.next/server/pages/resolve-order.js.nft.json') + ) + const resolveFallbackTrace = await fs.readJSON( + join(appDir, '.next/server/pages/resolve-fallback.js.nft.json') + ) + const basicAliasTrace = await fs.readJSON( + join(appDir, '.next/server/pages/basic-alias.js.nft.json') + ) + expect( + appTrace.files.some((file) => file.includes('node_modules/next')) + ).toBe(true) + expect( + singleAliasTrace.files.some((file) => + file.includes('components/hello.js') + ) + ).toBe(false) + expect( + wildcardAliasTrace.files.some((file) => + file.includes('mypackage/myfile.js') + ) + ).toBe(true) + expect( + wildcardAliasTrace.files.some((file) => + file.includes('mypackage/data.js') + ) + ).toBe(false) + expect( + resolveOrderTrace.files.some((file) => file.includes('lib/a/api.js')) + ).toBe(false) + expect( + resolveOrderTrace.files.some((file) => + file.includes('mypackage/data.js') + ) + ).toBe(true) + expect( + resolveFallbackTrace.files.some((file) => + file.includes('lib/b/b-only.js') + ) + ).toBe(false) + expect( + basicAliasTrace.files.some((file) => + file.includes('components/world.js') + ) + ).toBe(false) + }) + }) +}) From 7bf9edf7c2b408e0ff4da8bae7ad94d858001170 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Tue, 1 Mar 2022 18:51:37 +0100 Subject: [PATCH 2/4] tsconfig tests --- .../typescript-paths/test/index.test.js | 82 ++++++++++++++----- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/test/integration/typescript-paths/test/index.test.js b/test/integration/typescript-paths/test/index.test.js index f7100467a146201..89ffa04fcf319d8 100644 --- a/test/integration/typescript-paths/test/index.test.js +++ b/test/integration/typescript-paths/test/index.test.js @@ -2,7 +2,15 @@ import { join } from 'path' import cheerio from 'cheerio' -import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils' +import * as path from 'path' +import { + renderViaHTTP, + findPort, + launchApp, + killApp, + File, +} from 'next-test-utils' +import * as JSON5 from 'json5' const appDir = join(__dirname, '..') let appPort @@ -13,6 +21,35 @@ async function get$(path, query) { return cheerio.load(html) } +const tsconfig = new File(path.resolve(__dirname, '../tsconfig.json')) + +function runTests() { + it('should alias components', async () => { + const $ = await get$('/basic-alias') + expect($('body').text()).toMatch(/World/) + }) + + it('should resolve the first item in the array first', async () => { + const $ = await get$('/resolve-order') + expect($('body').text()).toMatch(/Hello from a/) + }) + + it('should resolve the second item in as a fallback', async () => { + const $ = await get$('/resolve-fallback') + expect($('body').text()).toMatch(/Hello from only b/) + }) + + it('should resolve a single matching alias', async () => { + const $ = await get$('/single-alias') + expect($('body').text()).toMatch(/Hello/) + }) + + it('should not resolve to .d.ts files', async () => { + const $ = await get$('/alias-to-d-ts') + expect($('body').text()).toMatch(/Not aliased to d\.ts file/) + }) +} + describe('TypeScript Features', () => { describe('default behavior', () => { beforeAll(async () => { @@ -21,29 +58,32 @@ describe('TypeScript Features', () => { }) afterAll(() => killApp(app)) - it('should alias components', async () => { - const $ = await get$('/basic-alias') - expect($('body').text()).toMatch(/World/) - }) - - it('should resolve the first item in the array first', async () => { - const $ = await get$('/resolve-order') - expect($('body').text()).toMatch(/Hello from a/) - }) + runTests() + }) - it('should resolve the second item in as a fallback', async () => { - const $ = await get$('/resolve-fallback') - expect($('body').text()).toMatch(/Hello from only b/) + describe('without baseurl', () => { + beforeAll(async () => { + const tsconfigContent = JSON5.parse(tsconfig.originalContent) + delete tsconfigContent.compilerOptions.baseUrl + tsconfigContent.compilerOptions.paths = { + 'isomorphic-unfetch': ['./types/unfetch.d.ts'], + '@c/*': ['./components/*'], + '@lib/*': ['./lib/a/*', './lib/b/*'], + '@mycomponent': ['./components/hello.tsx'], + 'd-ts-alias': [ + './components/alias-to-d-ts.d.ts', + './components/alias-to-d-ts.tsx', + ], + } + tsconfig.write(JSON.stringify(tsconfigContent, null, 2)) + appPort = await findPort() + app = await launchApp(appDir, appPort, {}) }) - - it('should resolve a single matching alias', async () => { - const $ = await get$('/single-alias') - expect($('body').text()).toMatch(/Hello/) + afterAll(() => { + tsconfig.restore() + killApp(app) }) - it('should not resolve to .d.ts files', async () => { - const $ = await get$('/alias-to-d-ts') - expect($('body').text()).toMatch(/Not aliased to d\.ts file/) - }) + runTests() }) }) From 121102a46e67d363b0434954179950bc32346130 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Tue, 1 Mar 2022 18:58:42 +0100 Subject: [PATCH 3/4] share some test infra --- .../jsconfig-paths-without-baseurl/.gitignore | 1 - .../components/hello.js | 5 - .../components/world.js | 5 - .../jsconfig.json | 10 -- .../lib/a/api.js | 5 - .../lib/b/api.js | 1 - .../lib/b/b-only.js | 1 - .../next.config.js | 6 - .../node_modules/mypackage/data.js | 3 - .../node_modules/mypackage/myfile.js | 3 - .../pages/basic-alias.js | 9 -- .../pages/resolve-fallback.js | 5 - .../pages/resolve-order.js | 5 - .../pages/single-alias.js | 9 -- .../pages/wildcard-alias.js | 5 - .../test/index.test.js | 144 ------------------ .../jsconfig-paths/test/index.test.js | 31 +++- .../typescript-paths/test/index.test.js | 98 ++++++------ 18 files changed, 78 insertions(+), 268 deletions(-) delete mode 100644 test/integration/jsconfig-paths-without-baseurl/.gitignore delete mode 100644 test/integration/jsconfig-paths-without-baseurl/components/hello.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/components/world.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/jsconfig.json delete mode 100644 test/integration/jsconfig-paths-without-baseurl/lib/a/api.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/lib/b/api.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/next.config.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js delete mode 100644 test/integration/jsconfig-paths-without-baseurl/test/index.test.js diff --git a/test/integration/jsconfig-paths-without-baseurl/.gitignore b/test/integration/jsconfig-paths-without-baseurl/.gitignore deleted file mode 100644 index 736e8ae58ad87f0..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/.gitignore +++ /dev/null @@ -1 +0,0 @@ -!node_modules \ No newline at end of file diff --git a/test/integration/jsconfig-paths-without-baseurl/components/hello.js b/test/integration/jsconfig-paths-without-baseurl/components/hello.js deleted file mode 100644 index 49c8de26f6c5045..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/components/hello.js +++ /dev/null @@ -1,5 +0,0 @@ -import React from 'react' - -export function Hello() { - return <>Hello -} diff --git a/test/integration/jsconfig-paths-without-baseurl/components/world.js b/test/integration/jsconfig-paths-without-baseurl/components/world.js deleted file mode 100644 index 5a93b9838685c12..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/components/world.js +++ /dev/null @@ -1,5 +0,0 @@ -import React from 'react' - -export function World() { - return <>World -} diff --git a/test/integration/jsconfig-paths-without-baseurl/jsconfig.json b/test/integration/jsconfig-paths-without-baseurl/jsconfig.json deleted file mode 100644 index 397117fed3d56f5..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/jsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "compilerOptions": { - "paths": { - "@c/*": ["./components/*"], - "@lib/*": ["./lib/a/*", "./lib/b/*"], - "@mycomponent": ["./components/hello.js"], - "*": ["./node_modules/*"] - } - } -} diff --git a/test/integration/jsconfig-paths-without-baseurl/lib/a/api.js b/test/integration/jsconfig-paths-without-baseurl/lib/a/api.js deleted file mode 100644 index 1a2637b0c956d46..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/lib/a/api.js +++ /dev/null @@ -1,5 +0,0 @@ -import data from 'mypackage/data' - -console.log(data) - -export default () => 'Hello from a' diff --git a/test/integration/jsconfig-paths-without-baseurl/lib/b/api.js b/test/integration/jsconfig-paths-without-baseurl/lib/b/api.js deleted file mode 100644 index 24ee786bd1a95b2..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/lib/b/api.js +++ /dev/null @@ -1 +0,0 @@ -export default () => 'Hello from b' diff --git a/test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js b/test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js deleted file mode 100644 index 0f1c1ce795f53df..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/lib/b/b-only.js +++ /dev/null @@ -1 +0,0 @@ -export default () => 'Hello from only b' diff --git a/test/integration/jsconfig-paths-without-baseurl/next.config.js b/test/integration/jsconfig-paths-without-baseurl/next.config.js deleted file mode 100644 index cc17cf48c578fd5..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/next.config.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - onDemandEntries: { - // Make sure entries are not getting disposed. - maxInactiveAge: 1000 * 60 * 60, - }, -} diff --git a/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js b/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js deleted file mode 100644 index f6ba735c283d676..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/data.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - hello: 'world', -} diff --git a/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js b/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js deleted file mode 100644 index f6ba735c283d676..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/node_modules/mypackage/myfile.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - hello: 'world', -} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js b/test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js deleted file mode 100644 index d2a31a937ae1142..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/pages/basic-alias.js +++ /dev/null @@ -1,9 +0,0 @@ -import React from 'react' -import { World } from '@c/world' -export default function HelloPage() { - return ( -
- -
- ) -} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js b/test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js deleted file mode 100644 index 148b4404d1fb592..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/pages/resolve-fallback.js +++ /dev/null @@ -1,5 +0,0 @@ -import React from 'react' -import api from '@lib/b-only' -export default function ResolveOrder() { - return
{api()}
-} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js b/test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js deleted file mode 100644 index f7fbe6cee827882..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/pages/resolve-order.js +++ /dev/null @@ -1,5 +0,0 @@ -import React from 'react' -import api from '@lib/api' -export default function ResolveOrder() { - return
{api()}
-} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js b/test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js deleted file mode 100644 index df2031b90ae62a9..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/pages/single-alias.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Hello } from '@mycomponent' - -export default function SingleAlias() { - return ( -
- -
- ) -} diff --git a/test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js b/test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js deleted file mode 100644 index 4b7e0c2b53f43c1..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/pages/wildcard-alias.js +++ /dev/null @@ -1,5 +0,0 @@ -import myfile from 'mypackage/myfile' - -export default function WildcardAlias() { - return
{myfile.hello}
-} diff --git a/test/integration/jsconfig-paths-without-baseurl/test/index.test.js b/test/integration/jsconfig-paths-without-baseurl/test/index.test.js deleted file mode 100644 index 7037101d88735a7..000000000000000 --- a/test/integration/jsconfig-paths-without-baseurl/test/index.test.js +++ /dev/null @@ -1,144 +0,0 @@ -/* eslint-env jest */ - -import fs from 'fs-extra' -import { join } from 'path' -import cheerio from 'cheerio' -import { - renderViaHTTP, - findPort, - launchApp, - nextBuild, - killApp, - check, -} from 'next-test-utils' - -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', () => { - let output = '' - - beforeAll(async () => { - appPort = await findPort() - app = await launchApp(appDir, appPort, { - onStderr(msg) { - output += msg || '' - }, - onStdout(msg) { - output += msg || '' - }, - }) - }) - afterAll(() => killApp(app)) - - it('should alias components', async () => { - const $ = await get$('/basic-alias') - expect($('body').text()).toMatch(/World/) - }) - - it('should resolve the first item in the array first', async () => { - const $ = await get$('/resolve-order') - expect($('body').text()).toMatch(/Hello from a/) - }) - - it('should resolve the second item as fallback', async () => { - const $ = await get$('/resolve-fallback') - expect($('body').text()).toMatch(/Hello from only b/) - }) - - it('should resolve a single matching alias', async () => { - const $ = await get$('/single-alias') - expect($('body').text()).toMatch(/Hello/) - }) - - it('should resolve a wildcard alias', async () => { - const $ = await get$('/wildcard-alias') - expect($('body').text()).toMatch(/world/) - }) - - it('should have correct module not found error', async () => { - const basicPage = join(appDir, 'pages/basic-alias.js') - const contents = await fs.readFile(basicPage, 'utf8') - - await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd')) - await renderViaHTTP(appPort, '/basic-alias') - - const found = await check( - () => output, - /Module not found: Can't resolve '@c\/worldd'/, - false - ) - await fs.writeFile(basicPage, contents) - expect(found).toBe(true) - }) - }) - - describe('should build', () => { - beforeAll(async () => { - await nextBuild(appDir) - }) - it('should trace correctly', async () => { - const appTrace = await fs.readJSON( - join(appDir, '.next/server/pages/_app.js.nft.json') - ) - const singleAliasTrace = await fs.readJSON( - join(appDir, '.next/server/pages/single-alias.js.nft.json') - ) - const wildcardAliasTrace = await fs.readJSON( - join(appDir, '.next/server/pages/wildcard-alias.js.nft.json') - ) - const resolveOrderTrace = await fs.readJSON( - join(appDir, '.next/server/pages/resolve-order.js.nft.json') - ) - const resolveFallbackTrace = await fs.readJSON( - join(appDir, '.next/server/pages/resolve-fallback.js.nft.json') - ) - const basicAliasTrace = await fs.readJSON( - join(appDir, '.next/server/pages/basic-alias.js.nft.json') - ) - expect( - appTrace.files.some((file) => file.includes('node_modules/next')) - ).toBe(true) - expect( - singleAliasTrace.files.some((file) => - file.includes('components/hello.js') - ) - ).toBe(false) - expect( - wildcardAliasTrace.files.some((file) => - file.includes('mypackage/myfile.js') - ) - ).toBe(true) - expect( - wildcardAliasTrace.files.some((file) => - file.includes('mypackage/data.js') - ) - ).toBe(false) - expect( - resolveOrderTrace.files.some((file) => file.includes('lib/a/api.js')) - ).toBe(false) - expect( - resolveOrderTrace.files.some((file) => - file.includes('mypackage/data.js') - ) - ).toBe(true) - expect( - resolveFallbackTrace.files.some((file) => - file.includes('lib/b/b-only.js') - ) - ).toBe(false) - expect( - basicAliasTrace.files.some((file) => - file.includes('components/world.js') - ) - ).toBe(false) - }) - }) -}) diff --git a/test/integration/jsconfig-paths/test/index.test.js b/test/integration/jsconfig-paths/test/index.test.js index 7037101d88735a7..c38e933b4cefeb5 100644 --- a/test/integration/jsconfig-paths/test/index.test.js +++ b/test/integration/jsconfig-paths/test/index.test.js @@ -3,6 +3,7 @@ import fs from 'fs-extra' import { join } from 'path' import cheerio from 'cheerio' +import * as path from 'path' import { renderViaHTTP, findPort, @@ -10,7 +11,9 @@ import { nextBuild, killApp, check, + File, } from 'next-test-utils' +import * as JSON5 from 'json5' const appDir = join(__dirname, '..') let appPort @@ -21,7 +24,7 @@ async function get$(path, query) { return cheerio.load(html) } -describe('TypeScript Features', () => { +function runTests() { describe('default behavior', () => { let output = '' @@ -141,4 +144,30 @@ describe('TypeScript Features', () => { ).toBe(false) }) }) +} + +const jsconfig = new File(path.resolve(__dirname, '../jsconfig.json')) + +describe('jsconfig paths', () => { + runTests() +}) + +describe('jsconfig paths without baseurl', () => { + beforeAll(() => { + const jsconfigContent = JSON5.parse(jsconfig.originalContent) + delete jsconfigContent.compilerOptions.baseUrl + jsconfigContent.compilerOptions.paths = { + '@c/*': ['./components/*'], + '@lib/*': ['./lib/a/*', './lib/b/*'], + '@mycomponent': ['./components/hello.js'], + '*': ['./node_modules/*'], + } + jsconfig.write(JSON.stringify(jsconfigContent, null, 2)) + }) + + afterAll(() => { + jsconfig.restore() + }) + + runTests() }) diff --git a/test/integration/typescript-paths/test/index.test.js b/test/integration/typescript-paths/test/index.test.js index 89ffa04fcf319d8..32a8e4923b93c37 100644 --- a/test/integration/typescript-paths/test/index.test.js +++ b/test/integration/typescript-paths/test/index.test.js @@ -24,33 +24,6 @@ async function get$(path, query) { const tsconfig = new File(path.resolve(__dirname, '../tsconfig.json')) function runTests() { - it('should alias components', async () => { - const $ = await get$('/basic-alias') - expect($('body').text()).toMatch(/World/) - }) - - it('should resolve the first item in the array first', async () => { - const $ = await get$('/resolve-order') - expect($('body').text()).toMatch(/Hello from a/) - }) - - it('should resolve the second item in as a fallback', async () => { - const $ = await get$('/resolve-fallback') - expect($('body').text()).toMatch(/Hello from only b/) - }) - - it('should resolve a single matching alias', async () => { - const $ = await get$('/single-alias') - expect($('body').text()).toMatch(/Hello/) - }) - - it('should not resolve to .d.ts files', async () => { - const $ = await get$('/alias-to-d-ts') - expect($('body').text()).toMatch(/Not aliased to d\.ts file/) - }) -} - -describe('TypeScript Features', () => { describe('default behavior', () => { beforeAll(async () => { appPort = await findPort() @@ -58,32 +31,57 @@ describe('TypeScript Features', () => { }) afterAll(() => killApp(app)) - runTests() - }) + it('should alias components', async () => { + const $ = await get$('/basic-alias') + expect($('body').text()).toMatch(/World/) + }) - describe('without baseurl', () => { - beforeAll(async () => { - const tsconfigContent = JSON5.parse(tsconfig.originalContent) - delete tsconfigContent.compilerOptions.baseUrl - tsconfigContent.compilerOptions.paths = { - 'isomorphic-unfetch': ['./types/unfetch.d.ts'], - '@c/*': ['./components/*'], - '@lib/*': ['./lib/a/*', './lib/b/*'], - '@mycomponent': ['./components/hello.tsx'], - 'd-ts-alias': [ - './components/alias-to-d-ts.d.ts', - './components/alias-to-d-ts.tsx', - ], - } - tsconfig.write(JSON.stringify(tsconfigContent, null, 2)) - appPort = await findPort() - app = await launchApp(appDir, appPort, {}) + it('should resolve the first item in the array first', async () => { + const $ = await get$('/resolve-order') + expect($('body').text()).toMatch(/Hello from a/) + }) + + it('should resolve the second item in as a fallback', async () => { + const $ = await get$('/resolve-fallback') + expect($('body').text()).toMatch(/Hello from only b/) }) - afterAll(() => { - tsconfig.restore() - killApp(app) + + it('should resolve a single matching alias', async () => { + const $ = await get$('/single-alias') + expect($('body').text()).toMatch(/Hello/) + }) + + it('should not resolve to .d.ts files', async () => { + const $ = await get$('/alias-to-d-ts') + expect($('body').text()).toMatch(/Not aliased to d\.ts file/) }) + }) +} + +describe('typescript paths', () => { + runTests() +}) - runTests() +describe('typescript paths without baseurl', () => { + beforeAll(async () => { + const tsconfigContent = JSON5.parse(tsconfig.originalContent) + delete tsconfigContent.compilerOptions.baseUrl + tsconfigContent.compilerOptions.paths = { + 'isomorphic-unfetch': ['./types/unfetch.d.ts'], + '@c/*': ['./components/*'], + '@lib/*': ['./lib/a/*', './lib/b/*'], + '@mycomponent': ['./components/hello.tsx'], + 'd-ts-alias': [ + './components/alias-to-d-ts.d.ts', + './components/alias-to-d-ts.tsx', + ], + } + tsconfig.write(JSON.stringify(tsconfigContent, null, 2)) }) + + afterAll(() => { + tsconfig.restore() + }) + + runTests() }) From a60c7d1da03a875322e0a45c4cf5b79275ad1ce6 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Thu, 3 Mar 2022 10:17:36 +0100 Subject: [PATCH 4/4] move var declaration around --- test/integration/jsconfig-paths/test/index.test.js | 4 ++-- test/integration/typescript-paths/test/index.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration/jsconfig-paths/test/index.test.js b/test/integration/jsconfig-paths/test/index.test.js index c38e933b4cefeb5..8680963c1a014fa 100644 --- a/test/integration/jsconfig-paths/test/index.test.js +++ b/test/integration/jsconfig-paths/test/index.test.js @@ -146,12 +146,12 @@ function runTests() { }) } -const jsconfig = new File(path.resolve(__dirname, '../jsconfig.json')) - describe('jsconfig paths', () => { runTests() }) +const jsconfig = new File(path.resolve(__dirname, '../jsconfig.json')) + describe('jsconfig paths without baseurl', () => { beforeAll(() => { const jsconfigContent = JSON5.parse(jsconfig.originalContent) diff --git a/test/integration/typescript-paths/test/index.test.js b/test/integration/typescript-paths/test/index.test.js index 32a8e4923b93c37..aeb3c297dd62709 100644 --- a/test/integration/typescript-paths/test/index.test.js +++ b/test/integration/typescript-paths/test/index.test.js @@ -21,8 +21,6 @@ async function get$(path, query) { return cheerio.load(html) } -const tsconfig = new File(path.resolve(__dirname, '../tsconfig.json')) - function runTests() { describe('default behavior', () => { beforeAll(async () => { @@ -62,6 +60,8 @@ describe('typescript paths', () => { runTests() }) +const tsconfig = new File(path.resolve(__dirname, '../tsconfig.json')) + describe('typescript paths without baseurl', () => { beforeAll(async () => { const tsconfigContent = JSON5.parse(tsconfig.originalContent)