Skip to content

Commit

Permalink
chore: update build minify script
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jan 15, 2022
1 parent 0ac0e8b commit 851455a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions scripts/build-atomics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function buildAtomicsDebug(opts: BuildOptions): RollupOptions {
}
},
},
...minifyPlugin(true),
...minifyPlugin(opts, true),
],
},
plugins: [
Expand Down Expand Up @@ -91,7 +91,7 @@ function buildAtomicsMin(opts: BuildOptions): RollupOptions {
}
},
},
...minifyPlugin(false),
...minifyPlugin(opts, false),
fileSize(),
],
},
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-main-snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export function buildMainSnippet(opts: BuildOptions): RollupOptions {
file: join(opts.distLibDebugDir, 'partytown.js'),
format: 'es',
exports: 'none',
plugins: [versionPlugin(opts), ...minifyPlugin(true)],
plugins: [versionPlugin(opts), ...minifyPlugin(opts, true)],
};

const partytownMin: OutputOptions = {
file: join(opts.distLibDir, 'partytown.js'),
format: 'es',
exports: 'none',
plugins: [
...minifyPlugin(false),
...minifyPlugin(opts, false),
{
name: 'snippetClosure',
generateBundle(opts, bundle) {
Expand Down
6 changes: 3 additions & 3 deletions scripts/build-service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function buildServiceWorker(opts: BuildOptions): RollupOptions {
file: join(opts.distLibDebugDir, 'partytown-sw.js'),
format: 'es',
exports: 'none',
plugins: [...minifyPlugin(true), versionPlugin(opts)],
plugins: [...minifyPlugin(opts, true), versionPlugin(opts)],
};

const output: OutputOptions[] = [swDebug];
Expand All @@ -29,7 +29,7 @@ export function buildServiceWorker(opts: BuildOptions): RollupOptions {
file: join(opts.distLibDir, 'partytown-sw.js'),
format: 'es',
exports: 'none',
plugins: [...minifyPlugin(false), fileSize()],
plugins: [...minifyPlugin(opts, false), fileSize()],
});
}

Expand Down Expand Up @@ -62,7 +62,7 @@ async function buildSandboxServiceWorker(opts: BuildOptions, msgType: MessageTyp
exports: 'none',
intro: `((window)=>{`,
outro: `})(window);`,
plugins: [...minifyPlugin(debug), jsBannerPlugin(opts)],
plugins: [...minifyPlugin(opts, debug), jsBannerPlugin(opts)],
});

const sandboxJsCode = generated.output[0].code;
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-web-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function buildWebWorker(opts: BuildOptions, msgType: MessageType, d
exports: 'none',
intro: `((self)=>{`,
outro: `})(self);`,
plugins: [...minifyPlugin(debug)],
plugins: [...minifyPlugin(opts, debug)],
};

const generated = await build.generate(output);
Expand Down
22 changes: 15 additions & 7 deletions scripts/minify.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
import type { BuildOptions } from './utils';
import type { Plugin } from 'rollup';
import { terser } from 'rollup-plugin-terser';
import type { MinifyOptions } from 'terser';

export function minifyPlugin(debug: boolean) {
export function minifyPlugin(opts: BuildOptions, debug: boolean) {
if (debug) {
return [terser(minifyOptions(true))];
return [terser(minifyOptions(opts, true))];
}
return [managlePropsPlugin(), terser(minifyOptions(false))];
return [managlePropsPlugin(), terser(minifyOptions(opts, false))];
}

function minifyOptions(debug: boolean): MinifyOptions {
function minifyOptions(opts: BuildOptions, debug: boolean): MinifyOptions {
if (debug) {
const moreCompression = !opts.isDev;
return {
compress: {
global_defs: {
'globalThis.partytownDebug': true,
},
keep_classnames: true,
booleans: false,
collapse_vars: false,
conditionals: moreCompression,
evaluate: moreCompression,
if_return: moreCompression,
inline: false,
join_vars: false,
keep_classnames: true,
loops: false,
sequences: false,
passes: 1,
drop_debugger: false,
drop_debugger: moreCompression,
},
format: {
comments: false,
Expand All @@ -40,7 +47,7 @@ function minifyOptions(debug: boolean): MinifyOptions {
},
keep_classnames: keepConstructorNames(),
ecma: 2018,
passes: 2,
passes: 3,
unsafe_symbols: true,
},
format: {
Expand All @@ -65,6 +72,7 @@ function managlePropsPlugin(): Plugin {
$args$: '',
$assignInstanceId$: '',
$body$: '',
$buf$: '',
$config$: '',
$content$: '',
$currentScriptId$: '',
Expand Down

0 comments on commit 851455a

Please sign in to comment.