Skip to content

Commit

Permalink
Add support for tsconfig moduleResolution node | node12 | nodenext (#…
Browse files Browse the repository at this point in the history
…36189)

- Fixes #35572 

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
styfle and ijjk committed Apr 15, 2022
1 parent 1c20d3d commit 4c15f89
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -167,7 +167,7 @@
"tree-kill": "1.2.2",
"tsec": "0.2.1",
"turbo": "1.0.28",
"typescript": "4.5.5",
"typescript": "4.6.3",

This comment was marked as spam.

Copy link
@Zebak2021

Zebak2021 Apr 29, 2022

packages/next/lib/typescript/writeConfigurationDefaults.ts

"wait-port": "0.2.2",
"web-streams-polyfill": "2.1.1",
"webpack": "link:./node_modules/webpack5",
Expand Down
6 changes: 6 additions & 0 deletions packages/next/lib/typescript/writeConfigurationDefaults.ts
Expand Up @@ -54,6 +54,12 @@ function getDesiredCompilerOptions(
},
moduleResolution: {
parsedValue: ts.ModuleResolutionKind.NodeJs,
// All of these values work:
parsedValues: [
ts.ModuleResolutionKind.NodeJs,
ts.ModuleResolutionKind.Node12,
ts.ModuleResolutionKind.NodeNext,
],
value: 'node',
reason: 'to match webpack resolution',
},
Expand Down
1 change: 0 additions & 1 deletion packages/next/server/web/utils.ts
Expand Up @@ -131,7 +131,6 @@ export function validateURL(url: string | URL): string {
} catch (error: any) {
throw new Error(
`URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls`,
// @ts-expect-error This will work for people who enable the error causes polyfill
{ cause: error }
)
}
Expand Down
10 changes: 8 additions & 2 deletions packages/next/taskfile.js
Expand Up @@ -192,11 +192,17 @@ export async function ncc_next__react_dev_overlay(task, opts) {
)
const content = fs.readFileSync(clientFile, 'utf8')
// remove AMD define branch as this forces the module to not
// be treated as commonjs in serverless mode
// be treated as commonjs in serverless/client mode
fs.writeFileSync(
clientFile,
content.replace(
'if(typeof define=="function"&&typeof define.amd=="object"&&define.amd){r.platform=v;define((function(){return v}))}else ',
new RegExp(
'if(typeof define=="function"&&typeof define.amd=="object"&&define.amd){r.platform=b;define((function(){return b}))}else '.replace(
/[|\\{}()[\]^$+*?.-]/g,
'\\$&'
),
'g'
),
''
)
)
Expand Down
46 changes: 46 additions & 0 deletions test/integration/tsconfig-verifier/test/index.test.js
Expand Up @@ -264,6 +264,52 @@ describe('tsconfig.json verifier', () => {
`)
})

it('allows you to set node12 moduleResolution mode', async () => {
expect(await exists(tsConfig)).toBe(false)

await writeFile(
tsConfig,
`{ "compilerOptions": { "esModuleInterop": false, "moduleResolution": "node12" } }`
)
await new Promise((resolve) => setTimeout(resolve, 500))
const { code } = await nextBuild(appDir)
expect(code).toBe(0)

expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(`
"{
\\"compilerOptions\\": {
\\"esModuleInterop\\": true,
\\"moduleResolution\\": \\"node12\\",
\\"target\\": \\"es5\\",
\\"lib\\": [
\\"dom\\",
\\"dom.iterable\\",
\\"esnext\\"
],
\\"allowJs\\": true,
\\"skipLibCheck\\": true,
\\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true,
\\"incremental\\": true,
\\"module\\": \\"esnext\\",
\\"resolveJsonModule\\": true,
\\"isolatedModules\\": true,
\\"jsx\\": \\"preserve\\"
},
\\"include\\": [
\\"next-env.d.ts\\",
\\"**/*.ts\\",
\\"**/*.tsx\\"
],
\\"exclude\\": [
\\"node_modules\\"
]
}
"
`)
})

it('allows you to extend another configuration file', async () => {
expect(await exists(tsConfig)).toBe(false)
expect(await exists(tsConfigBase)).toBe(false)
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -20667,10 +20667,10 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@4.5.5:
version "4.5.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==
typescript@4.6.3:
version "4.6.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.3.tgz#eefeafa6afdd31d725584c67a0eaba80f6fc6c6c"
integrity sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==

typescript@^4.1.3:
version "4.1.3"
Expand Down

5 comments on commit 4c15f89

@jasonwilliams
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@styfle @ijjk it looks like TypeScript 4.7 (which is now RC) have gone with "node16". is NextJS ready for this change? https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-rc/#ecmascript-module-support-in-node-js

@styfle
Copy link
Member Author

@styfle styfle commented on 4c15f89 May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not ready. Looks like TS 4.6 doesn't have the ts.ModuleResolutionKind.Node16 exported so we'll have to update to TS 4.7 in order to support it. Would you like to submit a PR?

@jasonwilliams
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reply @styfle if that's the case im happy to wait until TypeScript 4.7 is released as i understand you can't depend on a version which isn't out yet

@jasonwilliams
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@styfle
Copy link
Member Author

@styfle styfle commented on 4c15f89 May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonwilliams Great! Feel free to submit a PR 👍

Please sign in to comment.