Skip to content

Commit

Permalink
fix: provide caller information to Babel (#449)
Browse files Browse the repository at this point in the history
* 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 <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: Christian Kaisermann <christian@kaisermann.me>
  • Loading branch information
3 people committed Jan 17, 2022
1 parent d8b8108 commit 51007ab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/transformers/babel.ts
Expand Up @@ -20,6 +20,17 @@ const transformer: Transformer<Options.Babel> = async ({
minified: false,
ast: false,
code: true,
caller: {
name: 'svelte-preprocess',
supportsStaticESM: true,
supportsDynamicImport: true,
// this isn't supported by Svelte but let it error with a good error on this syntax untouched
supportsTopLevelAwait: true,
// todo: this can be enabled once all "peer deps" understand this
// this syntax is supported since rollup@1.26.0 and webpack@5.0.0-beta.21
// supportsExportNamespaceFrom: true,
...options?.caller,
},
} as TransformOptions;

const result = await transformAsync(content, babelOptions);
Expand Down
33 changes: 33 additions & 0 deletions test/transformers/babel.test.ts
Expand Up @@ -40,4 +40,37 @@ $: bar = foo?.b ?? 120
$: bar = (_foo$b = foo == null ? void 0 : foo.b) != null ? _foo$b : 120;</script>"
`);
});

it('should not transpile import/export syntax with preset-env', async () => {
const template = `<script>
import foo from './foo'
$: bar = foo?.b ?? 120
</script>`;

const opts = sveltePreprocess({
babel: {
presets: [
[
'@babel/preset-env',
{
loose: true,
targets: {
esmodules: true,
},
},
],
],
},
});

const preprocessed = await preprocess(template, opts);

expect(preprocessed.code).toMatchInlineSnapshot(`
"<script>var _foo$b;
import foo from './foo';
$: bar = (_foo$b = foo == null ? void 0 : foo.b) != null ? _foo$b : 120;</script>"
`);
});
});

0 comments on commit 51007ab

Please sign in to comment.