diff --git a/packages/swc/src/module.ts b/packages/swc/src/module.ts index d39d73462..84933c172 100644 --- a/packages/swc/src/module.ts +++ b/packages/swc/src/module.ts @@ -9,7 +9,7 @@ import type { Options } from './type'; export function swc(input: Options = {}): Plugin { const filter = createFilter(input.include, input.exclude); - const swcOptions: SWCOptions = merge({}, input.swc || {}, { + const defaults: SWCOptions = { jsc: { target: 'es2020', parser: { @@ -22,7 +22,13 @@ export function swc(input: Options = {}): Plugin { }, loose: true } - }); + }; + + if (input.swc && input.swc.env) { + delete defaults.jsc?.target; + } + + const swcOptions: SWCOptions = merge({}, input.swc || {}, defaults); return { name: 'swc', diff --git a/packages/swc/test/test.js b/packages/swc/test/test.js index a98c54134..b7eca0495 100644 --- a/packages/swc/test/test.js +++ b/packages/swc/test/test.js @@ -83,3 +83,47 @@ test.serial('work with source map', async (t) => { t.is(sourceMap.fileName, 'unminified.js.map'); t.is(sourceMap.type, 'asset'); }); + +test.serial('allow passing swc.env', async (t) => { + const bundle = await rollup({ + input: 'test/fixtures/export-default.js', + plugins: [ + swc({ + env: { + targets: ['es2020'], + shippedProposals: true + } + }) + ] + }); + const result = await bundle.generate({ format: 'cjs' }); + t.is(result.output.length, 1); + const [output] = result.output; + t.is(output.code, "'use strict';\n\nvar exportDefault = 5;\n\nmodule.exports = exportDefault;\n"); +}); + +test.serial('catch error on jsc and env option', async (t) => { + try { + const bundle = await rollup({ + input: 'test/fixtures/export-default.js', + plugins: [ + swc({ + swc: { + jsc: { + target: 'es2020' + }, + env: { + targets: ['es2022'], + shippedProposals: true + } + } + }) + ] + }); + + await bundle.generate({ format: 'cjs' }); + t.is(0, 1); + } catch (e) { + t.is(e.message, '`env` and `jsc.target` cannot be used together'); + } +});