@rushstack/module-minifier 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +12 -0
  3. package/dist/module-minifier.d.ts +250 -0
  4. package/dist/tsdoc-metadata.json +11 -0
  5. package/lib/LocalMinifier.d.ts +27 -0
  6. package/lib/LocalMinifier.d.ts.map +1 -0
  7. package/lib/LocalMinifier.js +69 -0
  8. package/lib/LocalMinifier.js.map +1 -0
  9. package/lib/MessagePortMinifier.d.ts +20 -0
  10. package/lib/MessagePortMinifier.d.ts.map +1 -0
  11. package/lib/MessagePortMinifier.js +56 -0
  12. package/lib/MessagePortMinifier.js.map +1 -0
  13. package/lib/MinifiedIdentifier.d.ts +20 -0
  14. package/lib/MinifiedIdentifier.d.ts.map +1 -0
  15. package/lib/MinifiedIdentifier.js +148 -0
  16. package/lib/MinifiedIdentifier.js.map +1 -0
  17. package/lib/MinifierWorker.d.ts +2 -0
  18. package/lib/MinifierWorker.d.ts.map +1 -0
  19. package/lib/MinifierWorker.js +17 -0
  20. package/lib/MinifierWorker.js.map +1 -0
  21. package/lib/MinifySingleFile.d.ts +13 -0
  22. package/lib/MinifySingleFile.d.ts.map +1 -0
  23. package/lib/MinifySingleFile.js +59 -0
  24. package/lib/MinifySingleFile.js.map +1 -0
  25. package/lib/NoopMinifier.d.ts +15 -0
  26. package/lib/NoopMinifier.d.ts.map +1 -0
  27. package/lib/NoopMinifier.js +46 -0
  28. package/lib/NoopMinifier.js.map +1 -0
  29. package/lib/WorkerPoolMinifier.d.ts +47 -0
  30. package/lib/WorkerPoolMinifier.d.ts.map +1 -0
  31. package/lib/WorkerPoolMinifier.js +119 -0
  32. package/lib/WorkerPoolMinifier.js.map +1 -0
  33. package/lib/constants.d.ts +13 -0
  34. package/lib/constants.d.ts.map +1 -0
  35. package/lib/constants.js +18 -0
  36. package/lib/constants.js.map +1 -0
  37. package/lib/index.d.ts +11 -0
  38. package/lib/index.d.ts.map +1 -0
  39. package/lib/index.js +18 -0
  40. package/lib/index.js.map +1 -0
  41. package/lib/types.d.ts +117 -0
  42. package/lib/types.d.ts.map +1 -0
  43. package/lib/types.js +5 -0
  44. package/lib/types.js.map +1 -0
  45. package/package.json +33 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MinifiedIdentifier.js","sourceRoot":"","sources":["../src/MinifiedIdentifier.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,4EAA4E;AAC5E,2CAAoF;AAEpF,4IAA4I;AAC5I,MAAM,iBAAiB,GAAa;IAClC,UAAU;IACV,WAAW;IACX,SAAS;IACT,OAAO;IACP,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;IACN,OAAO;IACP,OAAO;IACP,UAAU;IACV,UAAU;IACV,SAAS;IACT,QAAQ;IACR,IAAI;IACJ,QAAQ;IACR,MAAM;IACN,MAAM;IACN,QAAQ;IACR,SAAS;IACT,OAAO;IACP,OAAO;IACP,SAAS;IACT,OAAO;IACP,KAAK;IACL,UAAU;IACV,KAAK;IACL,MAAM;IACN,IAAI;IACJ,YAAY;IACZ,QAAQ;IACR,IAAI;IACJ,YAAY;IACZ,KAAK;IACL,WAAW;IACX,KAAK;IACL,MAAM;IACN,QAAQ;IACR,KAAK;IACL,MAAM;IACN,SAAS;IACT,SAAS;IACT,WAAW;IACX,QAAQ;IACR,QAAQ;IACR,KAAK;IACL,OAAO;IACP,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,cAAc;IACd,MAAM;IACN,OAAO;IACP,QAAQ;IACR,WAAW;IACX,MAAM;IACN,KAAK;IACL,QAAQ;IACR,KAAK;IACL,MAAM;IACN,UAAU;IACV,OAAO;IACP,MAAM;IACN,OAAO;CACR,CAAC;AAEF;;;GAGG;AACH,SAAgB,qBAAqB,CAAC,OAAe;IACnD,IAAI,GAAG,GAAW,qCAAyB,CAAC,OAAO,GAAG,EAAE,CAAC,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AS,EAAE;QACxB,OAAO,GAAG,CAAC;KACZ;IAED,OAAO,IAAI,EAAE,CAAC;IACd,OAAO,IAAI,gBAAgB,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAE,CAAC;IAC3D,OAAO,OAAO,CAAC;AACjB,CAAC;AAnBD,4EAmBC;AAED;;GAEG;AACH,MAAM,iBAAiB,GAAa,CAAC,GAAa,EAAE;IAClD,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,KAAK,MAAM,OAAO,IAAI,iBAAiB,EAAE;QACvC,MAAM,OAAO,GAAW,gCAAgC,CAAC,OAAO,CAAC,CAAC;QAClE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;YACnB,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;SACxB;KACF;IACD,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAS,EAAE,CAAS,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACxD,CAAC,CAAC,EAAE,CAAC;AAEL;;;;;;GAMG;AACH,SAAgB,aAAa,CAAC,OAAe;IAC3C,sCAAsC;IACtC,KACE,IAAI,CAAC,GAAW,CAAC,EAAE,GAAG,GAAW,iBAAiB,CAAC,MAAM,EACzD,CAAC,GAAG,GAAG,IAAI,OAAO,IAAI,iBAAiB,CAAC,CAAC,CAAC,EAC1C,CAAC,EAAE,EACH;QACA,EAAE,OAAO,CAAC;KACX;IAED,OAAO,qBAAqB,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC;AAXD,sCAWC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n// TODO: Allow dynamic override of these values in the input to the minifier\nimport { IDENTIFIER_LEADING_DIGITS, IDENTIFIER_TRAILING_DIGITS } from './constants';\n\n// Set of ECMAScript reserved keywords (past and present): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar\nconst RESERVED_KEYWORDS: string[] = [\n 'abstract',\n 'arguments',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'function',\n 'get',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'set',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield'\n];\n\n/**\n * Gets a base54 string suitable for use as a JavaScript identifier, not accounting for reserved keywords\n * @param ordinal - The number to convert to a base54 identifier\n */\nexport function getIdentifierInternal(ordinal: number): string {\n let ret: string = IDENTIFIER_LEADING_DIGITS[ordinal % 54];\n\n ordinal = (ordinal / 54) | 0; // eslint-disable-line no-bitwise\n while (ordinal > 0) {\n --ordinal;\n ret += IDENTIFIER_TRAILING_DIGITS[ordinal & 0x3f]; // eslint-disable-line no-bitwise\n ordinal >>>= 6; // eslint-disable-line no-bitwise\n }\n\n return ret;\n}\n\nconst leadingCharIndex: Map<number, number> = new Map();\nfor (let i: number = 0; i < 64; i++) {\n leadingCharIndex.set(IDENTIFIER_LEADING_DIGITS.charCodeAt(i), i);\n}\n\nconst trailingCharIndex: Map<number, number> = new Map();\nfor (let i: number = 0; i < 64; i++) {\n trailingCharIndex.set(IDENTIFIER_TRAILING_DIGITS.charCodeAt(i), i);\n}\n\n/**\n * Converts an identifier into the ordinal that would produce it, not accounting for reserved keywords\n * Returns NaN if the result would exceed 31 bits\n * @param identifier - The identifier to convert to a numeric value\n */\nexport function getOrdinalFromIdentifierInternal(identifier: string): number {\n let ordinal: number = 0;\n\n for (let i: number = identifier.length - 1; i > 0; i--) {\n if (ordinal >= 0x2000000) {\n return NaN;\n }\n\n ordinal <<= 6; // eslint-disable-line no-bitwise\n ordinal += trailingCharIndex.get(identifier.charCodeAt(i))! + 1;\n }\n\n if (ordinal >= 0x2000000) {\n return NaN;\n }\n\n ordinal *= 54;\n ordinal += leadingCharIndex.get(identifier.charCodeAt(0))!;\n return ordinal;\n}\n\n/**\n * getIdentifier(n) would otherwise return each of these specified ECMAScript reserved keywords, which are not legal identifiers.\n */\nconst RESERVED_ORDINALS: number[] = ((): number[] => {\n const reserved: number[] = [];\n for (const keyword of RESERVED_KEYWORDS) {\n const ordinal: number = getOrdinalFromIdentifierInternal(keyword);\n if (!isNaN(ordinal)) {\n reserved.push(ordinal);\n }\n }\n return reserved.sort((x: number, y: number) => x - y);\n})();\n\n/**\n * Gets a base54 string suitable for use as a JavaScript identifier, omitting those that are valid ECMAScript keywords\n *\n * @param ordinal - The number to convert to a base54 identifier\n *\n * @public\n */\nexport function getIdentifier(ordinal: number): string {\n // Need to skip over reserved keywords\n for (\n let i: number = 0, len: number = RESERVED_ORDINALS.length;\n i < len && ordinal >= RESERVED_ORDINALS[i];\n i++\n ) {\n ++ordinal;\n }\n\n return getIdentifierInternal(ordinal);\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MinifierWorker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MinifierWorker.d.ts","sourceRoot":"","sources":["../src/MinifierWorker.ts"],"names":[],"mappings":""}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const worker_threads_1 = require("worker_threads");
