Skip to content

Commit

Permalink
feat(swc): allow passing env property as option
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 5, 2023
1 parent 23c5ca7 commit 5235c75
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/swc/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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',
Expand Down
44 changes: 44 additions & 0 deletions packages/swc/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});

0 comments on commit 5235c75

Please sign in to comment.