chore: initial clean commit without large binaries

This commit is contained in:
2026-05-06 17:24:09 +02:00
parent 2f2cbed5bd
commit 8a4ed88b93
16624 changed files with 2650353 additions and 8 deletions
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2019 RollupJS Plugin Contributors (https://github.com/rollup/plugins/graphs/contributors)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,80 @@
[npm]: https://img.shields.io/npm/v/@rollup/plugin-terser
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-terser
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-terser
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-terser
[![npm][npm]][npm-url]
[![size][size]][size-url]
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
# @rollup/plugin-terser
🍣 A Rollup plugin to generate a minified bundle with terser.
## Requirements
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v2.0+.
## Install
Using npm:
```console
npm install @rollup/plugin-terser --save-dev
```
## Usage
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
```typescript
import terser from '@rollup/plugin-terser';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [terser()]
};
```
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
## Options
The plugin accepts a terser [Options](https://github.com/terser/terser#minify-options) object as input parameter,
to modify the default behaviour.
In addition to the `terser` options, it is also possible to provide the following options:
### `maxWorkers`
Type: `Number`<br>
Default: `undefined`
Instructs the plugin to use a specific amount of cpu threads.
```typescript
import terser from '@rollup/plugin-terser';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [
terser({
maxWorkers: 4
})
]
};
```
## Meta
[CONTRIBUTING](/.github/CONTRIBUTING.md)
[LICENSE (MIT)](/LICENSE)
@@ -0,0 +1,232 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var worker_threads = require('worker_threads');
var smob = require('smob');
var terser$1 = require('terser');
var url = require('url');
var async_hooks = require('async_hooks');
var os = require('os');
var events = require('events');
var serializeJavascript = require('serialize-javascript');
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
const taskInfo = Symbol('taskInfo');
const freeWorker = Symbol('freeWorker');
const workerPoolWorkerFlag = 'WorkerPoolWorker';
/**
* Duck typing worker context.
*
* @param input
*/
function isWorkerContextSerialized(input) {
return (smob.isObject(input) &&
smob.hasOwnProperty(input, 'code') &&
typeof input.code === 'string' &&
smob.hasOwnProperty(input, 'options') &&
typeof input.options === 'string');
}
function runWorker() {
if (worker_threads.isMainThread || !worker_threads.parentPort || worker_threads.workerData !== workerPoolWorkerFlag) {
return;
}
// eslint-disable-next-line no-eval
const eval2 = eval;
worker_threads.parentPort.on('message', async (data) => {
if (!isWorkerContextSerialized(data)) {
return;
}
const options = eval2(`(${data.options})`);
const result = await terser$1.minify(data.code, options);
const output = {
code: result.code || data.code,
nameCache: options.nameCache
};
if (typeof result.map === 'string') {
output.sourceMap = JSON.parse(result.map);
}
if (smob.isObject(result.map)) {
output.sourceMap = result.map;
}
worker_threads.parentPort === null || worker_threads.parentPort === void 0 ? void 0 : worker_threads.parentPort.postMessage(output);
});
}
class WorkerPoolTaskInfo extends async_hooks.AsyncResource {
constructor(callback) {
super('WorkerPoolTaskInfo');
this.callback = callback;
}
done(err, result) {
this.runInAsyncScope(this.callback, null, err, result);
this.emitDestroy();
}
}
class WorkerPool extends events.EventEmitter {
constructor(options) {
super();
this.tasks = [];
this.workers = [];
this.freeWorkers = [];
this.maxInstances = options.maxWorkers || os.cpus().length;
this.filePath = options.filePath;
this.on(freeWorker, () => {
if (this.tasks.length > 0) {
const { context, cb } = this.tasks.shift();
this.runTask(context, cb);
}
});
}
get numWorkers() {
return this.workers.length;
}
addAsync(context) {
return new Promise((resolve, reject) => {
this.runTask(context, (err, output) => {
if (err) {
reject(err);
return;
}
if (!output) {
reject(new Error('The output is empty'));
return;
}
resolve(output);
});
});
}
close() {
for (let i = 0; i < this.workers.length; i++) {
const worker = this.workers[i];
worker.terminate();
}
}
addNewWorker() {
const worker = new worker_threads.Worker(this.filePath, {
workerData: workerPoolWorkerFlag
});
worker.on('message', (result) => {
var _a;
(_a = worker[taskInfo]) === null || _a === void 0 ? void 0 : _a.done(null, result);
worker[taskInfo] = null;
this.freeWorkers.push(worker);
this.emit(freeWorker);
});
worker.on('error', (err) => {
if (worker[taskInfo]) {
worker[taskInfo].done(err, null);
}
else {
this.emit('error', err);
}
this.workers.splice(this.workers.indexOf(worker), 1);
this.addNewWorker();
});
this.workers.push(worker);
this.freeWorkers.push(worker);
this.emit(freeWorker);
}
runTask(context, cb) {
if (this.freeWorkers.length === 0) {
this.tasks.push({ context, cb });
if (this.numWorkers < this.maxInstances) {
this.addNewWorker();
}
return;
}
const worker = this.freeWorkers.pop();
if (worker) {
worker[taskInfo] = new WorkerPoolTaskInfo(cb);
worker.postMessage({
code: context.code,
options: serializeJavascript(context.options)
});
}
}
}
function terser(input = {}) {
const { maxWorkers, ...options } = input;
let workerPool;
let numOfChunks = 0;
let numOfWorkersUsed = 0;
return {
name: 'terser',
async renderChunk(code, chunk, outputOptions) {
if (!workerPool) {
workerPool = new WorkerPool({
filePath: url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.js', document.baseURI).href))),
maxWorkers
});
}
numOfChunks += 1;
const defaultOptions = {
sourceMap: outputOptions.sourcemap === true || typeof outputOptions.sourcemap === 'string'
};
if (outputOptions.format === 'es') {
defaultOptions.module = true;
}
if (outputOptions.format === 'cjs') {
defaultOptions.toplevel = true;
}
try {
const { code: result, nameCache, sourceMap } = await workerPool.addAsync({
code,
options: smob.merge({}, options || {}, defaultOptions)
});
if (options.nameCache && nameCache) {
let vars = {
props: {}
};
if (smob.hasOwnProperty(options.nameCache, 'vars') && smob.isObject(options.nameCache.vars)) {
vars = smob.merge({}, options.nameCache.vars || {}, vars);
}
if (smob.hasOwnProperty(nameCache, 'vars') && smob.isObject(nameCache.vars)) {
vars = smob.merge({}, nameCache.vars, vars);
}
// eslint-disable-next-line no-param-reassign
options.nameCache.vars = vars;
let props = {};
if (smob.hasOwnProperty(options.nameCache, 'props') && smob.isObject(options.nameCache.props)) {
// eslint-disable-next-line prefer-destructuring
props = options.nameCache.props;
}
if (smob.hasOwnProperty(nameCache, 'props') && smob.isObject(nameCache.props)) {
props = smob.merge({}, nameCache.props, props);
}
// eslint-disable-next-line no-param-reassign
options.nameCache.props = props;
}
if ((!!defaultOptions.sourceMap || !!options.sourceMap) && smob.isObject(sourceMap)) {
return {
code: result,
map: sourceMap
};
}
return result;
}
catch (e) {
return Promise.reject(e);
}
finally {
numOfChunks -= 1;
if (numOfChunks === 0) {
numOfWorkersUsed = workerPool.numWorkers;
workerPool.close();
workerPool = null;
}
}
},
get numOfWorkersUsed() {
return numOfWorkersUsed;
}
};
}
runWorker();
exports.default = terser;
module.exports = Object.assign(exports.default, exports);
//# sourceMappingURL=index.js.map
@@ -0,0 +1,226 @@
import { isMainThread, parentPort, workerData, Worker } from 'worker_threads';
import { isObject, hasOwnProperty, merge } from 'smob';
import { minify } from 'terser';
import { fileURLToPath } from 'url';
import { AsyncResource } from 'async_hooks';
import { cpus } from 'os';
import { EventEmitter } from 'events';
import serializeJavascript from 'serialize-javascript';
const taskInfo = Symbol('taskInfo');
const freeWorker = Symbol('freeWorker');
const workerPoolWorkerFlag = 'WorkerPoolWorker';
/**
* Duck typing worker context.
*
* @param input
*/
function isWorkerContextSerialized(input) {
return (isObject(input) &&
hasOwnProperty(input, 'code') &&
typeof input.code === 'string' &&
hasOwnProperty(input, 'options') &&
typeof input.options === 'string');
}
function runWorker() {
if (isMainThread || !parentPort || workerData !== workerPoolWorkerFlag) {
return;
}
// eslint-disable-next-line no-eval
const eval2 = eval;
parentPort.on('message', async (data) => {
if (!isWorkerContextSerialized(data)) {
return;
}
const options = eval2(`(${data.options})`);
const result = await minify(data.code, options);
const output = {
code: result.code || data.code,
nameCache: options.nameCache
};
if (typeof result.map === 'string') {
output.sourceMap = JSON.parse(result.map);
}
if (isObject(result.map)) {
output.sourceMap = result.map;
}
parentPort === null || parentPort === void 0 ? void 0 : parentPort.postMessage(output);
});
}
class WorkerPoolTaskInfo extends AsyncResource {
constructor(callback) {
super('WorkerPoolTaskInfo');
this.callback = callback;
}
done(err, result) {
this.runInAsyncScope(this.callback, null, err, result);
this.emitDestroy();
}
}
class WorkerPool extends EventEmitter {
constructor(options) {
super();
this.tasks = [];
this.workers = [];
this.freeWorkers = [];
this.maxInstances = options.maxWorkers || cpus().length;
this.filePath = options.filePath;
this.on(freeWorker, () => {
if (this.tasks.length > 0) {
const { context, cb } = this.tasks.shift();
this.runTask(context, cb);
}
});
}
get numWorkers() {
return this.workers.length;
}
addAsync(context) {
return new Promise((resolve, reject) => {
this.runTask(context, (err, output) => {
if (err) {
reject(err);
return;
}
if (!output) {
reject(new Error('The output is empty'));
return;
}
resolve(output);
});
});
}
close() {
for (let i = 0; i < this.workers.length; i++) {
const worker = this.workers[i];
worker.terminate();
}
}
addNewWorker() {
const worker = new Worker(this.filePath, {
workerData: workerPoolWorkerFlag
});
worker.on('message', (result) => {
var _a;
(_a = worker[taskInfo]) === null || _a === void 0 ? void 0 : _a.done(null, result);
worker[taskInfo] = null;
this.freeWorkers.push(worker);
this.emit(freeWorker);
});
worker.on('error', (err) => {
if (worker[taskInfo]) {
worker[taskInfo].done(err, null);
}
else {
this.emit('error', err);
}
this.workers.splice(this.workers.indexOf(worker), 1);
this.addNewWorker();
});
this.workers.push(worker);
this.freeWorkers.push(worker);
this.emit(freeWorker);
}
runTask(context, cb) {
if (this.freeWorkers.length === 0) {
this.tasks.push({ context, cb });
if (this.numWorkers < this.maxInstances) {
this.addNewWorker();
}
return;
}
const worker = this.freeWorkers.pop();
if (worker) {
worker[taskInfo] = new WorkerPoolTaskInfo(cb);
worker.postMessage({
code: context.code,
options: serializeJavascript(context.options)
});
}
}
}
function terser(input = {}) {
const { maxWorkers, ...options } = input;
let workerPool;
let numOfChunks = 0;
let numOfWorkersUsed = 0;
return {
name: 'terser',
async renderChunk(code, chunk, outputOptions) {
if (!workerPool) {
workerPool = new WorkerPool({
filePath: fileURLToPath(import.meta.url),
maxWorkers
});
}
numOfChunks += 1;
const defaultOptions = {
sourceMap: outputOptions.sourcemap === true || typeof outputOptions.sourcemap === 'string'
};
if (outputOptions.format === 'es') {
defaultOptions.module = true;
}
if (outputOptions.format === 'cjs') {
defaultOptions.toplevel = true;
}
try {
const { code: result, nameCache, sourceMap } = await workerPool.addAsync({
code,
options: merge({}, options || {}, defaultOptions)
});
if (options.nameCache && nameCache) {
let vars = {
props: {}
};
if (hasOwnProperty(options.nameCache, 'vars') && isObject(options.nameCache.vars)) {
vars = merge({}, options.nameCache.vars || {}, vars);
}
if (hasOwnProperty(nameCache, 'vars') && isObject(nameCache.vars)) {
vars = merge({}, nameCache.vars, vars);
}
// eslint-disable-next-line no-param-reassign
options.nameCache.vars = vars;
let props = {};
if (hasOwnProperty(options.nameCache, 'props') && isObject(options.nameCache.props)) {
// eslint-disable-next-line prefer-destructuring
props = options.nameCache.props;
}
if (hasOwnProperty(nameCache, 'props') && isObject(nameCache.props)) {
props = merge({}, nameCache.props, props);
}
// eslint-disable-next-line no-param-reassign
options.nameCache.props = props;
}
if ((!!defaultOptions.sourceMap || !!options.sourceMap) && isObject(sourceMap)) {
return {
code: result,
map: sourceMap
};
}
return result;
}
catch (e) {
return Promise.reject(e);
}
finally {
numOfChunks -= 1;
if (numOfChunks === 0) {
numOfWorkersUsed = workerPool.numWorkers;
workerPool.close();
workerPool = null;
}
}
},
get numOfWorkersUsed() {
return numOfWorkersUsed;
}
};
}
runWorker();
export { terser as default };
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"type":"module"}
@@ -0,0 +1,21 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -z "$NODE_PATH" ]; then
export NODE_PATH="/home/computer/projects/fitness-app/node_modules/.pnpm/rollup@4.60.3/node_modules/rollup/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/rollup@4.60.3/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/node_modules"
else
export NODE_PATH="/home/computer/projects/fitness-app/node_modules/.pnpm/rollup@4.60.3/node_modules/rollup/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/rollup@4.60.3/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/node_modules:$NODE_PATH"
fi
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../../../../rollup/dist/bin/rollup" "$@"
else
exec node "$basedir/../../../../rollup/dist/bin/rollup" "$@"
fi
@@ -0,0 +1,21 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -z "$NODE_PATH" ]; then
export NODE_PATH="/home/computer/projects/fitness-app/node_modules/.pnpm/terser@5.46.2/node_modules/terser/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/terser@5.46.2/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/node_modules"
else
export NODE_PATH="/home/computer/projects/fitness-app/node_modules/.pnpm/terser@5.46.2/node_modules/terser/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/terser@5.46.2/node_modules:/home/computer/projects/fitness-app/node_modules/.pnpm/node_modules:$NODE_PATH"
fi
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../../../../terser/bin/terser" "$@"
else
exec node "$basedir/../../../../terser/bin/terser" "$@"
fi
@@ -0,0 +1,73 @@
{
"name": "@rollup/plugin-terser",
"version": "1.0.0",
"publishConfig": {
"access": "public"
},
"description": "Generate minified bundle",
"license": "MIT",
"repository": {
"url": "rollup/plugins",
"directory": "packages/terser"
},
"author": "Peter Placzek <peter.placzek1996@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/terser#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
"exports": {
"types": "./types/index.d.ts",
"import": "./dist/es/index.js",
"default": "./dist/cjs/index.js"
},
"engines": {
"node": ">=20.0.0"
},
"files": [
"dist",
"!dist/**/*.map",
"src",
"types",
"README.md"
],
"keywords": [
"rollup",
"plugin",
"terser",
"minify",
"npm",
"modules"
],
"peerDependencies": {
"rollup": "^2.0.0||^3.0.0||^4.0.0"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"serialize-javascript": "^7.0.3",
"smob": "^1.0.0",
"terser": "^5.17.4"
},
"devDependencies": {
"@types/serialize-javascript": "^5.0.4",
"rollup": "^4.0.0-24",
"typescript": "^4.8.3"
},
"types": "./types/index.d.ts",
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:lint": "pnpm build && pnpm lint",
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm test -- --verbose && pnpm test:ts",
"prebuild": "del-cli dist",
"prerelease": "pnpm build",
"pretest": "pnpm build",
"release": "pnpm --workspace-root package:release $(pwd)",
"test": "ava",
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
}
}
@@ -0,0 +1,3 @@
export const taskInfo = Symbol('taskInfo');
export const freeWorker = Symbol('freeWorker');
export const workerPoolWorkerFlag = 'WorkerPoolWorker';
@@ -0,0 +1,8 @@
import { runWorker } from './worker';
import terser from './module';
runWorker();
export * from './type';
export default terser;
@@ -0,0 +1,105 @@
import { fileURLToPath } from 'url';
import type { NormalizedOutputOptions, RenderedChunk } from 'rollup';
import { hasOwnProperty, isObject, merge } from 'smob';
import type { Options } from './type';
import { WorkerPool } from './worker-pool';
export default function terser(input: Options = {}) {
const { maxWorkers, ...options } = input;
let workerPool: WorkerPool | null | undefined;
let numOfChunks = 0;
let numOfWorkersUsed = 0;
return {
name: 'terser',
async renderChunk(code: string, chunk: RenderedChunk, outputOptions: NormalizedOutputOptions) {
if (!workerPool) {
workerPool = new WorkerPool({
filePath: fileURLToPath(import.meta.url),
maxWorkers
});
}
numOfChunks += 1;
const defaultOptions: Options = {
sourceMap: outputOptions.sourcemap === true || typeof outputOptions.sourcemap === 'string'
};
if (outputOptions.format === 'es') {
defaultOptions.module = true;
}
if (outputOptions.format === 'cjs') {
defaultOptions.toplevel = true;
}
try {
const {
code: result,
nameCache,
sourceMap
} = await workerPool.addAsync({
code,
options: merge({}, options || {}, defaultOptions)
});
if (options.nameCache && nameCache) {
let vars: Record<string, any> = {
props: {}
};
if (hasOwnProperty(options.nameCache, 'vars') && isObject(options.nameCache.vars)) {
vars = merge({}, options.nameCache.vars || {}, vars);
}
if (hasOwnProperty(nameCache, 'vars') && isObject(nameCache.vars)) {
vars = merge({}, nameCache.vars, vars);
}
// eslint-disable-next-line no-param-reassign
options.nameCache.vars = vars;
let props: Record<string, any> = {};
if (hasOwnProperty(options.nameCache, 'props') && isObject(options.nameCache.props)) {
// eslint-disable-next-line prefer-destructuring
props = options.nameCache.props;
}
if (hasOwnProperty(nameCache, 'props') && isObject(nameCache.props)) {
props = merge({}, nameCache.props, props);
}
// eslint-disable-next-line no-param-reassign
options.nameCache.props = props;
}
if ((!!defaultOptions.sourceMap || !!options.sourceMap) && isObject(sourceMap)) {
return {
code: result,
map: sourceMap
};
}
return result;
} catch (e) {
return Promise.reject(e);
} finally {
numOfChunks -= 1;
if (numOfChunks === 0) {
numOfWorkersUsed = workerPool.numWorkers;
workerPool.close();
workerPool = null;
}
}
},
get numOfWorkersUsed() {
return numOfWorkersUsed;
}
};
}
@@ -0,0 +1,45 @@
import type { AsyncResource } from 'async_hooks';
import type { Worker } from 'worker_threads';
import type { MinifyOptions } from 'terser';
import type { taskInfo } from './constants';
export interface Options extends MinifyOptions {
nameCache?: Record<string, any>;
maxWorkers?: number;
}
export interface WorkerContext {
code: string;
options: Options;
}
export type WorkerCallback = (err: Error | null, output?: WorkerOutput) => void;
interface WorkerPoolTaskInfo extends AsyncResource {
done(err: Error | null, result: any): void;
}
export type WorkerWithTaskInfo = Worker & { [taskInfo]?: WorkerPoolTaskInfo | null };
export interface WorkerContextSerialized {
code: string;
options: string;
}
export interface WorkerOutput {
code: string;
nameCache?: Options['nameCache'];
sourceMap?: Record<string, any>;
}
export interface WorkerPoolOptions {
filePath: string;
maxWorkers?: number;
}
export interface WorkerPoolTask {
context: WorkerContext;
cb: WorkerCallback;
}
@@ -0,0 +1,128 @@
import { AsyncResource } from 'async_hooks';
import { Worker } from 'worker_threads';
import { cpus } from 'os';
import { EventEmitter } from 'events';
import serializeJavascript from 'serialize-javascript';
import { freeWorker, taskInfo, workerPoolWorkerFlag } from './constants';
import type {
WorkerCallback,
WorkerContext,
WorkerOutput,
WorkerPoolOptions,
WorkerPoolTask,
WorkerWithTaskInfo
} from './type';
class WorkerPoolTaskInfo extends AsyncResource {
constructor(private callback: WorkerCallback) {
super('WorkerPoolTaskInfo');
}
done(err: Error | null, result: any) {
this.runInAsyncScope(this.callback, null, err, result);
this.emitDestroy();
}
}
export class WorkerPool extends EventEmitter {
protected maxInstances: number;
protected filePath: string;
protected tasks: WorkerPoolTask[] = [];
protected workers: WorkerWithTaskInfo[] = [];
protected freeWorkers: WorkerWithTaskInfo[] = [];
constructor(options: WorkerPoolOptions) {
super();
this.maxInstances = options.maxWorkers || cpus().length;
this.filePath = options.filePath;
this.on(freeWorker, () => {
if (this.tasks.length > 0) {
const { context, cb } = this.tasks.shift()!;
this.runTask(context, cb);
}
});
}
get numWorkers(): number {
return this.workers.length;
}
addAsync(context: WorkerContext): Promise<WorkerOutput> {
return new Promise((resolve, reject) => {
this.runTask(context, (err, output) => {
if (err) {
reject(err);
return;
}
if (!output) {
reject(new Error('The output is empty'));
return;
}
resolve(output);
});
});
}
close() {
for (let i = 0; i < this.workers.length; i++) {
const worker = this.workers[i];
worker.terminate();
}
}
private addNewWorker() {
const worker: WorkerWithTaskInfo = new Worker(this.filePath, {
workerData: workerPoolWorkerFlag
});
worker.on('message', (result) => {
worker[taskInfo]?.done(null, result);
worker[taskInfo] = null;
this.freeWorkers.push(worker);
this.emit(freeWorker);
});
worker.on('error', (err) => {
if (worker[taskInfo]) {
worker[taskInfo].done(err, null);
} else {
this.emit('error', err);
}
this.workers.splice(this.workers.indexOf(worker), 1);
this.addNewWorker();
});
this.workers.push(worker);
this.freeWorkers.push(worker);
this.emit(freeWorker);
}
private runTask(context: WorkerContext, cb: WorkerCallback) {
if (this.freeWorkers.length === 0) {
this.tasks.push({ context, cb });
if (this.numWorkers < this.maxInstances) {
this.addNewWorker();
}
return;
}
const worker = this.freeWorkers.pop();
if (worker) {
worker[taskInfo] = new WorkerPoolTaskInfo(cb);
worker.postMessage({
code: context.code,
options: serializeJavascript(context.options)
});
}
}
}
@@ -0,0 +1,58 @@
import { isMainThread, parentPort, workerData } from 'worker_threads';
import { hasOwnProperty, isObject } from 'smob';
import { minify } from 'terser';
import { workerPoolWorkerFlag } from './constants';
import type { WorkerContextSerialized, WorkerOutput } from './type';
/**
* Duck typing worker context.
*
* @param input
*/
function isWorkerContextSerialized(input: unknown): input is WorkerContextSerialized {
return (
isObject(input) &&
hasOwnProperty(input, 'code') &&
typeof input.code === 'string' &&
hasOwnProperty(input, 'options') &&
typeof input.options === 'string'
);
}
export function runWorker() {
if (isMainThread || !parentPort || workerData !== workerPoolWorkerFlag) {
return;
}
// eslint-disable-next-line no-eval
const eval2 = eval;
parentPort.on('message', async (data: WorkerContextSerialized) => {
if (!isWorkerContextSerialized(data)) {
return;
}
const options = eval2(`(${data.options})`);
const result = await minify(data.code, options);
const output: WorkerOutput = {
code: result.code || data.code,
nameCache: options.nameCache
};
if (typeof result.map === 'string') {
output.sourceMap = JSON.parse(result.map);
}
if (isObject(result.map)) {
output.sourceMap = result.map;
}
parentPort?.postMessage(output);
});
}
@@ -0,0 +1,15 @@
import type { Plugin } from 'rollup';
import type { MinifyOptions } from 'terser';
export interface Options extends MinifyOptions {
nameCache?: Record<string, any>;
maxWorkers?: number;
}
/**
* A Rollup plugin to generate a minified output bundle.
*
* @param options - Plugin options.
* @returns Plugin instance.
*/
export default function terser(options?: Options): Plugin;
+1
View File
@@ -0,0 +1 @@
../../rollup@4.60.3/node_modules/rollup
@@ -0,0 +1 @@
../../serialize-javascript@7.0.5/node_modules/serialize-javascript
+1
View File
@@ -0,0 +1 @@
../../smob@1.6.1/node_modules/smob
+1
View File
@@ -0,0 +1 @@
../../terser@5.46.2/node_modules/terser