6
+ const MinifySingleFile_1 = require("./MinifySingleFile");
7
+ const terserOptions = worker_threads_1.workerData;
8
+ // Set to non-zero to help debug unexpected graceful exit
9
+ process.exitCode = 2;
10
+ worker_threads_1.parentPort.on('message', async (message) => {
11
+ if (!message) {
12
+ process.exit(0);
13
+ }
14
+ const result = await (0, MinifySingleFile_1.minifySingleFileAsync)(message, terserOptions);
15
+ worker_threads_1.parentPort.postMessage(result);
16
+ });
17
+ //# sourceMappingURL=MinifierWorker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MinifierWorker.js","sourceRoot":"","sources":["../src/MinifierWorker.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;AAG3D,mDAAwD;AAExD,yDAA2D;AAG3D,MAAM,aAAa,GAAkB,2BAAU,CAAC;AAEhD,yDAAyD;AACzD,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AAErB,2BAAW,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,EAAE,OAAmC,EAAE,EAAE;IACtE,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;IAED,MAAM,MAAM,GAA8B,MAAM,IAAA,wCAAqB,EAAC,OAAO,EAAE,aAAa,CAAC,CAAC;IAE9F,2BAAW,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AAClC,CAAC,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { MinifyOptions } from 'terser';\nimport { parentPort, workerData } from 'worker_threads';\n\nimport { minifySingleFileAsync } from './MinifySingleFile';\nimport type { IModuleMinificationRequest, IModuleMinificationResult } from './types';\n\nconst terserOptions: MinifyOptions = workerData;\n\n// Set to non-zero to help debug unexpected graceful exit\nprocess.exitCode = 2;\n\nparentPort!.on('message', async (message: IModuleMinificationRequest) => {\n if (!message) {\n process.exit(0);\n }\n\n const result: IModuleMinificationResult = await minifySingleFileAsync(message, terserOptions);\n\n parentPort!.postMessage(result);\n});\n"]}
@@ -0,0 +1,13 @@
1
+ import { MinifyOptions } from 'terser';
2
+ declare module 'terser' {
3
+ interface SourceMapOptions {
4
+ asObject?: boolean;
5
+ }
6
+ }
7
+ import type { IModuleMinificationRequest, IModuleMinificationResult } from './types';
8
+ /**
9
+ * Minifies a single chunk of code. Factored out for reuse between WorkerPoolMinifier and LocalMinifier
10
+ * @internal
11
+ */
12
+ export declare function minifySingleFileAsync(request: IModuleMinificationRequest, terserOptions: MinifyOptions): Promise<IModuleMinificationResult>;
13
+ //# sourceMappingURL=MinifySingleFile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MinifySingleFile.d.ts","sourceRoot":"","sources":["../src/MinifySingleFile.ts"],"names":[],"mappings":"AAGA,OAAO,EAAU,aAAa,EAAyC,MAAM,QAAQ,CAAC;AAGtF,OAAO,QAAQ,QAAQ,CAAC;IAEtB,UAAU,gBAAgB;QACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;KACpB;CACF;AAGD,OAAO,KAAK,EAAE,0BAA0B,EAAE,yBAAyB,EAAE,MAAM,SAAS,CAAC;AAMrF;;;GAGG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,0BAA0B,EACnC,aAAa,EAAE,aAAa,GAC3B,OAAO,CAAC,yBAAyB,CAAC,CA2DpC"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.minifySingleFileAsync = void 0;
6
+ const terser_1 = require("terser");
7
+ const MinifiedIdentifier_1 = require("./MinifiedIdentifier");
8
+ const nth_identifier = {
9
+ get: MinifiedIdentifier_1.getIdentifier
10
+ };
11
+ /**
12
+ * Minifies a single chunk of code. Factored out for reuse between WorkerPoolMinifier and LocalMinifier
13
+ * @internal
14
+ */
15
+ async function minifySingleFileAsync(request, terserOptions) {
16
+ const { code, nameForMap, hash, externals } = request;
17
+ try {
18
+ const { format: rawFormat, output: rawOutput, mangle: originalMangle, ...remainingOptions } = terserOptions;
19
+ const format = rawFormat || rawOutput || {};
20
+ const mangle = originalMangle === false ? false : typeof originalMangle === 'object' ? { ...originalMangle } : {};
21
+ const finalOptions = {
22
+ ...remainingOptions,
23
+ format,
24
+ mangle
25
+ };
26
+ format.comments = false;
27
+ if (mangle) {
28
+ mangle.nth_identifier = nth_identifier;
29
+ }
30
+ if (mangle && externals) {
31
+ mangle.reserved = mangle.reserved ? externals.concat(mangle.reserved) : externals;
32
+ }
33
+ finalOptions.sourceMap = nameForMap
34
+ ? {
35
+ asObject: true
36
+ }
37
+ : false;
38
+ const minified = await (0, terser_1.minify)({
39
+ [nameForMap || 'code']: code
40
+ }, finalOptions);
41
+ return {
42
+ error: undefined,
43
+ code: minified.code,
44
+ map: minified.map,
45
+ hash
46
+ };
47
+ }
48
+ catch (error) {
49
+ console.error(error);
50
+ return {
51
+ error: error,
52
+ code: undefined,
53
+ map: undefined,
54
+ hash
55
+ };
56
+ }
57
+ }
58
+ exports.minifySingleFileAsync = minifySingleFileAsync;
59
+ //# sourceMappingURL=MinifySingleFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MinifySingleFile.js","sourceRoot":"","sources":["../src/MinifySingleFile.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,mCAAsF;AAUtF,6DAAqD;AAGrD,MAAM,cAAc,GAA4B;IAC9C,GAAG,EAAE,kCAAa;CACnB,CAAC;AAEF;;;GAGG;AACI,KAAK,UAAU,qBAAqB,CACzC,OAAmC,EACnC,aAA4B;IAE5B,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IAEtD,IAAI;QACF,MAAM,EACJ,MAAM,EAAE,SAAS,EACjB,MAAM,EAAE,SAAS,EACjB,MAAM,EAAE,cAAc,EACtB,GAAG,gBAAgB,EACpB,GAAG,aAAa,CAAC;QAElB,MAAM,MAAM,GAA4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IT license.\n// See LICENSE in the project root for license information.\n\nimport { minify, MinifyOptions, MinifyOutput, SimpleIdentifierMangler } from 'terser';\nimport type { RawSourceMap } from 'source-map';\n\ndeclare module 'terser' {\n // eslint-disable-next-line @typescript-eslint/naming-convention\n interface SourceMapOptions {\n asObject?: boolean;\n }\n}\n\nimport { getIdentifier } from './MinifiedIdentifier';\nimport type { IModuleMinificationRequest, IModuleMinificationResult } from './types';\n\nconst nth_identifier: SimpleIdentifierMangler = {\n get: getIdentifier\n};\n\n/**\n * Minifies a single chunk of code. Factored out for reuse between WorkerPoolMinifier and LocalMinifier\n * @internal\n */\nexport async function minifySingleFileAsync(\n request: IModuleMinificationRequest,\n terserOptions: MinifyOptions\n): Promise<IModuleMinificationResult> {\n const { code, nameForMap, hash, externals } = request;\n\n try {\n const {\n format: rawFormat,\n output: rawOutput,\n mangle: originalMangle,\n ...remainingOptions\n } = terserOptions;\n\n const format: MinifyOptions['format'] = rawFormat || rawOutput || {};\n\n const mangle: MinifyOptions['mangle'] =\n originalMangle === false ? false : typeof originalMangle === 'object' ? { ...originalMangle } : {};\n\n const finalOptions: MinifyOptions = {\n ...remainingOptions,\n format,\n mangle\n };\n format.comments = false;\n\n if (mangle) {\n mangle.nth_identifier = nth_identifier;\n }\n\n if (mangle && externals) {\n mangle.reserved = mangle.reserved ? externals.concat(mangle.reserved) : externals;\n }\n\n finalOptions.sourceMap = nameForMap\n ? {\n asObject: true\n }\n : false;\n\n const minified: MinifyOutput = await minify(\n {\n [nameForMap || 'code']: code\n },\n finalOptions\n );\n\n return {\n error: undefined,\n code: minified.code!,\n map: minified.map as unknown as RawSourceMap,\n hash\n };\n } catch (error) {\n console.error(error);\n return {\n error: error as Error,\n code: undefined,\n map: undefined,\n hash\n };\n }\n}\n"]}
@@ -0,0 +1,15 @@
1
+ import type { IMinifierConnection, IModuleMinificationCallback, IModuleMinificationRequest, IModuleMinifier } from './types';
2
+ /**
3
+ * Minifier implementation that does not actually transform the code, for debugging.
4
+ * @public
5
+ */
6
+ export declare class NoopMinifier implements IModuleMinifier {
7
+ /**
8
+ * No-op code transform.
9
+ * @param request - The request to process
10
+ * @param callback - The callback to invoke
11
+ */
12
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
13
+ connect(): Promise<IMinifierConnection>;
14
+ }
15
+ //# sourceMappingURL=NoopMinifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NoopMinifier.d.ts","sourceRoot":"","sources":["../src/NoopMinifier.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,0BAA0B,EAC1B,eAAe,EAChB,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,qBAAa,YAAa,YAAW,eAAe;IAClD;;;;OAIG;IACI,MAAM,CAAC,OAAO,EAAE,0BAA0B,EAAE,QAAQ,EAAE,2BAA2B,GAAG,IAAI;IAsBlF,OAAO,IAAI,OAAO,CAAC,mBAAmB,CAAC;CASrD"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.NoopMinifier = void 0;
6
+ /**
7
+ * Minifier implementation that does not actually transform the code, for debugging.
8
+ * @public
9
+ */
10
+ class NoopMinifier {
11
+ /**
12
+ * No-op code transform.
13
+ * @param request - The request to process
14
+ * @param callback - The callback to invoke
15
+ */
16
+ minify(request, callback) {
17
+ const { code, hash, nameForMap } = request;
18
+ callback({
19
+ hash,
20
+ error: undefined,
21
+ code,
22
+ // If source maps are requested, provide an empty source map
23
+ map: nameForMap
24
+ ? {
25
+ version: 3,
26
+ names: [],
27
+ file: nameForMap,
28
+ sources: [nameForMap],
29
+ sourcesContent: [code],
30
+ // In source mapping parlance, this means "map line 0, column 0 to the input file at index 0, line 0, column 0"
31
+ mappings: 'AAAA'
32
+ }
33
+ : undefined
34
+ });
35
+ }
36
+ async connect() {
37
+ return {
38
+ configHash: NoopMinifier.name,
39
+ disconnect: async () => {
40
+ // Do nothing.
41
+ }
42
+ };
43
+ }
44
+ }
45
+ exports.NoopMinifier = NoopMinifier;
46
+ //# sourceMappingURL=NoopMinifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NoopMinifier.js","sourceRoot":"","sources":["../src/NoopMinifier.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAS3D;;;GAGG;AACH,MAAa,YAAY;IACvB;;;;OAIG;IACI,MAAM,CAAC,OAAmC,EAAE,QAAqC;QACtF,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;QAE3C,QAAQ,CAAC;YACP,IAAI;YACJ,KAAK,EAAE,SAAS;YAChB,IAAI;YACJ,4DAA4D;YAC5D,GAAG,EAAE,UAAU;gBACb,CAAC,CAAC;oBACE,OAAO,EAAE,CAAC;oBACV,KAAK,EAAE,EAAE;oBACT,IAAI,EAAE,UAAU;oBAChB,OAAO,EAAE,CAAC,UAAU,CAAC;oBACrB,cAAc,EAAE,CAAC,IAAI,CAAC;oBACtB,+GAA+G;oBAC/G,QAAQ,EAAE,MAAM;iBACjB;gBACH,CAAC,CAAC,SAAS;SACd,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,OAAO;QAClB,OAAO;YACL,UAAU,EAAE,YAAY,CAAC,IAAI;YAE7B,UAAU,EAAE,KAAK,IAAI,EAAE;gBACrB,cAAc;YAChB,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AArCD,oCAqCC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type {\n IMinifierConnection,\n IModuleMinificationCallback,\n IModuleMinificationRequest,\n IModuleMinifier\n} from './types';\n\n/**\n * Minifier implementation that does not actually transform the code, for debugging.\n * @public\n */\nexport class NoopMinifier implements IModuleMinifier {\n /**\n * No-op code transform.\n * @param request - The request to process\n * @param callback - The callback to invoke\n */\n public minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void {\n const { code, hash, nameForMap } = request;\n\n callback({\n hash,\n error: undefined,\n code,\n // If source maps are requested, provide an empty source map\n map: nameForMap\n ? {\n version: 3,\n names: [],\n file: nameForMap,\n sources: [nameForMap],\n sourcesContent: [code],\n // In source mapping parlance, this means \"map line 0, column 0 to the input file at index 0, line 0, column 0\"\n mappings: 'AAAA'\n }\n : undefined\n });\n }\n\n public async connect(): Promise<IMinifierConnection> {\n return {\n configHash: NoopMinifier.name,\n\n disconnect: async () => {\n // Do nothing.\n }\n };\n }\n}\n"]}
@@ -0,0 +1,47 @@
1
+ import type { MinifyOptions } from 'terser';
2
+ import type { IMinifierConnection, IModuleMinificationCallback, IModuleMinificationRequest, IModuleMinifier } from './types';
3
+ /**
4
+ * Options for configuring the WorkerPoolMinifier
5
+ * @public
6
+ */
7
+ export interface IWorkerPoolMinifierOptions {
8
+ /**
9
+ * Maximum number of worker threads to use. Will never use more than there are modules to process.
10
+ * Defaults to os.cpus().length
11
+ */
12
+ maxThreads?: number;
13
+ /**
14
+ * The options to forward to Terser.
15
+ * `output.comments` is currently not configurable and will always extract license comments to a separate file.
16
+ */
17
+ terserOptions?: MinifyOptions;
18
+ /**
19
+ * If true, log to the console about the minification results.
20
+ */
21
+ verbose?: boolean;
22
+ }
23
+ /**
24
+ * Minifier implementation that uses a thread pool for minification.
25
+ * @public
26
+ */
27
+ export declare class WorkerPoolMinifier implements IModuleMinifier {
28
+ private readonly _pool;
29
+ private readonly _verbose;
30
+ private readonly _configHash;
31
+ private _refCount;
32
+ private _deduped;
33
+ private _minified;
34
+ private readonly _resultCache;
35
+ private readonly _activeRequests;
36
+ constructor(options: IWorkerPoolMinifierOptions);
37
+ get maxThreads(): number;
38
+ set maxThreads(threads: number);
39
+ /**
40
+ * Transform code by farming it out to a worker pool.
41
+ * @param request - The request to process
42
+ * @param callback - The callback to invoke
43
+ */
44
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
45
+ connect(): Promise<IMinifierConnection>;
46
+ }
47
+ //# sourceMappingURL=WorkerPoolMinifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WorkerPoolMinifier.d.ts","sourceRoot":"","sources":["../src/WorkerPoolMinifier.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAG5C,OAAO,KAAK,EACV,mBAAmB,EACnB,2BAA2B,EAE3B,0BAA0B,EAC1B,eAAe,EAChB,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;IAE9B;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,eAAe;IACxD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;IACnC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IAErC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,SAAS,CAAS;IAE1B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAyC;IACtE,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA6C;gBAE1D,OAAO,EAAE,0BAA0B;IA2BtD,IAAW,UAAU,IAAI,MAAM,CAE9B;IAED,IAAW,UAAU,CAAC,OAAO,EAAE,MAAM,EAEpC;IAED;;;;OAIG;IACI,MAAM,CAAC,OAAO,EAAE,0BAA0B,EAAE,QAAQ,EAAE,2BAA2B,GAAG,IAAI;IAqDlF,OAAO,IAAI,OAAO,CAAC,mBAAmB,CAAC;CAuBrD"}
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.WorkerPoolMinifier = void 0;
9
+ const crypto_1 = require("crypto");
10
+ const os_1 = require("os");
11
+ const serialize_javascript_1 = __importDefault(require("serialize-javascript"));
12
+ const worker_pool_1 = require("@rushstack/worker-pool");
13
+ /**
14
+ * Minifier implementation that uses a thread pool for minification.
15
+ * @public
16
+ */
17
+ class WorkerPoolMinifier {
18
+ constructor(options) {
19
+ const { maxThreads = (0, os_1.cpus)().length, terserOptions = {}, verbose = false } = options || {};
20
+ const activeRequests = new Map();
21
+ const resultCache = new Map();
22
+ const terserPool = new worker_pool_1.WorkerPool({
23
+ id: 'Minifier',
24
+ maxWorkers: maxThreads,
25
+ workerData: terserOptions,
26
+ workerScriptPath: require.resolve('./MinifierWorker')
27
+ });
28
+ this._configHash = (0, crypto_1.createHash)('sha256')
29
+ .update(WorkerPoolMinifier.name, 'utf8')
30
+ .update((0, serialize_javascript_1.default)(terserOptions))
31
+ .digest('base64');
32
+ this._activeRequests = activeRequests;
33
+ this._refCount = 0;
34
+ this._resultCache = resultCache;
35
+ this._pool = terserPool;
36
+ this._verbose = verbose;
37
+ this._deduped = 0;
38
+ this._minified = 0;
39
+ }
40
+ get maxThreads() {
41
+ return this._pool.maxWorkers;
42
+ }
43
+ set maxThreads(threads) {
44
+ this._pool.maxWorkers = threads;
45
+ }
46
+ /**
47
+ * Transform code by farming it out to a worker pool.
48
+ * @param request - The request to process
49
+ * @param callback - The callback to invoke
50
+ */
51
+ minify(request, callback) {
52
+ const { hash } = request;
53
+ const cached = this._resultCache.get(hash);
54
+ if (cached) {
55
+ ++this._deduped;
56
+ return callback(cached);
57
+ }
58
+ const { _activeRequests: activeRequests } = this;
59
+ const callbacks = activeRequests.get(hash);
60
+ if (callbacks) {
61
+ ++this._deduped;
62
+ callbacks.push(callback);
63
+ return;
64
+ }
65
+ activeRequests.set(hash, [callback]);
66
+ ++this._minified;
67
+ this._pool
68
+ .checkoutWorkerAsync(true)
69
+ .then((worker) => {
70
+ const cb = (message) => {
71
+ worker.off('message', cb);
72
+ const callbacks = activeRequests.get(message.hash);
73
+ activeRequests.delete(message.hash);
74
+ this._resultCache.set(message.hash, message);
75
+ for (const callback of callbacks) {
76
+ callback(message);
77
+ }
78
+ // This should always be the last thing done with the worker
79
+ this._pool.checkinWorker(worker);
80
+ };
81
+ worker.on('message', cb);
82
+ worker.postMessage(request);
83
+ })
84
+ .catch((error) => {
85
+ const errorCallbacks = activeRequests.get(hash);
86
+ for (const errorCallback of errorCallbacks) {
87
+ errorCallback({
88
+ hash,
89
+ error,
90
+ code: undefined,
91
+ map: undefined
92
+ });
93
+ }
94
+ });
95
+ }
96
+ async connect() {
97
+ if (++this._refCount === 1) {
98
+ this._pool.reset();
99
+ }
100
+ return {
101
+ configHash: this._configHash,
102
+ disconnect: async () => {
103
+ if (--this._refCount === 0) {
104
+ if (this._verbose) {
105
+ console.log(`Shutting down minifier worker pool`);
106
+ }
107
+ await this._pool.finishAsync();
108
+ this._resultCache.clear();
109
+ this._activeRequests.clear();
110
+ if (this._verbose) {
111
+ console.log(`Module minification: ${this._deduped} Deduped, ${this._minified} Processed`);
112
+ }
113
+ }
114
+ }
115
+ };
116
+ }
117
+ }
118
+ exports.WorkerPoolMinifier = WorkerPoolMinifier;
119
+ //# sourceMappingURL=WorkerPoolMinifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WorkerPoolMinifier.js","sourceRoot":"","sources":["../src/WorkerPoolMinifier.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;AAE3D,mCAAoC;AACpC,2BAA0B;AAE1B,gFAA6C;AAE7C,wDAAoD;AAgCpD;;;GAGG;AACH,MAAa,kBAAk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SAAS,EAAE,EAAE,CAAC,CAAC;YACzB,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAC9B,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAY,EAAE,EAAE;YACtB,MAAM,cAAc,GAAkC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAE,CAAC;YAChF,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE;gBAC1C,aAAa,CAAC;oBACZ,IAAI;oBACJ,KAAK;oBACL,IAAI,EAAE,SAAS;oBACf,GAAG,EAAE,SAAS;iBACf,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CAAC;IACP,CAAC;IAEM,KAAK,CAAC,OAAO;QAClB,IAAI,EAAE,IAAI,CAAC,SAAS,KAAK,CAAC,EAAE;YAC1B,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;SACpB;QAED,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,WAAW;YAE5B,UAAU,EAAE,KAAK,IAAI,EAAE;gBACrB,IAAI,EAAE,IAAI,CAAC,SAAS,KAAK,CAAC,EAAE;oBAC1B,IAAI,IAAI,CAAC,QAAQ,EAAE;wBACjB,OAAO,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC;qBACnD;oBACD,MAAM,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC;oBAC/B,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;oBAC1B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;oBAC7B,IAAI,IAAI,CAAC,QAAQ,EAAE;wBACjB,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,CAAC,QAAQ,aAAa,IAAI,CAAC,SAAS,YAAY,CAAC,CAAC;qBAC3F;iBACF;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AAhID,gDAgIC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { createHash } from 'crypto';\nimport { cpus } from 'os';\n\nimport serialize from 'serialize-javascript';\nimport type { MinifyOptions } from 'terser';\nimport { WorkerPool } from '@rushstack/worker-pool';\n\nimport type {\n IMinifierConnection,\n IModuleMinificationCallback,\n IModuleMinificationResult,\n IModuleMinificationRequest,\n IModuleMinifier\n} from './types';\n\n/**\n * Options for configuring the WorkerPoolMinifier\n * @public\n */\nexport interface IWorkerPoolMinifierOptions {\n /**\n * Maximum number of worker threads to use. Will never use more than there are modules to process.\n * Defaults to os.cpus().length\n */\n maxThreads?: number;\n /**\n * The options to forward to Terser.\n * `output.comments` is currently not configurable and will always extract license comments to a separate file.\n */\n terserOptions?: MinifyOptions;\n\n /**\n * If true, log to the console about the minification results.\n */\n verbose?: boolean;\n}\n\n/**\n * Minifier implementation that uses a thread pool for minification.\n * @public\n */\nexport class WorkerPoolMinifier implements IModuleMinifier {\n private readonly _pool: WorkerPool;\n private readonly _verbose: boolean;\n private readonly _configHash: string;\n\n private _refCount: number;\n private _deduped: number;\n private _minified: number;\n\n private readonly _resultCache: Map<string, IModuleMinificationResult>;\n private readonly _activeRequests: Map<string, IModuleMinificationCallback[]>;\n\n public constructor(options: IWorkerPoolMinifierOptions) {\n const { maxThreads = cpus().length, terserOptions = {}, verbose = false } = options || {};\n\n const activeRequests: Map<string, IModuleMinificationCallback[]> = new Map();\n const resultCache: Map<string, IModuleMinificationResult> = new Map();\n const terserPool: WorkerPool = new WorkerPool({\n id: 'Minifier',\n maxWorkers: maxThreads,\n workerData: terserOptions,\n workerScriptPath: require.resolve('./MinifierWorker')\n });\n\n this._configHash = createHash('sha256')\n .update(WorkerPoolMinifier.name, 'utf8')\n .update(serialize(terserOptions))\n .digest('base64');\n\n this._activeRequests = activeRequests;\n this._refCount = 0;\n this._resultCache = resultCache;\n this._pool = terserPool;\n this._verbose = verbose;\n\n this._deduped = 0;\n this._minified = 0;\n }\n\n public get maxThreads(): number {\n return this._pool.maxWorkers;\n }\n\n public set maxThreads(threads: number) {\n this._pool.maxWorkers = threads;\n }\n\n /**\n * Transform code by farming it out to a worker pool.\n * @param request - The request to process\n * @param callback - The callback to invoke\n */\n public minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void {\n const { hash } = request;\n\n const cached: IModuleMinificationResult | undefined = this._resultCache.get(hash);\n if (cached) {\n ++this._deduped;\n return callback(cached);\n }\n\n const { _activeRequests: activeRequests } = this;\n const callbacks: IModuleMinificationCallback[] | undefined = activeRequests.get(hash);\n if (callbacks) {\n ++this._deduped;\n callbacks.push(callback);\n return;\n }\n\n activeRequests.set(hash, [callback]);\n ++this._minified;\n\n this._pool\n .checkoutWorkerAsync(true)\n .then((worker) => {\n const cb: (message: IModuleMinificationResult) => void = (\n message: IModuleMinificationResult\n ): void => {\n worker.off('message', cb);\n const callbacks: IModuleMinificationCallback[] | undefined = activeRequests.get(message.hash)!;\n activeRequests.delete(message.hash);\n this._resultCache.set(message.hash, message);\n for (const callback of callbacks) {\n callback(message);\n }\n // This should always be the last thing done with the worker\n this._pool.checkinWorker(worker);\n };\n\n worker.on('message', cb);\n worker.postMessage(request);\n })\n .catch((error: Error) => {\n const errorCallbacks: IModuleMinificationCallback[] = activeRequests.get(hash)!;\n for (const errorCallback of errorCallbacks) {\n errorCallback({\n hash,\n error,\n code: undefined,\n map: undefined\n });\n }\n });\n }\n\n public async connect(): Promise<IMinifierConnection> {\n if (++this._refCount === 1) {\n this._pool.reset();\n }\n\n return {\n configHash: this._configHash,\n\n disconnect: async () => {\n if (--this._refCount === 0) {\n if (this._verbose) {\n console.log(`Shutting down minifier worker pool`);\n }\n await this._pool.finishAsync();\n this._resultCache.clear();\n this._activeRequests.clear();\n if (this._verbose) {\n console.log(`Module minification: ${this._deduped} Deduped, ${this._minified} Processed`);\n }\n }\n }\n };\n }\n}\n"]}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The sorted sequence of leading digits for mangled identifiers
3
+ * Used in MinifiedIdentifier computation for converting an ordinal to a valid ECMAScript identifier
4
+ * @public
5
+ */
6
+ export declare const IDENTIFIER_LEADING_DIGITS: string;
7
+ /**
8
+ * The sorted sequence of trailing digits for mangled identifiers
9
+ * Used in MinifiedIdentifier computation for converting an ordinal to a valid ECMAScript identifier
10
+ * @public
11
+ */
12
+ export declare const IDENTIFIER_TRAILING_DIGITS: string;
13
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,EAAE,MAAiE,CAAC;AAE1G;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,EAAE,MAC2B,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.IDENTIFIER_TRAILING_DIGITS = exports.IDENTIFIER_LEADING_DIGITS = void 0;
6
+ /**
7
+ * The sorted sequence of leading digits for mangled identifiers
8
+ * Used in MinifiedIdentifier computation for converting an ordinal to a valid ECMAScript identifier
9
+ * @public
10
+ */
11
+ exports.IDENTIFIER_LEADING_DIGITS = 'etnairoscdlufpm_hbgvySDIxCOwEALkMPTUFHRNBjVzGKWqQYJXZ$';
12
+ /**
13
+ * The sorted sequence of trailing digits for mangled identifiers
14
+ * Used in MinifiedIdentifier computation for converting an ordinal to a valid ECMAScript identifier
15
+ * @public
16
+ */
17
+ exports.IDENTIFIER_TRAILING_DIGITS = 'etnairoscdlufpm_hbg01v32y67S4985DIxCOwEALkMPTUFHRNBjVzGKWqQYJXZ$';
18
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;GAIG;AACU,QAAA,yBAAyB,GAAW,wDAAwD,CAAC;AAE1G;;;;GAIG;AACU,QAAA,0BAA0B,GACrC,kEAAkE,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * The sorted sequence of leading digits for mangled identifiers\n * Used in MinifiedIdentifier computation for converting an ordinal to a valid ECMAScript identifier\n * @public\n */\nexport const IDENTIFIER_LEADING_DIGITS: string = 'etnairoscdlufpm_hbgvySDIxCOwEALkMPTUFHRNBjVzGKWqQYJXZ$';\n\n/**\n * The sorted sequence of trailing digits for mangled identifiers\n * Used in MinifiedIdentifier computation for converting an ordinal to a valid ECMAScript identifier\n * @public\n */\nexport const IDENTIFIER_TRAILING_DIGITS: string =\n 'etnairoscdlufpm_hbg01v32y67S4985DIxCOwEALkMPTUFHRNBjVzGKWqQYJXZ$';\n"]}
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export type { MinifyOptions } from 'terser';
2
+ export type { ILocalMinifierOptions } from './LocalMinifier';
3
+ export { LocalMinifier } from './LocalMinifier';
4
+ export { MessagePortMinifier } from './MessagePortMinifier';
5
+ export { getIdentifier } from './MinifiedIdentifier';
6
+ export { minifySingleFileAsync as _minifySingleFileAsync } from './MinifySingleFile';
7
+ export { NoopMinifier } from './NoopMinifier';
8
+ export type { IMinifierConnection, IModuleMinificationCallback, IModuleMinificationErrorResult, IModuleMinificationRequest, IModuleMinificationResult, IModuleMinificationSuccessResult, IModuleMinifier, IModuleMinifierFunction } from './types';
9
+ export type { IWorkerPoolMinifierOptions } from './WorkerPoolMinifier';
10
+ export { WorkerPoolMinifier } from './WorkerPoolMinifier';
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,YAAY,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5C,YAAY,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,OAAO,EAAE,qBAAqB,IAAI,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAErF,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,YAAY,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,8BAA8B,EAC9B,0BAA0B,EAC1B,yBAAyB,EACzB,gCAAgC,EAChC,eAAe,EACf,uBAAuB,EACxB,MAAM,SAAS,CAAC;AAEjB,YAAY,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.WorkerPoolMinifier = exports.NoopMinifier = exports._minifySingleFileAsync = exports.getIdentifier = exports.MessagePortMinifier = exports.LocalMinifier = void 0;
6
+ var LocalMinifier_1 = require("./LocalMinifier");
7
+ Object.defineProperty(exports, "LocalMinifier", { enumerable: true, get: function () { return LocalMinifier_1.LocalMinifier; } });
8
+ var MessagePortMinifier_1 = require("./MessagePortMinifier");
9
+ Object.defineProperty(exports, "MessagePortMinifier", { enumerable: true, get: function () { return MessagePortMinifier_1.MessagePortMinifier; } });
10
+ var MinifiedIdentifier_1 = require("./MinifiedIdentifier");
11
+ Object.defineProperty(exports, "getIdentifier", { enumerable: true, get: function () { return MinifiedIdentifier_1.getIdentifier; } });
12
+ var MinifySingleFile_1 = require("./MinifySingleFile");
13
+ Object.defineProperty(exports, "_minifySingleFileAsync", { enumerable: true, get: function () { return MinifySingleFile_1.minifySingleFileAsync; } });
14
+ var NoopMinifier_1 = require("./NoopMinifier");
15
+ Object.defineProperty(exports, "NoopMinifier", { enumerable: true, get: function () { return NoopMinifier_1.NoopMinifier; } });
16
+ var WorkerPoolMinifier_1 = require("./WorkerPoolMinifier");
17
+ Object.defineProperty(exports, "WorkerPoolMinifier", { enumerable: true, get: function () { return WorkerPoolMinifier_1.WorkerPoolMinifier; } });
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAK3D,iDAAgD;AAAvC,8GAAA,aAAa,OAAA;AAEtB,6DAA4D;AAAnD,0HAAA,mBAAmB,OAAA;AAE5B,2DAAqD;AAA5C,mHAAA,aAAa,OAAA;AAEtB,uDAAqF;AAA5E,0HAAA,qBAAqB,OAA0B;AAExD,+CAA8C;AAArC,4GAAA,YAAY,OAAA;AAcrB,2DAA0D;AAAjD,wHAAA,kBAAkB,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nexport type { MinifyOptions } from 'terser';\n\nexport type { ILocalMinifierOptions } from './LocalMinifier';\nexport { LocalMinifier } from './LocalMinifier';\n\nexport { MessagePortMinifier } from './MessagePortMinifier';\n\nexport { getIdentifier } from './MinifiedIdentifier';\n\nexport { minifySingleFileAsync as _minifySingleFileAsync } from './MinifySingleFile';\n\nexport { NoopMinifier } from './NoopMinifier';\n\nexport type {\n IMinifierConnection,\n IModuleMinificationCallback,\n IModuleMinificationErrorResult,\n IModuleMinificationRequest,\n IModuleMinificationResult,\n IModuleMinificationSuccessResult,\n IModuleMinifier,\n IModuleMinifierFunction\n} from './types';\n\nexport type { IWorkerPoolMinifierOptions } from './WorkerPoolMinifier';\nexport { WorkerPoolMinifier } from './WorkerPoolMinifier';\n"]}
package/lib/types.d.ts ADDED
@@ -0,0 +1,117 @@
1
+ import type { RawSourceMap } from 'source-map';
2
+ /**
3
+ * Request to the minifier
4
+ * @public
5
+ */
6
+ export interface IModuleMinificationRequest {
7
+ /**
8
+ * Identity of the request. Will be included in the response.
9
+ */
10
+ hash: string;
11
+ /**
12
+ * The raw code fragment
13
+ */
14
+ code: string;
15
+ /**
16
+ * File name to show for the source code in the source map
17
+ */
18
+ nameForMap: string | undefined;
19
+ /**
20
+ * Reserved variable names, e.g. __WEBPACK_EXTERNAL_MODULE_1__
21
+ */
22
+ externals: string[] | undefined;
23
+ }
24
+ /**
25
+ * Result from the minifier function when an error is encountered.
26
+ * @public
27
+ */
28
+ export interface IModuleMinificationErrorResult {
29
+ /**
30
+ * Identity of the request
31
+ */
32
+ hash: string;
33
+ /**
34
+ * The error encountered, to be added to the current compilation's error collection.
35
+ */
36
+ error: Error;
37
+ /**
38
+ * Marker property to always return the same result shape.
39
+ */
40
+ code?: undefined;
41
+ /**
42
+ * Marker property to always return the same result shape.
43
+ */
44
+ map?: undefined;
45
+ }
46
+ /**
47
+ * Result from the minifier on a successful minification.
48
+ * @public
49
+ */
50
+ export interface IModuleMinificationSuccessResult {
51
+ /**
52
+ * Identity of the request
53
+ */
54
+ hash: string;
55
+ /**
56
+ * The error property being `undefined` indicates success.
57
+ */
58
+ error: undefined;
59
+ /**
60
+ * The minified code.
61
+ */
62
+ code: string;
63
+ /**
64
+ * Marker property to always return the same result shape.
65
+ */
66
+ map?: RawSourceMap;
67
+ }
68
+ /**
69
+ * Result from the minifier.
70
+ * @public
71
+ */
72
+ export declare type IModuleMinificationResult = IModuleMinificationErrorResult | IModuleMinificationSuccessResult;
73
+ /**
74
+ * Callback passed to a minifier function
75
+ * @public
76
+ */
77
+ export interface IModuleMinificationCallback {
78
+ (result: IModuleMinificationResult): void;
79
+ }
80
+ /**
81
+ * An async function called to minify a chunk of code
82
+ * @public
83
+ */
84
+ export interface IModuleMinifierFunction {
85
+ (request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
86
+ }
87
+ /**
88
+ * Metadata from the minifier for the plugin
89
+ * @public
90
+ */
91
+ export interface IMinifierConnection {
92
+ /**
93
+ * Hash of the configuration of this minifier, for cache busting.
94
+ */
95
+ configHash: string;
96
+ /**
97
+ * Callback to be invoked when done with the minifier
98
+ */
99
+ disconnect(): Promise<void>;
100
+ }
101
+ /**
102
+ * Object that can be invoked to minify code.
103
+ * @public
104
+ */
105
+ export interface IModuleMinifier {
106
+ /**
107
+ * Asynchronously minify a module
108
+ */
109
+ minify: IModuleMinifierFunction;
110
+ /**
111
+ * Prevents the minifier from shutting down until the returned `disconnect()` callback is invoked.
112
+ * The callback may be used to surface errors encountered by the minifier that may not be relevant to a specific file.
113
+ * It should be called to allow the minifier to cleanup
114
+ */
115
+ connect(): Promise<IMinifierConnection>;
116
+ }
117
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;;GAGG;AACH,MAAM,WAAW,0BAA0B;IACzC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B;;OAEG;IACH,SAAS,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,8BAA8B;IAC7C;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,KAAK,EAAE,KAAK,CAAC;IACb;;OAEG;IACH,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB;;OAEG;IACH,GAAG,CAAC,EAAE,SAAS,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,gCAAgC;IAC/C;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,KAAK,EAAE,SAAS,CAAC;IACjB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,GAAG,CAAC,EAAE,YAAY,CAAC;CACpB;AAED;;;GAGG;AACH,oBAAY,yBAAyB,GAAG,8BAA8B,GAAG,gCAAgC,CAAC;AAE1G;;;GAGG;AACH,MAAM,WAAW,2BAA2B;IAC1C,CAAC,MAAM,EAAE,yBAAyB,GAAG,IAAI,CAAC;CAC3C;AAED;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC,CAAC,OAAO,EAAE,0BAA0B,EAAE,QAAQ,EAAE,2BAA2B,GAAG,IAAI,CAAC;CACpF;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7B;AAED;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,MAAM,EAAE,uBAAuB,CAAC;IAEhC;;;;OAIG;IACH,OAAO,IAAI,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACzC"}
package/lib/types.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { RawSourceMap } from 'source-map';\n\n/**\n * Request to the minifier\n * @public\n */\nexport interface IModuleMinificationRequest {\n /**\n * Identity of the request. Will be included in the response.\n */\n hash: string;\n /**\n * The raw code fragment\n */\n code: string;\n /**\n * File name to show for the source code in the source map\n */\n nameForMap: string | undefined;\n /**\n * Reserved variable names, e.g. __WEBPACK_EXTERNAL_MODULE_1__\n */\n externals: string[] | undefined;\n}\n\n/**\n * Result from the minifier function when an error is encountered.\n * @public\n */\nexport interface IModuleMinificationErrorResult {\n /**\n * Identity of the request\n */\n hash: string;\n /**\n * The error encountered, to be added to the current compilation's error collection.\n */\n error: Error;\n /**\n * Marker property to always return the same result shape.\n */\n code?: undefined;\n /**\n * Marker property to always return the same result shape.\n */\n map?: undefined;\n}\n\n/**\n * Result from the minifier on a successful minification.\n * @public\n */\nexport interface IModuleMinificationSuccessResult {\n /**\n * Identity of the request\n */\n hash: string;\n /**\n * The error property being `undefined` indicates success.\n */\n error: undefined;\n /**\n * The minified code.\n */\n code: string;\n /**\n * Marker property to always return the same result shape.\n */\n map?: RawSourceMap;\n}\n\n/**\n * Result from the minifier.\n * @public\n */\nexport type IModuleMinificationResult = IModuleMinificationErrorResult | IModuleMinificationSuccessResult;\n\n/**\n * Callback passed to a minifier function\n * @public\n */\nexport interface IModuleMinificationCallback {\n (result: IModuleMinificationResult): void;\n}\n\n/**\n * An async function called to minify a chunk of code\n * @public\n */\nexport interface IModuleMinifierFunction {\n (request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;\n}\n\n/**\n * Metadata from the minifier for the plugin\n * @public\n */\nexport interface IMinifierConnection {\n /**\n * Hash of the configuration of this minifier, for cache busting.\n */\n configHash: string;\n /**\n * Callback to be invoked when done with the minifier\n */\n disconnect(): Promise<void>;\n}\n\n/**\n * Object that can be invoked to minify code.\n * @public\n */\nexport interface IModuleMinifier {\n /**\n * Asynchronously minify a module\n */\n minify: IModuleMinifierFunction;\n\n /**\n * Prevents the minifier from shutting down until the returned `disconnect()` callback is invoked.\n * The callback may be used to surface errors encountered by the minifier that may not be relevant to a specific file.\n * It should be called to allow the minifier to cleanup\n */\n connect(): Promise<IMinifierConnection>;\n}\n"]}