@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
package/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ @rushstack/module-minifier
2
+
3
+ Copyright (c) Microsoft Corporation. All rights reserved.
4
+
5
+ MIT License
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # @rushstack/module-minifier
2
+
3
+ This library wraps terser in convenient handles for parallelization. It powers @rushstack/module-minifier-plugin but has no coupling with webpack.
4
+
5
+ ## Links
6
+
7
+ - [CHANGELOG.md](
8
+ https://github.com/microsoft/rushstack/blob/main/libraries/module-minifier/CHANGELOG.md) - Find
9
+ out what's new in the latest version
10
+ - [API Reference](https://rushstack.io/pages/api/module-minifier/)
11
+
12
+ `@rushstack/module-minifier` is part of the [Rush Stack](https://rushstack.io/) family of projects.
@@ -0,0 +1,250 @@
1
+ /// <reference types="node" />
2
+
3
+ import { MessagePort } from 'worker_threads';
4
+ import { MinifyOptions } from 'terser';
5
+ import type { RawSourceMap } from 'source-map';
6
+
7
+ /**
8
+ * Gets a base54 string suitable for use as a JavaScript identifier, omitting those that are valid ECMAScript keywords
9
+ *
10
+ * @param ordinal - The number to convert to a base54 identifier
11
+ *
12
+ * @public
13
+ */
14
+ export declare function getIdentifier(ordinal: number): string;
15
+
16
+ /**
17
+ * Options for configuring the LocalMinifier
18
+ * @public
19
+ */
20
+ export declare interface ILocalMinifierOptions {
21
+ terserOptions?: MinifyOptions;
22
+ }
23
+
24
+ /**
25
+ * Metadata from the minifier for the plugin
26
+ * @public
27
+ */
28
+ export declare interface IMinifierConnection {
29
+ /**
30
+ * Hash of the configuration of this minifier, for cache busting.
31
+ */
32
+ configHash: string;
33
+ /**
34
+ * Callback to be invoked when done with the minifier
35
+ */
36
+ disconnect(): Promise<void>;
37
+ }
38
+
39
+ /**
40
+ * Callback passed to a minifier function
41
+ * @public
42
+ */
43
+ export declare interface IModuleMinificationCallback {
44
+ (result: IModuleMinificationResult): void;
45
+ }
46
+
47
+ /**
48
+ * Result from the minifier function when an error is encountered.
49
+ * @public
50
+ */
51
+ export declare interface IModuleMinificationErrorResult {
52
+ /**
53
+ * Identity of the request
54
+ */
55
+ hash: string;
56
+ /**
57
+ * The error encountered, to be added to the current compilation's error collection.
58
+ */
59
+ error: Error;
60
+ /**
61
+ * Marker property to always return the same result shape.
62
+ */
63
+ code?: undefined;
64
+ /**
65
+ * Marker property to always return the same result shape.
66
+ */
67
+ map?: undefined;
68
+ }
69
+
70
+ /**
71
+ * Request to the minifier
72
+ * @public
73
+ */
74
+ export declare interface IModuleMinificationRequest {
75
+ /**
76
+ * Identity of the request. Will be included in the response.
77
+ */
78
+ hash: string;
79
+ /**
80
+ * The raw code fragment
81
+ */
82
+ code: string;
83
+ /**
84
+ * File name to show for the source code in the source map
85
+ */
86
+ nameForMap: string | undefined;
87
+ /**
88
+ * Reserved variable names, e.g. __WEBPACK_EXTERNAL_MODULE_1__
89
+ */
90
+ externals: string[] | undefined;
91
+ }
92
+
93
+ /**
94
+ * Result from the minifier.
95
+ * @public
96
+ */
97
+ export declare type IModuleMinificationResult = IModuleMinificationErrorResult | IModuleMinificationSuccessResult;
98
+
99
+ /**
100
+ * Result from the minifier on a successful minification.
101
+ * @public
102
+ */
103
+ export declare interface IModuleMinificationSuccessResult {
104
+ /**
105
+ * Identity of the request
106
+ */
107
+ hash: string;
108
+ /**
109
+ * The error property being `undefined` indicates success.
110
+ */
111
+ error: undefined;
112
+ /**
113
+ * The minified code.
114
+ */
115
+ code: string;
116
+ /**
117
+ * Marker property to always return the same result shape.
118
+ */
119
+ map?: RawSourceMap;
120
+ }
121
+
122
+ /**
123
+ * Object that can be invoked to minify code.
124
+ * @public
125
+ */
126
+ export declare interface IModuleMinifier {
127
+ /**
128
+ * Asynchronously minify a module
129
+ */
130
+ minify: IModuleMinifierFunction;
131
+ /**
132
+ * Prevents the minifier from shutting down until the returned `disconnect()` callback is invoked.
133
+ * The callback may be used to surface errors encountered by the minifier that may not be relevant to a specific file.
134
+ * It should be called to allow the minifier to cleanup
135
+ */
136
+ connect(): Promise<IMinifierConnection>;
137
+ }
138
+
139
+ /**
140
+ * An async function called to minify a chunk of code
141
+ * @public
142
+ */
143
+ export declare interface IModuleMinifierFunction {
144
+ (request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
145
+ }
146
+
147
+ /**
148
+ * Options for configuring the WorkerPoolMinifier
149
+ * @public
150
+ */
151
+ export declare interface IWorkerPoolMinifierOptions {
152
+ /**
153
+ * Maximum number of worker threads to use. Will never use more than there are modules to process.
154
+ * Defaults to os.cpus().length
155
+ */
156
+ maxThreads?: number;
157
+ /**
158
+ * The options to forward to Terser.
159
+ * `output.comments` is currently not configurable and will always extract license comments to a separate file.
160
+ */
161
+ terserOptions?: MinifyOptions;
162
+ /**
163
+ * If true, log to the console about the minification results.
164
+ */
165
+ verbose?: boolean;
166
+ }
167
+
168
+ /**
169
+ * Minifier implementation that minifies code on the main thread.
170
+ * @public
171
+ */
172
+ export declare class LocalMinifier implements IModuleMinifier {
173
+ private readonly _terserOptions;
174
+ private readonly _resultCache;
175
+ private readonly _configHash;
176
+ constructor(options: ILocalMinifierOptions);
177
+ /**
178
+ * Transform that invokes Terser on the main thread
179
+ * @param request - The request to process
180
+ * @param callback - The callback to invoke
181
+ */
182
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
183
+ connect(): Promise<IMinifierConnection>;
184
+ }
185
+
186
+ /**
187
+ * Minifier implementation that outsources requests to the other side of a MessagePort
188
+ * @public
189
+ */
190
+ export declare class MessagePortMinifier implements IModuleMinifier {
191
+ readonly port: MessagePort;
192
+ private readonly _callbacks;
193
+ constructor(port: MessagePort);
194
+ /**
195
+ * No-op code transform.
196
+ * @param request - The request to process
197
+ * @param callback - The callback to invoke
198
+ */
199
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
200
+ connect(): Promise<IMinifierConnection>;
201
+ }
202
+
203
+ export { MinifyOptions }
204
+
205
+ /**
206
+ * Minifies a single chunk of code. Factored out for reuse between WorkerPoolMinifier and LocalMinifier
207
+ * @internal
208
+ */
209
+ export declare function _minifySingleFileAsync(request: IModuleMinificationRequest, terserOptions: MinifyOptions): Promise<IModuleMinificationResult>;
210
+
211
+ /**
212
+ * Minifier implementation that does not actually transform the code, for debugging.
213
+ * @public
214
+ */
215
+ export declare class NoopMinifier implements IModuleMinifier {
216
+ /**
217
+ * No-op code transform.
218
+ * @param request - The request to process
219
+ * @param callback - The callback to invoke
220
+ */
221
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
222
+ connect(): Promise<IMinifierConnection>;
223
+ }
224
+
225
+ /**
226
+ * Minifier implementation that uses a thread pool for minification.
227
+ * @public
228
+ */
229
+ export declare class WorkerPoolMinifier implements IModuleMinifier {
230
+ private readonly _pool;
231
+ private readonly _verbose;
232
+ private readonly _configHash;
233
+ private _refCount;
234
+ private _deduped;
235
+ private _minified;
236
+ private readonly _resultCache;
237
+ private readonly _activeRequests;
238
+ constructor(options: IWorkerPoolMinifierOptions);
239
+ get maxThreads(): number;
240
+ set maxThreads(threads: number);
241
+ /**
242
+ * Transform code by farming it out to a worker pool.
243
+ * @param request - The request to process
244
+ * @param callback - The callback to invoke
245
+ */
246
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
247
+ connect(): Promise<IMinifierConnection>;
248
+ }
249
+
250
+ export { }
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.24.1"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,27 @@
1
+ import type { MinifyOptions } from 'terser';
2
+ import type { IMinifierConnection, IModuleMinificationCallback, IModuleMinificationRequest, IModuleMinifier } from './types';
3
+ /**
4
+ * Options for configuring the LocalMinifier
5
+ * @public
6
+ */
7
+ export interface ILocalMinifierOptions {
8
+ terserOptions?: MinifyOptions;
9
+ }
10
+ /**
11
+ * Minifier implementation that minifies code on the main thread.
12
+ * @public
13
+ */
14
+ export declare class LocalMinifier implements IModuleMinifier {
15
+ private readonly _terserOptions;
16
+ private readonly _resultCache;
17
+ private readonly _configHash;
18
+ constructor(options: ILocalMinifierOptions);
19
+ /**
20
+ * Transform that invokes Terser on the main thread
21
+ * @param request - The request to process
22
+ * @param callback - The callback to invoke
23
+ */
24
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
25
+ connect(): Promise<IMinifierConnection>;
26
+ }
27
+ //# sourceMappingURL=LocalMinifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalMinifier.d.ts","sourceRoot":"","sources":["../src/LocalMinifier.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5C,OAAO,KAAK,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,0BAA0B,EAE1B,eAAe,EAChB,MAAM,SAAS,CAAC;AAGjB;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B;AAED;;;GAGG;AACH,qBAAa,aAAc,YAAW,eAAe;IACnD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAE/C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAyC;IACtE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;gBAElB,OAAO,EAAE,qBAAqB;IAoBjD;;;;OAIG;IACI,MAAM,CAAC,OAAO,EAAE,0BAA0B,EAAE,QAAQ,EAAE,2BAA2B,GAAG,IAAI;IAwBlF,OAAO,IAAI,OAAO,CAAC,mBAAmB,CAAC;CAQrD"}
@@ -0,0 +1,69 @@
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.LocalMinifier = void 0;
9
+ const crypto_1 = require("crypto");
10
+ const serialize_javascript_1 = __importDefault(require("serialize-javascript"));
11
+ const MinifySingleFile_1 = require("./MinifySingleFile");
12
+ /**
13
+ * Minifier implementation that minifies code on the main thread.
14
+ * @public
15
+ */
16
+ class LocalMinifier {
17
+ constructor(options) {
18
+ const { terserOptions = {} } = options || {};
19
+ this._terserOptions = {
20
+ ...terserOptions,
21
+ output: terserOptions.output
22
+ ? {
23
+ ...terserOptions.output
24
+ }
25
+ : {}
26
+ };
27
+ this._configHash = (0, crypto_1.createHash)('sha256')
28
+ .update(LocalMinifier.name, 'utf8')
29
+ .update((0, serialize_javascript_1.default)(terserOptions))
30
+ .digest('base64');
31
+ this._resultCache = new Map();
32
+ }
33
+ /**
34
+ * Transform that invokes Terser on the main thread
35
+ * @param request - The request to process
36
+ * @param callback - The callback to invoke
37
+ */
38
+ minify(request, callback) {
39
+ const { hash } = request;
40
+ const cached = this._resultCache.get(hash);
41
+ if (cached) {
42
+ return callback(cached);
43
+ }
44
+ (0, MinifySingleFile_1.minifySingleFileAsync)(request, this._terserOptions)
45
+ .then((result) => {
46
+ this._resultCache.set(hash, result);
47
+ callback(result);
48
+ })
49
+ .catch((error) => {
50
+ // This branch is here to satisfy the no-floating-promises lint rule
51
+ callback({
52
+ error: error,
53
+ code: undefined,
54
+ map: undefined,
55
+ hash
56
+ });
57
+ });
58
+ }
59
+ async connect() {
60
+ return {
61
+ configHash: this._configHash,
62
+ disconnect: async () => {
63
+ // Do nothing.
64
+ }
65
+ };
66
+ }
67
+ }
68
+ exports.LocalMinifier = LocalMinifier;
69
+ //# sourceMappingURL=LocalMinifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalMinifier.js","sourceRoot":"","sources":["../src/LocalMinifier.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;AAE3D,mCAAoC;AACpC,gFAA6C;AAU7C,yDAA2D;AAU3D;;;GAGG;AACH,MAAa,aAAa;IAMxB,YAAmB,OAA8B;QAC/C,MAAM,EAAE,aAAa,GAAG,EAAE,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;QAE7C,IAAI,CAAC,cAAc,GAAG;YACpB,GAAG,aAAa;YAChB,MAAM,EAAE,aAAa,CAAC,MAAM;gBAC1B,CAAC,CAAC;oBACE,GAAG,aAAa,CAAC,MAAM;iBACxB;gBACH,CAAC,CAAC,EAAE;SACP,CAAC;QAEF,IAAI,CAAC,WAAW,GAAG,IAAA,mBAAU,EAAC,QAAQ,CAAC;aACpC,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE,MAAM,CAAC;aAClC,MAAM,CAAC,IAAA,8BAAS,EAAC,aAAa,CAAC,CAAC;aAChC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAEpB,IAAI,CAAC,YAAY,GAAG,IAAI,GAAG,EAAE,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,OAAmC,EAAE,QAAqC;QACtF,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;QAEzB,MAAM,MAAM,GAA0C,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAClF,IAAI,MAAM,EAAE;YACV,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC;SACzB;QAED,IAAA,wCAAqB,EAAC,OAAO,EAAE,IAAI,CAAC,cAAc,CAAC;aAChD,IAAI,CAAC,CAAC,MAAiC,EAAE,EAAE;YAC1C,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACpC,QAAQ,CAAC,MAAM,CAAC,CAAC;QACnB,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACf,oEAAoE;YACpE,QAAQ,CAAC;gBACP,KAAK,EAAE,KAAc;gBACrB,IAAI,EAAE,SAAS;gBACf,GAAG,EAAE,SAAS;gBACd,IAAI;aACL,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACP,CAAC;IAEM,KAAK,CAAC,OAAO;QAClB,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,WAAW;YAC5B,UAAU,EAAE,KAAK,IAAI,EAAE;gBACrB,cAAc;YAChB,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AA/DD,sCA+DC","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 serialize from 'serialize-javascript';\nimport type { MinifyOptions } from 'terser';\n\nimport type {\n IMinifierConnection,\n IModuleMinificationCallback,\n IModuleMinificationRequest,\n IModuleMinificationResult,\n IModuleMinifier\n} from './types';\nimport { minifySingleFileAsync } from './MinifySingleFile';\n\n/**\n * Options for configuring the LocalMinifier\n * @public\n */\nexport interface ILocalMinifierOptions {\n terserOptions?: MinifyOptions;\n}\n\n/**\n * Minifier implementation that minifies code on the main thread.\n * @public\n */\nexport class LocalMinifier implements IModuleMinifier {\n private readonly _terserOptions: MinifyOptions;\n\n private readonly _resultCache: Map<string, IModuleMinificationResult>;\n private readonly _configHash: string;\n\n public constructor(options: ILocalMinifierOptions) {\n const { terserOptions = {} } = options || {};\n\n this._terserOptions = {\n ...terserOptions,\n output: terserOptions.output\n ? {\n ...terserOptions.output\n }\n : {}\n };\n\n this._configHash = createHash('sha256')\n .update(LocalMinifier.name, 'utf8')\n .update(serialize(terserOptions))\n .digest('base64');\n\n this._resultCache = new Map();\n }\n\n /**\n * Transform that invokes Terser on the main thread\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 return callback(cached);\n }\n\n minifySingleFileAsync(request, this._terserOptions)\n .then((result: IModuleMinificationResult) => {\n this._resultCache.set(hash, result);\n callback(result);\n })\n .catch((error) => {\n // This branch is here to satisfy the no-floating-promises lint rule\n callback({\n error: error as Error,\n code: undefined,\n map: undefined,\n hash\n });\n });\n }\n\n public async connect(): Promise<IMinifierConnection> {\n return {\n configHash: this._configHash,\n disconnect: async () => {\n // Do nothing.\n }\n };\n }\n}\n"]}
@@ -0,0 +1,20 @@
1
+ /// <reference types="node" />
2
+ import { MessagePort } from 'worker_threads';
3
+ import type { IMinifierConnection, IModuleMinificationCallback, IModuleMinificationRequest, IModuleMinifier } from './types';
4
+ /**
5
+ * Minifier implementation that outsources requests to the other side of a MessagePort
6
+ * @public
7
+ */
8
+ export declare class MessagePortMinifier implements IModuleMinifier {
9
+ readonly port: MessagePort;
10
+ private readonly _callbacks;
11
+ constructor(port: MessagePort);
12
+ /**
13
+ * No-op code transform.
14
+ * @param request - The request to process
15
+ * @param callback - The callback to invoke
16
+ */
17
+ minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void;
18
+ connect(): Promise<IMinifierConnection>;
19
+ }
20
+ //# sourceMappingURL=MessagePortMinifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessagePortMinifier.d.ts","sourceRoot":"","sources":["../src/MessagePortMinifier.ts"],"names":[],"mappings":";AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,OAAO,KAAK,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,0BAA0B,EAE1B,eAAe,EAChB,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,qBAAa,mBAAoB,YAAW,eAAe;IACzD,SAAgB,IAAI,EAAE,WAAW,CAAC;IAElC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA6C;gBAErD,IAAI,EAAE,WAAW;IAKpC;;;;OAIG;IACI,MAAM,CAAC,OAAO,EAAE,0BAA0B,EAAE,QAAQ,EAAE,2BAA2B,GAAG,IAAI;IAclF,OAAO,IAAI,OAAO,CAAC,mBAAmB,CAAC;CA0BrD"}
@@ -0,0 +1,56 @@
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.MessagePortMinifier = void 0;
6
+ const events_1 = require("events");
7
+ /**
8
+ * Minifier implementation that outsources requests to the other side of a MessagePort
9
+ * @public
10
+ */
11
+ class MessagePortMinifier {
12
+ constructor(port) {
13
+ this.port = port;
14
+ this._callbacks = new Map();
15
+ }
16
+ /**
17
+ * No-op code transform.
18
+ * @param request - The request to process
19
+ * @param callback - The callback to invoke
20
+ */
21
+ minify(request, callback) {
22
+ const { hash } = request;
23
+ const callbacks = this._callbacks.get(hash);
24
+ if (callbacks) {
25
+ callbacks.push(callback);
26
+ return;
27
+ }
28
+ this._callbacks.set(hash, [callback]);
29
+ this.port.postMessage(request);
30
+ }
31
+ async connect() {
32
+ const configHashPromise = (0, events_1.once)(this.port, 'message');
33
+ this.port.postMessage('initialize');
34
+ const configHash = await configHashPromise;
35
+ const callbacks = this._callbacks;
36
+ function handler(message) {
37
+ if (typeof message === 'object') {
38
+ const callbacksForRequest = callbacks.get(message.hash);
39
+ callbacks.delete(message.hash);
40
+ for (const callback of callbacksForRequest) {
41
+ callback(message);
42
+ }
43
+ }
44
+ }
45
+ this.port.on('message', handler);
46
+ return {
47
+ configHash,
48
+ disconnect: async () => {
49
+ this.port.off('message', handler);
50
+ this.port.close();
51
+ }
52
+ };
53
+ }
54
+ }
55
+ exports.MessagePortMinifier = MessagePortMinifier;
56
+ //# sourceMappingURL=MessagePortMinifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessagePortMinifier.js","sourceRoot":"","sources":["../src/MessagePortMinifier.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,mCAA8B;AAW9B;;;GAGG;AACH,MAAa,mBAAmB;IAK9B,YAAmB,IAAiB;QAClC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,OAAmC,EAAE,QAAqC;QACtF,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;QAEzB,MAAM,SAAS,GAA8C,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACvF,IAAI,SAAS,EAAE;YACb,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACzB,OAAO;SACR;QAED,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;QAEtC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;IAEM,KAAK,CAAC,OAAO;QAClB,MAAM,iBAAiB,GAAoB,IAAA,aAAI,EAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAA+B,CAAC;QACpG,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;QACpC,MAAM,UAAU,GAAW,MAAM,iBAAiB,CAAC;QAEnD,MAAM,SAAS,GAA+C,IAAI,CAAC,UAAU,CAAC;QAE9E,SAAS,OAAO,CAAC,OAAmD;YAClE,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAC/B,MAAM,mBAAmB,GAAkC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAE,CAAC;gBACxF,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;gBAC/B,KAAK,MAAM,QAAQ,IAAI,mBAAmB,EAAE;oBAC1C,QAAQ,CAAC,OAAO,CAAC,CAAC;iBACnB;aACF;QACH,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACjC,OAAO;YACL,UAAU;YACV,UAAU,EAAE,KAAK,IAAI,EAAE;gBACrB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;gBAClC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YACpB,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AAvDD,kDAuDC","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 { once } from 'events';\nimport { MessagePort } from 'worker_threads';\n\nimport type {\n IMinifierConnection,\n IModuleMinificationCallback,\n IModuleMinificationRequest,\n IModuleMinificationResult,\n IModuleMinifier\n} from './types';\n\n/**\n * Minifier implementation that outsources requests to the other side of a MessagePort\n * @public\n */\nexport class MessagePortMinifier implements IModuleMinifier {\n public readonly port: MessagePort;\n\n private readonly _callbacks: Map<string, IModuleMinificationCallback[]>;\n\n public constructor(port: MessagePort) {\n this.port = port;\n this._callbacks = new Map();\n }\n\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 { hash } = request;\n\n const callbacks: IModuleMinificationCallback[] | undefined = this._callbacks.get(hash);\n if (callbacks) {\n callbacks.push(callback);\n return;\n }\n\n this._callbacks.set(hash, [callback]);\n\n this.port.postMessage(request);\n }\n\n public async connect(): Promise<IMinifierConnection> {\n const configHashPromise: Promise<string> = once(this.port, 'message') as unknown as Promise<string>;\n this.port.postMessage('initialize');\n const configHash: string = await configHashPromise;\n\n const callbacks: Map<string, IModuleMinificationCallback[]> = this._callbacks;\n\n function handler(message: IModuleMinificationResult | number | false): void {\n if (typeof message === 'object') {\n const callbacksForRequest: IModuleMinificationCallback[] = callbacks.get(message.hash)!;\n callbacks.delete(message.hash);\n for (const callback of callbacksForRequest) {\n callback(message);\n }\n }\n }\n\n this.port.on('message', handler);\n return {\n configHash,\n disconnect: async () => {\n this.port.off('message', handler);\n this.port.close();\n }\n };\n }\n}\n"]}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Gets a base54 string suitable for use as a JavaScript identifier, not accounting for reserved keywords
3
+ * @param ordinal - The number to convert to a base54 identifier
4
+ */
5
+ export declare function getIdentifierInternal(ordinal: number): string;
6
+ /**
7
+ * Converts an identifier into the ordinal that would produce it, not accounting for reserved keywords
8
+ * Returns NaN if the result would exceed 31 bits
9
+ * @param identifier - The identifier to convert to a numeric value
10
+ */
11
+ export declare function getOrdinalFromIdentifierInternal(identifier: string): number;
12
+ /**
13
+ * Gets a base54 string suitable for use as a JavaScript identifier, omitting those that are valid ECMAScript keywords
14
+ *
15
+ * @param ordinal - The number to convert to a base54 identifier
16
+ *
17
+ * @public
18
+ */
19
+ export declare function getIdentifier(ordinal: number): string;
20
+ //# sourceMappingURL=MinifiedIdentifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MinifiedIdentifier.d.ts","sourceRoot":"","sources":["../src/MinifiedIdentifier.ts"],"names":[],"mappings":"AA0EA;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAW7D;AAYD;;;;GAIG;AACH,wBAAgB,gCAAgC,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAmB3E;AAgBD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAWrD"}
@@ -0,0 +1,148 @@
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.getIdentifier = exports.getOrdinalFromIdentifierInternal = exports.getIdentifierInternal = void 0;
6
+ // TODO: Allow dynamic override of these values in the input to the minifier
7
+ const constants_1 = require("./constants");
8
+ // Set of ECMAScript reserved keywords (past and present): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar
9
+ const RESERVED_KEYWORDS = [
10
+ 'abstract',
11
+ 'arguments',
12
+ 'boolean',
13
+ 'break',
14
+ 'byte',
15
+ 'case',
16
+ 'catch',
17
+ 'char',
18
+ 'class',
19
+ 'const',
20
+ 'continue',
21
+ 'debugger',
22
+ 'default',
23
+ 'delete',
24
+ 'do',
25
+ 'double',
26
+ 'else',
27
+ 'enum',
28
+ 'export',
29
+ 'extends',
30
+ 'false',
31
+ 'final',
32
+ 'finally',
33
+ 'float',
34
+ 'for',
35
+ 'function',
36
+ 'get',
37
+ 'goto',
38
+ 'if',
39
+ 'implements',
40
+ 'import',
41
+ 'in',
42
+ 'instanceof',
43
+ 'int',
44
+ 'interface',
45
+ 'let',
46
+ 'long',
47
+ 'native',
48
+ 'new',
49
+ 'null',
50
+ 'package',
51
+ 'private',
52
+ 'protected',
53
+ 'public',
54
+ 'return',
55
+ 'set',
56
+ 'short',
57
+ 'static',
58
+ 'super',
59
+ 'switch',
60
+ 'synchronized',
61
+ 'this',
62
+ 'throw',
63
+ 'throws',
64
+ 'transient',
65
+ 'true',
66
+ 'try',
67
+ 'typeof',
68
+ 'var',
69
+ 'void',
70
+ 'volatile',
71
+ 'while',
72
+ 'with',
73
+ 'yield'
74
+ ];
75
+ /**
76
+ * Gets a base54 string suitable for use as a JavaScript identifier, not accounting for reserved keywords
77
+ * @param ordinal - The number to convert to a base54 identifier
78
+ */
79
+ function getIdentifierInternal(ordinal) {
80
+ let ret = constants_1.IDENTIFIER_LEADING_DIGITS[ordinal % 54];
81
+ ordinal = (ordinal / 54) | 0; // eslint-disable-line no-bitwise
82
+ while (ordinal > 0) {
83
+ --ordinal;
84
+ ret += constants_1.IDENTIFIER_TRAILING_DIGITS[ordinal & 0x3f]; // eslint-disable-line no-bitwise
85
+ ordinal >>>= 6; // eslint-disable-line no-bitwise
86
+ }
87
+ return ret;
88
+ }
89
+ exports.getIdentifierInternal = getIdentifierInternal;
90
+ const leadingCharIndex = new Map();
91
+ for (let i = 0; i < 64; i++) {
92
+ leadingCharIndex.set(constants_1.IDENTIFIER_LEADING_DIGITS.charCodeAt(i), i);
93
+ }
94
+ const trailingCharIndex = new Map();
95
+ for (let i = 0; i < 64; i++) {
96
+ trailingCharIndex.set(constants_1.IDENTIFIER_TRAILING_DIGITS.charCodeAt(i), i);
97
+ }
98
+ /**
99
+ * Converts an identifier into the ordinal that would produce it, not accounting for reserved keywords
100
+ * Returns NaN if the result would exceed 31 bits
101
+ * @param identifier - The identifier to convert to a numeric value
102
+ */
103
+ function getOrdinalFromIdentifierInternal(identifier) {
104
+ let ordinal = 0;
105
+ for (let i = identifier.length - 1; i > 0; i--) {
106
+ if (ordinal >= 0x2000000) {
107
+ return NaN;
108
+ }
109
+ ordinal <<= 6; // eslint-disable-line no-bitwise
110
+ ordinal += trailingCharIndex.get(identifier.charCodeAt(i)) + 1;
111
+ }
112
+ if (ordinal >= 0x2000000) {
113
+ return NaN;
114
+ }
115
+ ordinal *= 54;
116
+ ordinal += leadingCharIndex.get(identifier.charCodeAt(0));
117
+ return ordinal;
118
+ }
119
+ exports.getOrdinalFromIdentifierInternal = getOrdinalFromIdentifierInternal;
120
+ /**
121
+ * getIdentifier(n) would otherwise return each of these specified ECMAScript reserved keywords, which are not legal identifiers.
122
+ */
123
+ const RESERVED_ORDINALS = (() => {
124
+ const reserved = [];
125
+ for (const keyword of RESERVED_KEYWORDS) {
126
+ const ordinal = getOrdinalFromIdentifierInternal(keyword);
127
+ if (!isNaN(ordinal)) {
128
+ reserved.push(ordinal);
129
+ }
130
+ }
131
+ return reserved.sort((x, y) => x - y);
132
+ })();
133
+ /**
134
+ * Gets a base54 string suitable for use as a JavaScript identifier, omitting those that are valid ECMAScript keywords
135
+ *
136
+ * @param ordinal - The number to convert to a base54 identifier
137
+ *
138
+ * @public
139
+ */
140
+ function getIdentifier(ordinal) {
141
+ // Need to skip over reserved keywords
142
+ for (let i = 0, len = RESERVED_ORDINALS.length; i < len && ordinal >= RESERVED_ORDINALS[i]; i++) {
143
+ ++ordinal;
144
+ }
145
+ return getIdentifierInternal(ordinal);
146
+ }
147
+ exports.getIdentifier = getIdentifier;
148
+ //# sourceMappingURL=MinifiedIdentifier.js.map