-
Notifications
You must be signed in to change notification settings - Fork 27k
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
feat: allow module: Preserve
tsconfig option
#64110
Merged
huozhi
merged 9 commits into
vercel:canary
from
juliusmarminge:julius/tsconfig-module-preserve
Apr 8, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d7694cd
allow `module: Preserve` tsconfig option
juliusmarminge ec915f6
check other options too
juliusmarminge edb4f11
revert ts bump and test using e2e for now
juliusmarminge 474837d
comment where to find e2e test
juliusmarminge c68be9b
Merge branch 'canary' into julius/tsconfig-module-preserve
juliusmarminge 28b0b18
update test
huozhi c570e73
Merge remote-tracking branch 'origin/canary' into julius/tsconfig-mod…
huozhi 5913592
update test
huozhi aac9d2c
stablize test
huozhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,14 +40,11 @@ function getDesiredCompilerOptions( | |
// These values are required and cannot be changed by the user | ||
// Keep this in sync with the webpack config | ||
// 'parsedValue' matches the output value from ts.parseJsonConfigFileContent() | ||
esModuleInterop: { | ||
value: true, | ||
reason: 'requirement for SWC / babel', | ||
}, | ||
module: { | ||
parsedValue: ts.ModuleKind.ESNext, | ||
// All of these values work: | ||
parsedValues: [ | ||
semver.gte(ts.version, '5.4.0') && (ts.ModuleKind as any).Preserve, | ||
ts.ModuleKind.ES2020, | ||
ts.ModuleKind.ESNext, | ||
ts.ModuleKind.CommonJS, | ||
|
@@ -58,28 +55,47 @@ function getDesiredCompilerOptions( | |
value: 'esnext', | ||
reason: 'for dynamic import() support', | ||
}, | ||
moduleResolution: { | ||
// In TypeScript 5.0, `NodeJs` has renamed to `Node10` | ||
parsedValue: | ||
ts.ModuleResolutionKind.Bundler ?? | ||
ts.ModuleResolutionKind.NodeNext ?? | ||
(ts.ModuleResolutionKind as any).Node10 ?? | ||
ts.ModuleResolutionKind.NodeJs, | ||
// All of these values work: | ||
parsedValues: [ | ||
(ts.ModuleResolutionKind as any).Node10 ?? | ||
ts.ModuleResolutionKind.NodeJs, | ||
// only newer TypeScript versions have this field, it | ||
// will be filtered for new versions of TypeScript | ||
(ts.ModuleResolutionKind as any).Node12, | ||
ts.ModuleResolutionKind.Node16, | ||
ts.ModuleResolutionKind.NodeNext, | ||
ts.ModuleResolutionKind.Bundler, | ||
].filter((val) => typeof val !== 'undefined'), | ||
value: 'node', | ||
reason: 'to match webpack resolution', | ||
}, | ||
resolveJsonModule: { value: true, reason: 'to match webpack resolution' }, | ||
// TODO: Semver check not needed once Next.js repo uses 5.4. | ||
...(semver.gte(ts.version, '5.4.0') && | ||
tsOptions?.module === (ts.ModuleKind as any).Preserve | ||
? { | ||
// TypeScript 5.4 introduced `Preserve`. Using `Preserve` implies | ||
// - `moduleResolution` is `Bundler` | ||
// - `esModuleInterop` is `true` | ||
// - `resolveJsonModule` is `true` | ||
// This means that if the user is using Preserve, they don't need these options | ||
Comment on lines
+62
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
: { | ||
esModuleInterop: { | ||
value: true, | ||
reason: 'requirement for SWC / babel', | ||
}, | ||
moduleResolution: { | ||
// In TypeScript 5.0, `NodeJs` has renamed to `Node10` | ||
parsedValue: | ||
ts.ModuleResolutionKind.Bundler ?? | ||
ts.ModuleResolutionKind.NodeNext ?? | ||
(ts.ModuleResolutionKind as any).Node10 ?? | ||
ts.ModuleResolutionKind.NodeJs, | ||
// All of these values work: | ||
parsedValues: [ | ||
(ts.ModuleResolutionKind as any).Node10 ?? | ||
ts.ModuleResolutionKind.NodeJs, | ||
// only newer TypeScript versions have this field, it | ||
// will be filtered for new versions of TypeScript | ||
(ts.ModuleResolutionKind as any).Node12, | ||
ts.ModuleResolutionKind.Node16, | ||
ts.ModuleResolutionKind.NodeNext, | ||
ts.ModuleResolutionKind.Bundler, | ||
].filter((val) => typeof val !== 'undefined'), | ||
value: 'node', | ||
reason: 'to match webpack resolution', | ||
}, | ||
resolveJsonModule: { | ||
value: true, | ||
reason: 'to match webpack resolution', | ||
}, | ||
}), | ||
...(tsOptions?.verbatimModuleSyntax === true | ||
? undefined | ||
: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { retry } from 'next-test-utils' | ||
import stripAnsi from 'strip-ansi' | ||
|
||
describe('tsconfig module: preserve', () => { | ||
const { next } = nextTestSetup({ | ||
files: { | ||
'tsconfig.json': JSON.stringify({ | ||
compilerOptions: { module: 'preserve' }, | ||
}), | ||
'pages/index.tsx': ` | ||
export default function Page() { | ||
return <p>hello world</p> | ||
} | ||
`, | ||
}, | ||
dependencies: { | ||
typescript: '5.4.4', | ||
}, | ||
}) | ||
|
||
it('allows you to skip moduleResolution, esModuleInterop and resolveJsonModule when using "module: preserve"', async () => { | ||
let output = '' | ||
|
||
await retry(() => { | ||
output = stripAnsi(next.cliOutput) | ||
expect(output).toContain( | ||
'The following mandatory changes were made to your tsconfig.json' | ||
) | ||
}) | ||
|
||
expect(output).not.toContain('moduleResolution') | ||
expect(output).not.toContain('esModuleInterop') | ||
expect(output).not.toContain('resolveJsonModule') | ||
|
||
expect(await next.readFile('tsconfig.json')).toMatchInlineSnapshot(` | ||
"{ | ||
"compilerOptions": { | ||
"module": "preserve", | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
"esnext" | ||
], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve" | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} | ||
" | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,8 @@ import path from 'path' | |
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"esModuleInterop": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this moved down a line to keep the code above a bit cleaner without needing multiple checks |
||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
|
@@ -91,8 +91,8 @@ import path from 'path' | |
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
|
@@ -430,8 +430,8 @@ import path from 'path' | |
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
|
@@ -541,8 +541,8 @@ import path from 'path' | |
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"jsx": "preserve", | ||
|
@@ -598,8 +598,8 @@ import path from 'path' | |
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"jsx": "preserve", | ||
|
@@ -753,5 +753,60 @@ import path from 'path' | |
" | ||
`) | ||
}) | ||
|
||
// TODO: Enable this test when repo has upgraded to TypeScript 5.4. Currently tested as E2E: tsconfig-module-preserve | ||
it.skip('allows you to skip moduleResolution, esModuleInterop and resolveJsonModule when using "module: preserve"', async () => { | ||
expect(await exists(tsConfig)).toBe(false) | ||
|
||
await writeFile( | ||
tsConfig, | ||
`{ "compilerOptions": { "module": "preserve" } }` | ||
) | ||
await new Promise((resolve) => setTimeout(resolve, 500)) | ||
const { code, stderr, stdout } = await nextBuild(appDir, undefined, { | ||
stderr: true, | ||
stdout: true, | ||
}) | ||
expect(stderr + stdout).not.toContain('moduleResolution') | ||
expect(stderr + stdout).not.toContain('esModuleInterop') | ||
expect(stderr + stdout).not.toContain('resolveJsonModule') | ||
expect(code).toBe(0) | ||
|
||
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(` | ||
"{ | ||
"compilerOptions": { | ||
"module": "preserve", | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
"esnext" | ||
], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"strictNullChecks": true | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
".next/types/**/*.ts", | ||
"**/*.ts", | ||
"**/*.tsx" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} | ||
" | ||
`) | ||
}) | ||
} | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps
Preserve
should be the default if using 5.4 or above, but i didn't feel like updating all the tests. Can do so if you think it's a good idea