From 51007abf946ffdd1d407c6bcf86439d38e113626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 17 Jan 2022 14:26:46 +0100 Subject: [PATCH] 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 <5968653+dummdidumm@users.noreply.github.com> Co-authored-by: Christian Kaisermann --- src/transformers/babel.ts | 11 +++++++++++ test/transformers/babel.test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/transformers/babel.ts b/src/transformers/babel.ts index dc9645ea..39863673 100644 --- a/src/transformers/babel.ts +++ b/src/transformers/babel.ts @@ -20,6 +20,17 @@ const transformer: Transformer = 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); diff --git a/test/transformers/babel.test.ts b/test/transformers/babel.test.ts index 2e16ffa7..081ae4f2 100644 --- a/test/transformers/babel.test.ts +++ b/test/transformers/babel.test.ts @@ -40,4 +40,37 @@ $: bar = foo?.b ?? 120 $: bar = (_foo$b = foo == null ? void 0 : foo.b) != null ? _foo$b : 120;" `); }); + + it('should not transpile import/export syntax with preset-env', async () => { + const template = ``; + + const opts = sveltePreprocess({ + babel: { + presets: [ + [ + '@babel/preset-env', + { + loose: true, + targets: { + esmodules: true, + }, + }, + ], + ], + }, + }); + + const preprocessed = await preprocess(template, opts); + + expect(preprocessed.code).toMatchInlineSnapshot(` + "" + `); + }); });