Skip to content

Commit

Permalink
Add an ability to disable worker. Just set maxWorkers to 0. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar authored Nov 12, 2024
1 parent 85c18d4 commit 5e31791
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cocos/rollup-plugin-terser",
"version": "0.4.4",
"version": "0.4.5",
"publishConfig": {
"access": "public"
},
Expand Down
22 changes: 14 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { hasOwnProperty, isObject, merge } from 'smob';
import { Worker } from 'jest-worker';
import serializeJavascript from 'serialize-javascript';

import type { Options, TerserWorker } from './type';
import type { Options, TerserWorker, WorkerOutput } from './type';
import { transform } from './worker';

export default function terser(input: Options = {}) {
const { maxWorkers, ...options } = input;

const useWorker = maxWorkers === undefined || maxWorkers > 0;
let worker: TerserWorker | null | undefined;
let numOfChunks = 0;

Expand All @@ -22,7 +23,7 @@ export default function terser(input: Options = {}) {
name: 'terser',

async renderChunk(code: string, chunk: RenderedChunk, outputOptions: NormalizedOutputOptions) {
if (!worker) {
if (!worker && useWorker) {
worker = new Worker(fileURLToPath(currentScriptURL), {
numWorkers: maxWorkers
}) as TerserWorker;
Expand Down Expand Up @@ -55,14 +56,19 @@ export default function terser(input: Options = {}) {
}

try {
let output: WorkerOutput;
const mergedOptions = merge({}, options || {}, defaultOptions);
if (useWorker && worker) {
output = await worker.runWorker(code, serializeJavascript(mergedOptions));
} else {
output = await transform(code, mergedOptions);
}

const {
code: result,
nameCache,
sourceMap
} = await worker.runWorker(
code,
serializeJavascript(merge({}, options || {}, defaultOptions))
);
} = output;

if (options.nameCache && nameCache) {
let vars: Record<string, any> = {
Expand Down Expand Up @@ -106,7 +112,7 @@ export default function terser(input: Options = {}) {
return Promise.reject(e);
} finally {
numOfChunks -= 1;
if (numOfChunks === 0) {
if (numOfChunks === 0 && worker) {
const { forceExited } = await worker.end();
if (forceExited) {
console.error('Workers failed to exit gracefully');
Expand Down
9 changes: 6 additions & 3 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import type { WorkerOutput } from './type';
// eslint-disable-next-line no-eval
const eval2 = eval;

export async function runWorker(code: string, optionsString: string): Promise<WorkerOutput> {
const options = eval2(`(${optionsString})`);

export async function transform(code: string, options: any): Promise<WorkerOutput> {
const result = await minify(code, options);
const output: WorkerOutput = {
code: result.code || code,
Expand All @@ -26,3 +24,8 @@ export async function runWorker(code: string, optionsString: string): Promise<Wo

return output;
}

export async function runWorker(code: string, optionsString: string): Promise<WorkerOutput> {
const options = eval2(`(${optionsString})`);
return transform(code, options);
}
3 changes: 3 additions & 0 deletions test/fixtures/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Base {
_aaa$ = 100;
}
12 changes: 12 additions & 0 deletions test/fixtures/sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Base } from './base';

export class Sub extends Base {
_bbb$ = 'hello';

foo() {
this._bbb$ = 'world';
this._aaa$ = 456;
}
}

export { Base };
10 changes: 10 additions & 0 deletions test/fixtures/sub2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Base } from './base';

export class Sub2 extends Base {
_ccc$ = 'hello2';

foo() {
this._ccc$ = 'world2';
this._aaa$ = 789;
}
}
81 changes: 81 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,87 @@ const { rollup } = require('rollup');

const terser = require('../');

test.serial('inherit', async (t) => {
const nameCache = {};

const bundle = await rollup({
input: ['test/fixtures/sub.js', 'test/fixtures/sub2.js'],
plugins: [terser(
{
compress: {
reduce_funcs: false,
keep_fargs: false,
unsafe_Function: true,
unsafe_math: true,
unsafe_methods: true,
passes: 2,
},
mangle: {
properties: {
regex: /^[a-zA-Z_][a-zA-Z0-9_]{3,}\$$/,
}
},
keep_fnames: false,
output: {
beautify: true,
},
nameCache,
maxWorkers: 0,
}
)]
});
const result = await bundle.generate({ format: 'system' });
t.is(result.output.length, 3);
const [output1, output2, output3] = result.output;
t.is(output1.code, `System.register([ "./base-7xu-expY.js" ], (function(e) {
"use strict";
var s;
return {
setters: [ function(t) {
s = t.B, e("Base", t.B);
} ],
execute: function() {
e("Sub", class extends s {
t="hello";
foo() {
this.t = "world", this.o = 456;
}
});
}
};
}));
`);
t.is(output2.code, `System.register([ "./base-7xu-expY.js" ], (function(t) {
"use strict";
var e;
return {
setters: [ function(t) {
e = t.B;
} ],
execute: function() {
t("Sub2", class extends e {
u="hello2";
foo() {
this.u = "world2", this.o = 789;
}
});
}
};
}));
`);
t.is(output3.code, `System.register([], (function(t) {
"use strict";
return {
execute: function() {
t("B", class {
o=100;
});
}
};
}));
`)
});

test.serial('minify', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/unminified.js',
Expand Down

0 comments on commit 5e31791

Please sign in to comment.