Skip to content

Commit 25db656

Browse files
committed
fix import assertions
1 parent dee4556 commit 25db656

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

src/test/transpilers.spec.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,48 +81,51 @@ test.suite('swc', (test) => {
8181
});
8282
});
8383

84-
test.suite('transforms various forms of jsx', (test) => {
85-
const macro = test.macro(
86-
(compilerOptions: object, expectedOutput: string) => [
87-
() => `${JSON.stringify(compilerOptions)}`,
88-
async (t) => {
89-
const code = t.context.tsNodeUnderTest
90-
.create({
91-
swc: true,
92-
skipProject: true,
93-
compilerOptions: {
94-
module: 'esnext',
95-
...compilerOptions,
96-
},
97-
})
98-
.compile(input, 'input.tsx');
99-
expect(code.replace(/\/\/# sourceMappingURL.*/, '').trim()).toBe(
100-
expectedOutput
101-
);
102-
},
103-
]
104-
);
84+
const compileMacro = test.macro(
85+
(compilerOptions: object, input: string, expectedOutput: string) => [
86+
(title?: string) => title ?? `${JSON.stringify(compilerOptions)}`,
87+
async (t) => {
88+
const code = t.context.tsNodeUnderTest
89+
.create({
90+
swc: true,
91+
skipProject: true,
92+
compilerOptions: {
93+
module: 'esnext',
94+
...compilerOptions,
95+
},
96+
})
97+
.compile(input, 'input.tsx');
98+
expect(code.replace(/\/\/# sourceMappingURL.*/, '').trim()).toBe(
99+
expectedOutput
100+
);
101+
},
102+
]
103+
);
105104

105+
test.suite('transforms various forms of jsx', (test) => {
106106
const input = outdent`
107107
const div = <div></div>;
108108
`;
109109

110110
test(
111-
macro,
111+
compileMacro,
112112
{ jsx: 'react' },
113+
input,
113114
`const div = /*#__PURE__*/ React.createElement("div", null);`
114115
);
115116
test(
116-
macro,
117+
compileMacro,
117118
{ jsx: 'react-jsx' },
119+
input,
118120
outdent`
119121
import { jsx as _jsx } from "react/jsx-runtime";
120122
const div = /*#__PURE__*/ _jsx("div", {});
121123
`
122124
);
123125
test(
124-
macro,
126+
compileMacro,
125127
{ jsx: 'react-jsxdev' },
128+
input,
126129
outdent`
127130
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
128131
const div = /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, {
@@ -133,4 +136,22 @@ test.suite('swc', (test) => {
133136
`
134137
);
135138
});
139+
140+
test.suite('preserves import assertions for json imports', (test) => {
141+
test(
142+
'basic json import',
143+
compileMacro,
144+
{ module: 'esnext' },
145+
outdent`
146+
import document from './document.json' assert {type: 'json'};
147+
document;
148+
`,
149+
outdent`
150+
import document from './document.json' assert {
151+
type: 'json'
152+
};
153+
document;
154+
`
155+
);
156+
});
136157
});

src/transpilers/swc.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ export function createSwcOptions(
233233
tsx: isTsx,
234234
decorators: experimentalDecorators,
235235
dynamicImport: true,
236+
importAssertions: true,
236237
},
237238
target: swcTarget,
238239
transform: {
@@ -248,6 +249,9 @@ export function createSwcOptions(
248249
} as swcTypes.ReactConfig,
249250
},
250251
keepClassNames,
252+
experimental: {
253+
keepImportAssertions: true,
254+
},
251255
} as swcTypes.JscConfig,
252256
};
253257

0 commit comments

Comments
 (0)