Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swc): allow passing env property as option #1624

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
});
Loading