Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/get_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const DEFAULT_CONFIG: SWCConfig = {
runtime: 'automatic',
},
},
experimental: {
keepImportAttributes: true,
},
keepClassNames: true,
},
}
Expand Down Expand Up @@ -96,6 +99,9 @@ function tsConfigToSwcConfig(tsConfig: TsConfigJsonResolved): SWCConfig {
: {}),
},
},
experimental: {
keepImportAttributes: true,
},
keepClassNames: true,
},
}
Expand Down
12 changes: 12 additions & 0 deletions tests/get_config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ test.group('Get config', () => {
{
"swcConfig": {
"jsc": {
"experimental": {
"keepImportAttributes": true,
},
"externalHelpers": false,
"keepClassNames": true,
"parser": {
Expand Down Expand Up @@ -59,6 +62,9 @@ test.group('Get config', () => {
"swcConfig": {
"jsc": {
"baseUrl": undefined,
"experimental": {
"keepImportAttributes": true,
},
"externalHelpers": false,
"keepClassNames": true,
"parser": {
Expand Down Expand Up @@ -107,6 +113,9 @@ test.group('Get config', () => {
"swcConfig": {
"jsc": {
"baseUrl": undefined,
"experimental": {
"keepImportAttributes": true,
},
"externalHelpers": false,
"keepClassNames": true,
"parser": {
Expand Down Expand Up @@ -158,6 +167,9 @@ test.group('Get config', () => {
"swcConfig": {
"jsc": {
"baseUrl": undefined,
"experimental": {
"keepImportAttributes": true,
},
"externalHelpers": false,
"keepClassNames": true,
"parser": {
Expand Down
56 changes: 56 additions & 0 deletions tests/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,60 @@ test.group('Loader', (group) => {
'Error: Cannot import "./get_path.ts?v=1" using ".ts" extension. Enable "compilerOptions.rewriteRelativeImportExtensions" to import TypeScript files'
)
})

test('preserve import attributes for JSON imports', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {
compilerOptions: {},
})

await fs.createJson('data.json', {
name: 'ts-exec',
version: '1.0.0',
})

await fs.create(
'index.ts',
`
import data from './data.json' with { type: 'json' }
console.log(data.name)
`
)

const result = await spawnPromisified(
process.execPath,
['--no-warnings', '--import', './build/index.js', join(fs.basePath, 'index.ts')],
{}
)

assert.equal(result.stdout.trim(), 'ts-exec')
assert.equal(result.stderr.trim(), '')
})

test('preserve import attributes for dynamic JSON imports', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {
compilerOptions: {},
})

await fs.createJson('config.json', {
enabled: true,
timeout: 5000,
})

await fs.create(
'index.ts',
`
const config = await import('./config.json', { with: { type: 'json' } })
console.log(config.default.timeout)
`
)

const result = await spawnPromisified(
process.execPath,
['--no-warnings', '--import', './build/index.js', join(fs.basePath, 'index.ts')],
{}
)

assert.equal(result.stdout.trim(), '5000')
assert.equal(result.stderr.trim(), '')
})
})