Skip to content

Commit

Permalink
fix import assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Jun 27, 2022
1 parent dee4556 commit 25db656
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
69 changes: 45 additions & 24 deletions src/test/transpilers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,48 +81,51 @@ test.suite('swc', (test) => {
});
});

test.suite('transforms various forms of jsx', (test) => {
const macro = test.macro(
(compilerOptions: object, expectedOutput: string) => [
() => `${JSON.stringify(compilerOptions)}`,
async (t) => {
const code = t.context.tsNodeUnderTest
.create({
swc: true,
skipProject: true,
compilerOptions: {
module: 'esnext',
...compilerOptions,
},
})
.compile(input, 'input.tsx');
expect(code.replace(/\/\/# sourceMappingURL.*/, '').trim()).toBe(
expectedOutput
);
},
]
);
const compileMacro = test.macro(
(compilerOptions: object, input: string, expectedOutput: string) => [
(title?: string) => title ?? `${JSON.stringify(compilerOptions)}`,
async (t) => {
const code = t.context.tsNodeUnderTest
.create({
swc: true,
skipProject: true,
compilerOptions: {
module: 'esnext',
...compilerOptions,
},
})
.compile(input, 'input.tsx');
expect(code.replace(/\/\/# sourceMappingURL.*/, '').trim()).toBe(
expectedOutput
);
},
]
);

test.suite('transforms various forms of jsx', (test) => {
const input = outdent`
const div = <div></div>;
`;

test(
macro,
compileMacro,
{ jsx: 'react' },
input,
`const div = /*#__PURE__*/ React.createElement("div", null);`
);
test(
macro,
compileMacro,
{ jsx: 'react-jsx' },
input,
outdent`
import { jsx as _jsx } from "react/jsx-runtime";
const div = /*#__PURE__*/ _jsx("div", {});
`
);
test(
macro,
compileMacro,
{ jsx: 'react-jsxdev' },
input,
outdent`
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
const div = /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, {
Expand All @@ -133,4 +136,22 @@ test.suite('swc', (test) => {
`
);
});

test.suite('preserves import assertions for json imports', (test) => {
test(
'basic json import',
compileMacro,
{ module: 'esnext' },
outdent`
import document from './document.json' assert {type: 'json'};
document;
`,
outdent`
import document from './document.json' assert {
type: 'json'
};
document;
`
);
});
});
4 changes: 4 additions & 0 deletions src/transpilers/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export function createSwcOptions(
tsx: isTsx,
decorators: experimentalDecorators,
dynamicImport: true,
importAssertions: true,
},
target: swcTarget,
transform: {
Expand All @@ -248,6 +249,9 @@ export function createSwcOptions(
} as swcTypes.ReactConfig,
},
keepClassNames,
experimental: {
keepImportAssertions: true,
},
} as swcTypes.JscConfig,
};

Expand Down

0 comments on commit 25db656

Please sign in to comment.