Skip to content

Commit b087511

Browse files
committed
add minification to builder, move from terser to swc
1 parent 8b3dfec commit b087511

File tree

6 files changed

+48
-180
lines changed

6 files changed

+48
-180
lines changed

packages/core-js-builder/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const { script } = await builder({
3131
// output format, 'bundle' by default, can be 'cjs' or 'esm', and in this case
3232
// the result will not be bundled and will contain imports of required modules
3333
format: 'bundle',
34+
// minify the output in the case of format: bundle, `true` by default
35+
minify: true,
3436
// optional target filename, if it's missed a file will not be created
3537
filename: PATH_TO_MY_COREJS_BUNDLE,
3638
});

packages/core-js-builder/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Options = Pick<CompatOptions, "exclude" | "modules" | "targets"> & {
2222
format?: Format,
2323
/** optional target filename, if it's missed a file will not be created */
2424
filename?: string,
25+
/** minify the output in the case of format: bundle, `true` by default */
26+
minify?: boolean,
2527
/** shows summary for the bundle, disabled by default */
2628
summary?: Summary,
2729
};

packages/core-js-builder/index.mjs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default async function ({
3535
exclude = [],
3636
targets = null,
3737
format = 'bundle',
38+
minify = true,
3839
filename = null,
3940
summary = {},
4041
} = {}) {
@@ -81,16 +82,48 @@ export default async function ({
8182
}
8283

8384
// rolldown helpers / wrappers contain es2015 syntax
84-
code = (await transform(code, {
85+
const swcOptions = {
8586
env: {
8687
include: [
8788
'transform-arrow-functions',
8889
'transform-shorthand-properties',
8990
],
9091
},
91-
})).code;
92+
};
93+
94+
if (minify) Object.assign(swcOptions, {
95+
minify: true,
96+
jsc: {
97+
minify: {
98+
compress: {
99+
arrows: false,
100+
ecma: 5,
101+
hoist_funs: true,
102+
keep_fnames: true,
103+
pure_getters: true,
104+
reduce_funcs: true,
105+
// document.all detection case
106+
typeofs: false,
107+
unsafe_proto: true,
108+
unsafe_undefined: true,
109+
},
110+
mangle: {
111+
keep_fnames: true,
112+
safari10: true,
113+
toplevel: true,
114+
},
115+
format: {
116+
comments: false,
117+
ecma: 5,
118+
},
119+
},
120+
},
121+
});
122+
123+
code = (await transform(code, swcOptions)).code;
92124

93-
code = `!function (undefined) { 'use strict'; ${ code } }();\n`;
125+
// swc considers output code as a module and drops 'use strict`
126+
code = `!function () { 'use strict'; ${ code } }();\n`;
94127
} else {
95128
code = importModules(list, format);
96129
}
Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,24 @@
1-
import { minify } from 'terser';
21
import builder from '@core-js/builder';
3-
import config from '@core-js/builder/config.mjs';
42

53
const { cyan, green } = chalk;
64
const ESMODULES = argv._.includes('esmodules');
75
const BUNDLED_NAME = argv._.includes('bundled-name') ? argv._[argv._.indexOf('bundled-name') + 1] : 'index';
86
const MINIFIED_NAME = argv._.includes('minified-name') ? argv._[argv._.indexOf('minified-name') + 1] : 'minified';
97
const PATH = 'packages/core-js-bundle/';
108

9+
const options = ESMODULES ? { targets: { esmodules: true } } : {};
10+
1111
function log(kind, name, code) {
1212
const size = (code.length / 1024).toFixed(2);
1313
echo(green(`${ kind }: ${ cyan(`${ PATH }${ name }.js`) }, size: ${ cyan(`${ size }KB`) }`));
1414
}
1515

16-
async function bundle({ bundled, minified, options = {} }) {
17-
const { script } = await builder({ modules: 'full', ...options });
18-
19-
log('bundling', bundled, script);
20-
await fs.writeFile(`${ PATH }${ bundled }.js`, script);
16+
const { script } = await builder({ modules: 'full', minify: false, ...options });
2117

22-
const { code, map } = await minify(script, {
23-
ecma: 5,
24-
safari10: true,
25-
keep_fnames: true,
26-
compress: {
27-
hoist_funs: true,
28-
hoist_vars: true,
29-
passes: 2,
30-
pure_getters: true,
31-
// document.all detection case
32-
typeofs: false,
33-
unsafe_proto: true,
34-
unsafe_undefined: true,
35-
},
36-
format: {
37-
max_line_len: 32000,
38-
preamble: config.banner,
39-
webkit: true,
40-
// https://v8.dev/blog/preparser#pife
41-
wrap_func_args: false,
42-
},
43-
sourceMap: {
44-
url: `${ minified }.js.map`,
45-
},
46-
});
18+
log('bundling', BUNDLED_NAME, script);
19+
await fs.writeFile(`${ PATH }${ BUNDLED_NAME }.js`, script);
4720

48-
await fs.writeFile(`${ PATH }${ minified }.js`, code);
49-
await fs.writeFile(`${ PATH }${ minified }.js.map`, map);
50-
log('minification', minified, code);
51-
}
21+
const { script: minified } = await builder({ modules: 'full', minify: true, ...options });
5222

53-
await bundle({
54-
bundled: BUNDLED_NAME,
55-
minified: MINIFIED_NAME,
56-
options: ESMODULES ? { targets: { esmodules: true } } : {},
57-
});
23+
await fs.writeFile(`${ PATH }${ MINIFIED_NAME }.js`, minified);
24+
log('bundling minified', MINIFIED_NAME, minified);

scripts/bundle-package/package-lock.json

Lines changed: 0 additions & 130 deletions
This file was deleted.

scripts/bundle-package/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)