File size: 8,200 Bytes
96c8713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'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