Skip to content

Commit 51007ab

Browse files
Andaristdummdidummkaisermann
authored
fix: provide caller information to Babel (#449)
* feat(babel): provide caller information to Babel * Update test/transformers/babel.test.ts * Skip providing `supportsExportNamespaceFrom: true` for now * add comment for `supportsTopLevelAwait: true` * chore: update src/transformers/babel.ts Co-authored-by: Simon H <[email protected]> Co-authored-by: Christian Kaisermann <[email protected]>
1 parent d8b8108 commit 51007ab

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Diff for: src/transformers/babel.ts

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ const transformer: Transformer<Options.Babel> = async ({
2020
minified: false,
2121
ast: false,
2222
code: true,
23+
caller: {
24+
name: 'svelte-preprocess',
25+
supportsStaticESM: true,
26+
supportsDynamicImport: true,
27+
// this isn't supported by Svelte but let it error with a good error on this syntax untouched
28+
supportsTopLevelAwait: true,
29+
// todo: this can be enabled once all "peer deps" understand this
30+
// this syntax is supported since [email protected] and [email protected]
31+
// supportsExportNamespaceFrom: true,
32+
...options?.caller,
33+
},
2334
} as TransformOptions;
2435

2536
const result = await transformAsync(content, babelOptions);

Diff for: test/transformers/babel.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,37 @@ $: bar = foo?.b ?? 120
4040
$: bar = (_foo$b = foo == null ? void 0 : foo.b) != null ? _foo$b : 120;</script>"
4141
`);
4242
});
43+
44+
it('should not transpile import/export syntax with preset-env', async () => {
45+
const template = `<script>
46+
import foo from './foo'
47+
$: bar = foo?.b ?? 120
48+
</script>`;
49+
50+
const opts = sveltePreprocess({
51+
babel: {
52+
presets: [
53+
[
54+
'@babel/preset-env',
55+
{
56+
loose: true,
57+
targets: {
58+
esmodules: true,
59+
},
60+
},
61+
],
62+
],
63+
},
64+
});
65+
66+
const preprocessed = await preprocess(template, opts);
67+
68+
expect(preprocessed.code).toMatchInlineSnapshot(`
69+
"<script>var _foo$b;
70+
71+
import foo from './foo';
72+
73+
$: bar = (_foo$b = foo == null ? void 0 : foo.b) != null ? _foo$b : 120;</script>"
74+
`);
75+
});
4376
});

0 commit comments

Comments
 (0)