Skip to content

Commit

Permalink
feat: use onlyRemoveTypeImports
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Oct 15, 2024
1 parent c188ada commit 6bc094f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
43 changes: 42 additions & 1 deletion packages/vite/src/node/plugins/oxc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function transformWithOxc(
if (lang === 'ts' || lang === 'tsx') {
const loadedTsconfig = await loadTsconfigJsonForFile(filename)
const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}
// tsc compiler alwaysStrict/experimentalDecorators/importsNotUsedAsValues/preserveValueImports/target/useDefineForClassFields/verbatimModuleSyntax
// tsc compiler experimentalDecorators/target/useDefineForClassFields

resolvedOptions.jsx ??= {}
if (loadedCompilerOptions.jsxFactory) {
Expand Down Expand Up @@ -91,6 +91,47 @@ export async function transformWithOxc(
default:
break
}

/**
* | preserveValueImports | importsNotUsedAsValues | verbatimModuleSyntax | onlyRemoveTypeImports |
* | -------------------- | ---------------------- | -------------------- |---------------------- |
* | false | remove | false | false |
* | false | preserve, error | - | - |
* | true | remove | - | - |
* | true | preserve, error | true | true |
*/
if (loadedCompilerOptions.verbatimModuleSyntax !== undefined) {
resolvedOptions.typescript ??= {}
resolvedOptions.typescript.onlyRemoveTypeImports =
loadedCompilerOptions.verbatimModuleSyntax
} else if (
loadedCompilerOptions.preserveValueImports !== undefined ||
loadedCompilerOptions.importsNotUsedAsValues !== undefined
) {
const preserveValueImports =
loadedCompilerOptions.preserveValueImports ?? false
const importsNotUsedAsValues =
loadedCompilerOptions.importsNotUsedAsValues ?? 'remove'
if (
preserveValueImports === false &&
importsNotUsedAsValues === 'remove'
) {
resolvedOptions.typescript ??= {}
resolvedOptions.typescript.onlyRemoveTypeImports = true
} else if (
preserveValueImports === true &&
(importsNotUsedAsValues === 'preserve' ||
importsNotUsedAsValues === 'error')
) {
resolvedOptions.typescript ??= {}
resolvedOptions.typescript.onlyRemoveTypeImports = false
} else {
ctx.warn(
`preserveValueImports=${preserveValueImports} + importsNotUsedAsValues=${importsNotUsedAsValues} is not supported by oxc.` +
'Please migrate to the new verbatimModuleSyntax option.',
)
}
}
}

const result = transform(filename, code, resolvedOptions)
Expand Down
26 changes: 12 additions & 14 deletions playground/tsconfig-json/__tests__/tsconfig-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { describe, expect, test } from 'vitest'
import { browserLogs, isServe, serverLogs } from '~utils'

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

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

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

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

test('preserveValueImports', async () => {
test('verbatimModuleSyntax', async () => {
const main = path.resolve(__dirname, '../src/main.ts')
const mainContent = fs.readFileSync(main, 'utf-8')
const result = await transformWithEsbuild(mainContent, main, {
tsconfigRaw: {
compilerOptions: {
useDefineForClassFields: false,
preserveValueImports: true,
verbatimModuleSyntax: false,
},
},
})
// "importsNotUsedAsValues": "preserve" from tsconfig.json should still work
expect(result.code).toContain(
'import { MainTypeOnlyClass } from "./not-used-type";',
)
// "verbatimModuleSyntax": false from tsconfig.json should still work
expect(result.code).not.toMatch(/import.*".\/not-used-type";/)
})

test('experimentalDecorators', async () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/tsconfig-json/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"noImplicitReturns": true,

"useDefineForClassFields": true,
"importsNotUsedAsValues": "preserve",
"verbatimModuleSyntax": true,
"experimentalDecorators": true
},
"include": ["./src"]
Expand Down

0 comments on commit 6bc094f

Please sign in to comment.