Skip to content

Commit 6bc094f

Browse files
committed
feat: use onlyRemoveTypeImports
1 parent c188ada commit 6bc094f

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

packages/vite/src/node/plugins/oxc.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function transformWithOxc(
6161
if (lang === 'ts' || lang === 'tsx') {
6262
const loadedTsconfig = await loadTsconfigJsonForFile(filename)
6363
const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}
64-
// tsc compiler alwaysStrict/experimentalDecorators/importsNotUsedAsValues/preserveValueImports/target/useDefineForClassFields/verbatimModuleSyntax
64+
// tsc compiler experimentalDecorators/target/useDefineForClassFields
6565

6666
resolvedOptions.jsx ??= {}
6767
if (loadedCompilerOptions.jsxFactory) {
@@ -91,6 +91,47 @@ export async function transformWithOxc(
9191
default:
9292
break
9393
}
94+
95+
/**
96+
* | preserveValueImports | importsNotUsedAsValues | verbatimModuleSyntax | onlyRemoveTypeImports |
97+
* | -------------------- | ---------------------- | -------------------- |---------------------- |
98+
* | false | remove | false | false |
99+
* | false | preserve, error | - | - |
100+
* | true | remove | - | - |
101+
* | true | preserve, error | true | true |
102+
*/
103+
if (loadedCompilerOptions.verbatimModuleSyntax !== undefined) {
104+
resolvedOptions.typescript ??= {}
105+
resolvedOptions.typescript.onlyRemoveTypeImports =
106+
loadedCompilerOptions.verbatimModuleSyntax
107+
} else if (
108+
loadedCompilerOptions.preserveValueImports !== undefined ||
109+
loadedCompilerOptions.importsNotUsedAsValues !== undefined
110+
) {
111+
const preserveValueImports =
112+
loadedCompilerOptions.preserveValueImports ?? false
113+
const importsNotUsedAsValues =
114+
loadedCompilerOptions.importsNotUsedAsValues ?? 'remove'
115+
if (
116+
preserveValueImports === false &&
117+
importsNotUsedAsValues === 'remove'
118+
) {
119+
resolvedOptions.typescript ??= {}
120+
resolvedOptions.typescript.onlyRemoveTypeImports = true
121+
} else if (
122+
preserveValueImports === true &&
123+
(importsNotUsedAsValues === 'preserve' ||
124+
importsNotUsedAsValues === 'error')
125+
) {
126+
resolvedOptions.typescript ??= {}
127+
resolvedOptions.typescript.onlyRemoveTypeImports = false
128+
} else {
129+
ctx.warn(
130+
`preserveValueImports=${preserveValueImports} + importsNotUsedAsValues=${importsNotUsedAsValues} is not supported by oxc.` +
131+
'Please migrate to the new verbatimModuleSyntax option.',
132+
)
133+
}
134+
}
94135
}
95136

96137
const result = transform(filename, code, resolvedOptions)

playground/tsconfig-json/__tests__/tsconfig-json.spec.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import { describe, expect, test } from 'vitest'
55
import { browserLogs, isServe, serverLogs } from '~utils'
66

77
test.skip('should respected each `tsconfig.json`s compilerOptions', () => {
8-
// main side effect should be called (because of `"importsNotUsedAsValues": "preserve"`)
8+
// main side effect should be called (because of `"verbatimModuleSyntax": true`)
99
expect(browserLogs).toContain('main side effect')
1010
// main base setter should not be called (because of `"useDefineForClassFields": true"`)
1111
expect(browserLogs).not.toContain('data setter in MainBase')
1212

13-
// nested side effect should not be called (because "importsNotUsedAsValues" is not set, defaults to "remove")
13+
// nested side effect should not be called (because "verbatimModuleSyntax" is not set, defaults to false)
1414
expect(browserLogs).not.toContain('nested side effect')
1515
// nested base setter should be called (because of `"useDefineForClassFields": false"`)
1616
expect(browserLogs).toContain('data setter in NestedBase')
1717

18-
// nested-with-extends side effect should be called (because "importsNotUsedAsValues" is extended from the main tsconfig.json, which is "preserve")
18+
// nested-with-extends side effect should be called (because "verbatimModuleSyntax" is extended from the main tsconfig.json, which is true)
1919
expect(browserLogs).toContain('nested-with-extends side effect')
2020
// nested-with-extends base setter should be called (because of `"useDefineForClassFields": false"`)
2121
expect(browserLogs).toContain('data setter in NestedWithExtendsBase')
@@ -42,8 +42,8 @@ describe('transformWithEsbuild', () => {
4242
},
4343
},
4444
})
45-
// "importsNotUsedAsValues": "preserve" from tsconfig.json should still work
46-
expect(result.code).toContain('import "./not-used-type";')
45+
// "verbatimModuleSyntax": true from tsconfig.json should still work
46+
expect(result.code).toMatch(/import.*".\/not-used-type";/)
4747
})
4848

4949
test('overwrite tsconfigRaw string', async () => {
@@ -56,26 +56,24 @@ describe('transformWithEsbuild', () => {
5656
}
5757
}`,
5858
})
59-
// "importsNotUsedAsValues": "preserve" from tsconfig.json should not be read
60-
// and defaults to "remove"
61-
expect(result.code).not.toContain('import "./not-used-type";')
59+
// "verbatimModuleSyntax": true from from tsconfig.json should not be read
60+
// and defaults to false
61+
expect(result.code).not.toMatch(/import.*".\/not-used-type";/)
6262
})
6363

64-
test('preserveValueImports', async () => {
64+
test('verbatimModuleSyntax', async () => {
6565
const main = path.resolve(__dirname, '../src/main.ts')
6666
const mainContent = fs.readFileSync(main, 'utf-8')
6767
const result = await transformWithEsbuild(mainContent, main, {
6868
tsconfigRaw: {
6969
compilerOptions: {
7070
useDefineForClassFields: false,
71-
preserveValueImports: true,
71+
verbatimModuleSyntax: false,
7272
},
7373
},
7474
})
75-
// "importsNotUsedAsValues": "preserve" from tsconfig.json should still work
76-
expect(result.code).toContain(
77-
'import { MainTypeOnlyClass } from "./not-used-type";',
78-
)
75+
// "verbatimModuleSyntax": false from tsconfig.json should still work
76+
expect(result.code).not.toMatch(/import.*".\/not-used-type";/)
7977
})
8078

8179
test('experimentalDecorators', async () => {

playground/tsconfig-json/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"noImplicitReturns": true,
1515

1616
"useDefineForClassFields": true,
17-
"importsNotUsedAsValues": "preserve",
17+
"verbatimModuleSyntax": true,
1818
"experimentalDecorators": true
1919
},
2020
"include": ["./src"]

0 commit comments

Comments
 (0)