Website Structure
This commit is contained in:
parent
62812f2090
commit
71f0676a62
22365 changed files with 4265753 additions and 791 deletions
3
Frontend-Learner/node_modules/@rollup/plugin-terser/src/constants.ts
generated
vendored
Normal file
3
Frontend-Learner/node_modules/@rollup/plugin-terser/src/constants.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export const taskInfo = Symbol('taskInfo');
|
||||
export const freeWorker = Symbol('freeWorker');
|
||||
export const workerPoolWorkerFlag = 'WorkerPoolWorker';
|
||||
8
Frontend-Learner/node_modules/@rollup/plugin-terser/src/index.ts
generated
vendored
Normal file
8
Frontend-Learner/node_modules/@rollup/plugin-terser/src/index.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import { runWorker } from './worker';
|
||||
import terser from './module';
|
||||
|
||||
runWorker();
|
||||
|
||||
export * from './type';
|
||||
|
||||
export default terser;
|
||||
105
Frontend-Learner/node_modules/@rollup/plugin-terser/src/module.ts
generated
vendored
Normal file
105
Frontend-Learner/node_modules/@rollup/plugin-terser/src/module.ts
generated
vendored
Normal file
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
45
Frontend-Learner/node_modules/@rollup/plugin-terser/src/type.ts
generated
vendored
Normal file
45
Frontend-Learner/node_modules/@rollup/plugin-terser/src/type.ts
generated
vendored
Normal file
|
|
@ -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;
|
||||
}
|
||||
128
Frontend-Learner/node_modules/@rollup/plugin-terser/src/worker-pool.ts
generated
vendored
Normal file
128
Frontend-Learner/node_modules/@rollup/plugin-terser/src/worker-pool.ts
generated
vendored
Normal file
|
|
@ -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)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
58
Frontend-Learner/node_modules/@rollup/plugin-terser/src/worker.ts
generated
vendored
Normal file
58
Frontend-Learner/node_modules/@rollup/plugin-terser/src/worker.ts
generated
vendored
Normal file
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue