@aws-amplify/graphql-model-transformer 1.3.3-with-standalone-transformer.0 → 1.3.4-agqlac.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 (57) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/lib/graphql-model-transformer.js +3 -3
  3. package/lib/graphql-model-transformer.js.map +1 -1
  4. package/lib/rds-lambda.zip +0 -0
  5. package/package.json +13 -6
  6. package/rds-lambda/node_modules/.package-lock.json +200 -200
  7. package/rds-lambda/node_modules/@aws-sdk/client-ssm/package.json +13 -13
  8. package/rds-lambda/node_modules/@aws-sdk/client-sso/package.json +10 -10
  9. package/rds-lambda/node_modules/@aws-sdk/client-sso-oidc/package.json +10 -10
  10. package/rds-lambda/node_modules/@aws-sdk/client-sts/package.json +13 -13
  11. package/rds-lambda/node_modules/@aws-sdk/config-resolver/package.json +2 -2
  12. package/rds-lambda/node_modules/@aws-sdk/credential-provider-env/package.json +2 -2
  13. package/rds-lambda/node_modules/@aws-sdk/credential-provider-imds/package.json +3 -3
  14. package/rds-lambda/node_modules/@aws-sdk/credential-provider-ini/package.json +8 -8
  15. package/rds-lambda/node_modules/@aws-sdk/credential-provider-node/package.json +9 -9
  16. package/rds-lambda/node_modules/@aws-sdk/credential-provider-process/package.json +3 -3
  17. package/rds-lambda/node_modules/@aws-sdk/credential-provider-sso/package.json +5 -5
  18. package/rds-lambda/node_modules/@aws-sdk/credential-provider-web-identity/package.json +2 -2
  19. package/rds-lambda/node_modules/@aws-sdk/fetch-http-handler/package.json +1 -1
  20. package/rds-lambda/node_modules/@aws-sdk/middleware-retry/package.json +2 -2
  21. package/rds-lambda/node_modules/@aws-sdk/middleware-sdk-sts/package.json +2 -2
  22. package/rds-lambda/node_modules/@aws-sdk/middleware-signing/package.json +3 -3
  23. package/rds-lambda/node_modules/@aws-sdk/middleware-user-agent/package.json +2 -2
  24. package/rds-lambda/node_modules/@aws-sdk/node-config-provider/package.json +3 -3
  25. package/rds-lambda/node_modules/@aws-sdk/property-provider/package.json +1 -1
  26. package/rds-lambda/node_modules/@aws-sdk/shared-ini-file-loader/package.json +1 -1
  27. package/rds-lambda/node_modules/@aws-sdk/signature-v4/package.json +1 -1
  28. package/rds-lambda/node_modules/@aws-sdk/token-providers/package.json +4 -4
  29. package/rds-lambda/node_modules/@aws-sdk/util-defaults-mode-browser/package.json +2 -2
  30. package/rds-lambda/node_modules/@aws-sdk/util-defaults-mode-node/package.json +5 -5
  31. package/rds-lambda/node_modules/@aws-sdk/util-endpoints/dist-cjs/lib/aws/partitions.json +11 -0
  32. package/rds-lambda/node_modules/@aws-sdk/util-endpoints/dist-es/lib/aws/partitions.json +11 -0
  33. package/rds-lambda/node_modules/@aws-sdk/util-endpoints/package.json +1 -1
  34. package/rds-lambda/node_modules/@aws-sdk/util-user-agent-node/package.json +2 -2
  35. package/rds-lambda/node_modules/@sinonjs/fake-timers/README.md +1 -4
  36. package/rds-lambda/node_modules/@sinonjs/fake-timers/package.json +1 -1
  37. package/rds-lambda/node_modules/@smithy/protocol-http/package.json +3 -2
  38. package/rds-lambda/node_modules/@smithy/types/package.json +7 -2
  39. package/rds-lambda/node_modules/@types/node/README.md +1 -1
  40. package/rds-lambda/node_modules/@types/node/package.json +2 -2
  41. package/rds-lambda/node_modules/browserslist/package.json +3 -3
  42. package/rds-lambda/node_modules/caniuse-lite/package.json +1 -1
  43. package/rds-lambda/node_modules/cjs-module-lexer/LICENSE +10 -10
  44. package/rds-lambda/node_modules/cjs-module-lexer/README.md +461 -453
  45. package/rds-lambda/node_modules/cjs-module-lexer/dist/lexer.mjs +1 -1
  46. package/rds-lambda/node_modules/cjs-module-lexer/package.json +45 -43
  47. package/rds-lambda/node_modules/electron-to-chromium/full-chromium-versions.json +1 -1
  48. package/rds-lambda/node_modules/electron-to-chromium/full-versions.json +1 -1
  49. package/rds-lambda/node_modules/electron-to-chromium/package.json +1 -1
  50. package/rds-lambda/node_modules/jest-snapshot/node_modules/semver/package.json +4 -4
  51. package/rds-lambda/node_modules/pirates/package.json +6 -6
  52. package/rds-lambda/node_modules/ts-jest/node_modules/semver/package.json +4 -4
  53. package/rds-lambda/package-lock.json +200 -200
  54. package/src/__tests__/model-transformer.test.ts +6 -42
  55. package/src/graphql-model-transformer.ts +3 -3
  56. package/tsconfig.tsbuildinfo +1 -1
  57. package/rds-lambda/node_modules/cjs-module-lexer/CHANGELOG.md +0 -40
@@ -1,453 +1,461 @@
1
- # CJS Module Lexer
2
-
3
- [![Build Status][travis-image]][travis-url]
4
-
5
- A [very fast](#benchmarks) JS CommonJS module syntax lexer used to detect the most likely list of named exports of a CommonJS module.
6
-
7
- Outputs the list of named exports (`exports.name = ...`) and possible module reexports (`module.exports = require('...')`), including the common transpiler variations of these cases.
8
-
9
- Forked from https://github.com/guybedford/es-module-lexer.
10
-
11
- _Comprehensively handles the JS language grammar while remaining small and fast. - ~90ms per MB of JS cold and ~15ms per MB of JS warm, [see benchmarks](#benchmarks) for more info._
12
-
13
- ### Usage
14
-
15
- ```
16
- npm install cjs-module-lexer
17
- ```
18
-
19
- For use in CommonJS:
20
-
21
- ```js
22
- const { parse } = require('cjs-module-lexer');
23
-
24
- // `init` return a promise for parity with the ESM API, but you do not have to call it
25
-
26
- const { exports, reexports } = parse(`
27
- // named exports detection
28
- module.exports.a = 'a';
29
- (function () {
30
- exports.b = 'b';
31
- })();
32
- Object.defineProperty(exports, 'c', { value: 'c' });
33
- /* exports.d = 'not detected'; */
34
-
35
- // reexports detection
36
- if (maybe) module.exports = require('./dep1.js');
37
- if (another) module.exports = require('./dep2.js');
38
-
39
- // literal exports assignments
40
- module.exports = { a, b: c, d, 'e': f }
41
-
42
- // __esModule detection
43
- Object.defineProperty(module.exports, '__esModule', { value: true })
44
- `);
45
-
46
- // exports === ['a', 'b', 'c', '__esModule']
47
- // reexports === ['./dep1.js', './dep2.js']
48
- ```
49
-
50
- When using the ESM version, Wasm is supported instead:
51
-
52
- ```js
53
- import { parse, init } from 'cjs-module-lexer';
54
- // init needs to be called and waited upon
55
- await init();
56
- const { exports, reexports } = parse(source);
57
- ```
58
-
59
- The Wasm build is around 1.5x faster and without a cold start.
60
-
61
- ### Grammar
62
-
63
- CommonJS exports matches are run against the source token stream.
64
-
65
- The token grammar is:
66
-
67
- ```
68
- IDENTIFIER: As defined by ECMA-262, without support for identifier `\` escapes, filtered to remove strict reserved words:
69
- "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "enum"
70
-
71
- STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal.
72
-
73
- MODULE_EXPORTS: `module` `.` `exports`
74
-
75
- EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports`
76
-
77
- EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER `.` IDENTIFIER `=`
78
-
79
- EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` STRING_LITERAL `]` `=`
80
-
81
- EXPORTS_LITERAL_PROP: (IDENTIFIER (`:` IDENTIFIER)?) | (STRING_LITERAL `:` IDENTIFIER)
82
-
83
- EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE)
84
-
85
- EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN
86
-
87
- EXPORTS_DEFINE: `Object` `.` `defineProperty `(` EXPORTS_IDENFITIER `,` STRING_LITERAL
88
-
89
- EXPORTS_DEFINE_VALUE: EXPORTS_DEFINE `, {`
90
- (`enumerable: true,`)?
91
- (
92
- `value:` |
93
- `get` (`: function` IDENTIFIER? )? `() {` return IDENTIFIER (`.` IDENTIFIER | `[` STRING_LITERAL `]`)? `;`? `}` `,`?
94
- )
95
- `})`
96
-
97
- EXPORTS_LITERAL: MODULE_EXPORTS `=` `{` (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) `,`)+ `}`
98
-
99
- REQUIRE: `require` `(` STRING_LITERAL `)`
100
-
101
- EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` (`_interopRequireWildcard (`)? REQUIRE
102
-
103
- MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS `=` REQUIRE
104
-
105
- EXPORT_STAR: (`__export` | `__exportStar`) `(` REQUIRE
106
-
107
- EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2 `) {`
108
- (
109
- (
110
- `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`?
111
- (
112
- (`if (Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)) return` `;`?)?
113
- (`if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`)?
114
- )?
115
- ) |
116
- `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) (`&& !` (`Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)` | IDENTIFIER `.hasOwnProperty(` IDENTIFIER$2 `)`))? `)`
117
- )
118
- (
119
- EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? |
120
- `Object.defineProperty(` EXPORTS_IDENTIFIER `, ` IDENTIFIER$2 `, { enumerable: true, get` (`: function` IDENTIFIER? )? `() { return ` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? `}` `,`? `})` `;`?
121
- )
122
- `})`
123
- ```
124
-
125
- Spacing between tokens is taken to be any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment.
126
-
127
- * The returned export names are taken to be the combination of:
128
- 1. All `IDENTIFIER` and `STRING_LITERAL` slots for `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
129
- 2. The first `STRING_LITERAL` slot for all `EXPORTS_DEFINE_VALUE` matches where that same string is not an `EXPORTS_DEFINE` match that is not also an `EXPORTS_DEFINE_VALUE` match.
130
- * The reexport specifiers are taken to be the combination of:
131
- 1. The `REQUIRE` matches of the last matched of either `MODULE_EXPORTS_ASSIGN` or `EXPORTS_LITERAL`.
132
- 2. All _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`.
133
-
134
- ### Parsing Examples
135
-
136
- #### Named Exports Parsing
137
-
138
- The basic matching rules for named exports are `exports.name`, `exports['name']` or `Object.defineProperty(exports, 'name', ...)`. This matching is done without scope analysis and regardless of the expression position:
139
-
140
- ```js
141
- // DETECTS EXPORTS: a, b
142
- (function (exports) {
143
- exports.a = 'a';
144
- exports['b'] = 'b';
145
- })(exports);
146
- ```
147
-
148
- Because there is no scope analysis, the above detection may overclassify:
149
-
150
- ```js
151
- // DETECTS EXPORTS: a, b, c
152
- (function (exports, Object) {
153
- exports.a = 'a';
154
- exports['b'] = 'b';
155
- if (false)
156
- exports.c = 'c';
157
- })(NOT_EXPORTS, NOT_OBJECT);
158
- ```
159
-
160
- It will in turn underclassify in cases where the identifiers are renamed:
161
-
162
- ```js
163
- // DETECTS: NO EXPORTS
164
- (function (e) {
165
- e.a = 'a';
166
- e['b'] = 'b';
167
- })(exports);
168
- ```
169
-
170
- #### Getter Exports Parsing
171
-
172
- `Object.defineProperty` is detected for specifically value and getter forms returning an identifier or member expression:
173
-
174
- ```js
175
- // DETECTS: a, b, c, d, __esModule
176
- Object.defineProperty(exports, 'a', {
177
- enumerable: true,
178
- get: function () {
179
- return q.p;
180
- }
181
- });
182
- Object.defineProperty(exports, 'b', {
183
- enumerable: true,
184
- get: function () {
185
- return q['p'];
186
- }
187
- });
188
- Object.defineProperty(exports, 'c', {
189
- enumerable: true,
190
- get () {
191
- return b;
192
- }
193
- });
194
- Object.defineProperty(exports, 'd', { value: 'd' });
195
- Object.defineProperty(exports, '__esModule', { value: true });
196
- ```
197
-
198
- Value properties are also detected specifically:
199
-
200
- ```js
201
- Object.defineProperty(exports, 'a', {
202
- value: 'no problem'
203
- });
204
- ```
205
-
206
- To avoid matching getters that have side effects, any getter for an export name that does not support the forms above will
207
- opt-out of the getter matching:
208
-
209
- ```js
210
- // DETECTS: NO EXPORTS
211
- Object.defineProperty(exports, 'a', {
212
- get () {
213
- return 'nope';
214
- }
215
- });
216
-
217
- if (false) {
218
- Object.defineProperty(module.exports, 'a', {
219
- get () {
220
- return dynamic();
221
- }
222
- })
223
- }
224
- ```
225
-
226
- Alternative object definition structures or getter function bodies are not detected:
227
-
228
- ```js
229
- // DETECTS: NO EXPORTS
230
- Object.defineProperty(exports, 'a', {
231
- enumerable: false,
232
- get () {
233
- return p;
234
- }
235
- });
236
- Object.defineProperty(exports, 'b', {
237
- configurable: true,
238
- get () {
239
- return p;
240
- }
241
- });
242
- Object.defineProperty(exports, 'c', {
243
- get: () => p
244
- });
245
- Object.defineProperty(exports, 'd', {
246
- enumerable: true,
247
- get: function () {
248
- return dynamic();
249
- }
250
- });
251
- Object.defineProperty(exports, 'e', {
252
- enumerable: true,
253
- get () {
254
- return 'str';
255
- }
256
- });
257
- ```
258
-
259
- `Object.defineProperties` is also not supported.
260
-
261
- #### Exports Object Assignment
262
-
263
- A best-effort is made to detect `module.exports` object assignments, but because this is not a full parser, arbitrary expressions are not handled in the
264
- object parsing process.
265
-
266
- Simple object definitions are supported:
267
-
268
- ```js
269
- // DETECTS EXPORTS: a, b, c
270
- module.exports = {
271
- a,
272
- 'b': b,
273
- c: c,
274
- ...d
275
- };
276
- ```
277
-
278
- Object properties that are not identifiers or string expressions will bail out of the object detection, while spreads are ignored:
279
-
280
- ```js
281
- // DETECTS EXPORTS: a, b
282
- module.exports = {
283
- a,
284
- ...d,
285
- b: require('c'),
286
- c: "not detected since require('c') above bails the object detection"
287
- }
288
- ```
289
-
290
- `Object.defineProperties` is not currently supported either.
291
-
292
- #### module.exports reexport assignment
293
-
294
- Any `module.exports = require('mod')` assignment is detected as a reexport, but only the last one is returned:
295
-
296
- ```js
297
- // DETECTS REEXPORTS: c
298
- module.exports = require('a');
299
- (module => module.exports = require('b'))(NOT_MODULE);
300
- if (false) module.exports = require('c');
301
- ```
302
-
303
- This is to avoid over-classification in Webpack bundles with externals which include `module.exports = require('external')` in their source for every external dependency.
304
-
305
- In exports object assignment, any spread of `require()` are detected as multiple separate reexports:
306
-
307
- ```js
308
- // DETECTS REEXPORTS: a, b
309
- module.exports = require('ignored');
310
- module.exports = {
311
- ...require('a'),
312
- ...require('b')
313
- };
314
- ```
315
-
316
- #### Transpiler Re-exports
317
-
318
- For named exports, transpiler output works well with the rules described above.
319
-
320
- But for star re-exports, special care is taken to support common patterns of transpiler outputs from Babel and TypeScript as well as bundlers like RollupJS.
321
- These reexport and star reexport patterns are restricted to only be detected at the top-level as provided by the direct output of these tools.
322
-
323
- For example, `export * from 'external'` is output by Babel as:
324
-
325
- ```js
326
- "use strict";
327
-
328
- exports.__esModule = true;
329
-
330
- var _external = require("external");
331
-
332
- Object.keys(_external).forEach(function (key) {
333
- if (key === "default" || key === "__esModule") return;
334
- exports[key] = _external[key];
335
- });
336
- ```
337
-
338
- Where the `var _external = require("external")` is specifically detected as well as the `Object.keys(_external)` statement, down to the exact
339
- for of that entire expression including minor variations of the output. The `_external` and `key` identifiers are carefully matched in this
340
- detection.
341
-
342
- Similarly for TypeScript, `export * from 'external'` is output as:
343
-
344
- ```js
345
- "use strict";
346
- function __export(m) {
347
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
348
- }
349
- Object.defineProperty(exports, "__esModule", { value: true });
350
- __export(require("external"));
351
- ```
352
-
353
- Where the `__export(require("external"))` statement is explicitly detected as a reexport, including variations `tslib.__export` and `__exportStar`.
354
-
355
- ### Environment Support
356
-
357
- Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).
358
-
359
- ### JS Grammar Support
360
-
361
- * Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators.
362
- * Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking.
363
- * Always correctly parses valid JS source, but may parse invalid JS source without errors.
364
-
365
- ### Benchmarks
366
-
367
- Benchmarks can be run with `npm run bench`.
368
-
369
- Current results:
370
-
371
- JS Build:
372
-
373
- ```
374
- Module load time
375
- > 4ms
376
- Cold Run, All Samples
377
- test/samples/*.js (3635 KiB)
378
- > 299ms
379
-
380
- Warm Runs (average of 25 runs)
381
- test/samples/angular.js (1410 KiB)
382
- > 13.96ms
383
- test/samples/angular.min.js (303 KiB)
384
- > 4.72ms
385
- test/samples/d3.js (553 KiB)
386
- > 6.76ms
387
- test/samples/d3.min.js (250 KiB)
388
- > 4ms
389
- test/samples/magic-string.js (34 KiB)
390
- > 0.64ms
391
- test/samples/magic-string.min.js (20 KiB)
392
- > 0ms
393
- test/samples/rollup.js (698 KiB)
394
- > 8.48ms
395
- test/samples/rollup.min.js (367 KiB)
396
- > 5.36ms
397
-
398
- Warm Runs, All Samples (average of 25 runs)
399
- test/samples/*.js (3635 KiB)
400
- > 40.28ms
401
- ```
402
-
403
- Wasm Build:
404
- ```
405
- Module load time
406
- > 10ms
407
- Cold Run, All Samples
408
- test/samples/*.js (3635 KiB)
409
- > 43ms
410
-
411
- Warm Runs (average of 25 runs)
412
- test/samples/angular.js (1410 KiB)
413
- > 9.32ms
414
- test/samples/angular.min.js (303 KiB)
415
- > 3.16ms
416
- test/samples/d3.js (553 KiB)
417
- > 5ms
418
- test/samples/d3.min.js (250 KiB)
419
- > 2.32ms
420
- test/samples/magic-string.js (34 KiB)
421
- > 0.16ms
422
- test/samples/magic-string.min.js (20 KiB)
423
- > 0ms
424
- test/samples/rollup.js (698 KiB)
425
- > 6.28ms
426
- test/samples/rollup.min.js (367 KiB)
427
- > 3.6ms
428
-
429
- Warm Runs, All Samples (average of 25 runs)
430
- test/samples/*.js (3635 KiB)
431
- > 27.76ms
432
- ```
433
-
434
- ### Wasm Build Steps
435
-
436
- To build download the WASI SDK from https://github.com/WebAssembly/wasi-sdk/releases.
437
-
438
- The Makefile assumes the existence of "wasi-sdk-11.0" and "wabt" (optional) as sibling folders to this project.
439
-
440
- The build through the Makefile is then run via `make lib/lexer.wasm`, which can also be triggered via `npm run build-wasm` to create `dist/lexer.js`.
441
-
442
- On Windows it may be preferable to use the Linux subsystem.
443
-
444
- After the Web Assembly build, the CJS build can be triggered via `npm run build`.
445
-
446
- Optimization passes are run with [Binaryen](https://github.com/WebAssembly/binaryen) prior to publish to reduce the Web Assembly footprint.
447
-
448
- ### License
449
-
450
- MIT
451
-
452
- [travis-url]: https://travis-ci.org/guybedford/es-module-lexer
453
- [travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master
1
+ # CJS Module Lexer
2
+
3
+ [![Build Status][travis-image]][travis-url]
4
+
5
+ A [very fast](#benchmarks) JS CommonJS module syntax lexer used to detect the most likely list of named exports of a CommonJS module.
6
+
7
+ Outputs the list of named exports (`exports.name = ...`) and possible module reexports (`module.exports = require('...')`), including the common transpiler variations of these cases.
8
+
9
+ Forked from https://github.com/guybedford/es-module-lexer.
10
+
11
+ _Comprehensively handles the JS language grammar while remaining small and fast. - ~90ms per MB of JS cold and ~15ms per MB of JS warm, [see benchmarks](#benchmarks) for more info._
12
+
13
+ ### Project Status
14
+
15
+ This project is used in Node.js core for detecting the named exports available when importing a CJS module into ESM, and is maintained for this purpose.
16
+
17
+ PRs will be accepted and upstreamed for parser bugs, performance improvements or new syntax support only.
18
+
19
+ _Detection patterns for this project are **frozen**_. This is because adding any new export detection patterns would result in fragmented backwards-compatibility. Specifically, it would be very difficult to figure out why an ES module named export for CommonJS might work in newer Node.js versions but not older versions. This problem would only be discovered downstream of module authors, with the fix for module authors being to then have to understand which patterns in this project provide full backwards-compatibily. Rather, by fully freezing the detected patterns, if it works in any Node.js version it will work in any other. Build tools can also reliably treat the supported syntax for this project as a part of their output target for ensuring syntax support.
20
+
21
+ ### Usage
22
+
23
+ ```
24
+ npm install cjs-module-lexer
25
+ ```
26
+
27
+ For use in CommonJS:
28
+
29
+ ```js
30
+ const { parse } = require('cjs-module-lexer');
31
+
32
+ // `init` return a promise for parity with the ESM API, but you do not have to call it
33
+
34
+ const { exports, reexports } = parse(`
35
+ // named exports detection
36
+ module.exports.a = 'a';
37
+ (function () {
38
+ exports.b = 'b';
39
+ })();
40
+ Object.defineProperty(exports, 'c', { value: 'c' });
41
+ /* exports.d = 'not detected'; */
42
+
43
+ // reexports detection
44
+ if (maybe) module.exports = require('./dep1.js');
45
+ if (another) module.exports = require('./dep2.js');
46
+
47
+ // literal exports assignments
48
+ module.exports = { a, b: c, d, 'e': f }
49
+
50
+ // __esModule detection
51
+ Object.defineProperty(module.exports, '__esModule', { value: true })
52
+ `);
53
+
54
+ // exports === ['a', 'b', 'c', '__esModule']
55
+ // reexports === ['./dep1.js', './dep2.js']
56
+ ```
57
+
58
+ When using the ESM version, Wasm is supported instead:
59
+
60
+ ```js
61
+ import { parse, init } from 'cjs-module-lexer';
62
+ // init needs to be called and waited upon
63
+ await init();
64
+ const { exports, reexports } = parse(source);
65
+ ```
66
+
67
+ The Wasm build is around 1.5x faster and without a cold start.
68
+
69
+ ### Grammar
70
+
71
+ CommonJS exports matches are run against the source token stream.
72
+
73
+ The token grammar is:
74
+
75
+ ```
76
+ IDENTIFIER: As defined by ECMA-262, without support for identifier `\` escapes, filtered to remove strict reserved words:
77
+ "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "enum"
78
+
79
+ STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal.
80
+
81
+ MODULE_EXPORTS: `module` `.` `exports`
82
+
83
+ EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports`
84
+
85
+ EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER `.` IDENTIFIER `=`
86
+
87
+ EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` STRING_LITERAL `]` `=`
88
+
89
+ EXPORTS_LITERAL_PROP: (IDENTIFIER (`:` IDENTIFIER)?) | (STRING_LITERAL `:` IDENTIFIER)
90
+
91
+ EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE)
92
+
93
+ EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN
94
+
95
+ EXPORTS_DEFINE: `Object` `.` `defineProperty `(` EXPORTS_IDENFITIER `,` STRING_LITERAL
96
+
97
+ EXPORTS_DEFINE_VALUE: EXPORTS_DEFINE `, {`
98
+ (`enumerable: true,`)?
99
+ (
100
+ `value:` |
101
+ `get` (`: function` IDENTIFIER? )? `() {` return IDENTIFIER (`.` IDENTIFIER | `[` STRING_LITERAL `]`)? `;`? `}` `,`?
102
+ )
103
+ `})`
104
+
105
+ EXPORTS_LITERAL: MODULE_EXPORTS `=` `{` (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) `,`)+ `}`
106
+
107
+ REQUIRE: `require` `(` STRING_LITERAL `)`
108
+
109
+ EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` (`_interopRequireWildcard (`)? REQUIRE
110
+
111
+ MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS `=` REQUIRE
112
+
113
+ EXPORT_STAR: (`__export` | `__exportStar`) `(` REQUIRE
114
+
115
+ EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2 `) {`
116
+ (
117
+ (
118
+ `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`?
119
+ (
120
+ (`if (Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)) return` `;`?)?
121
+ (`if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`)?
122
+ )?
123
+ ) |
124
+ `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) (`&& !` (`Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)` | IDENTIFIER `.hasOwnProperty(` IDENTIFIER$2 `)`))? `)`
125
+ )
126
+ (
127
+ EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? |
128
+ `Object.defineProperty(` EXPORTS_IDENTIFIER `, ` IDENTIFIER$2 `, { enumerable: true, get` (`: function` IDENTIFIER? )? `() { return ` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? `}` `,`? `})` `;`?
129
+ )
130
+ `})`
131
+ ```
132
+
133
+ Spacing between tokens is taken to be any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment.
134
+
135
+ * The returned export names are taken to be the combination of:
136
+ 1. All `IDENTIFIER` and `STRING_LITERAL` slots for `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
137
+ 2. The first `STRING_LITERAL` slot for all `EXPORTS_DEFINE_VALUE` matches where that same string is not an `EXPORTS_DEFINE` match that is not also an `EXPORTS_DEFINE_VALUE` match.
138
+ * The reexport specifiers are taken to be the combination of:
139
+ 1. The `REQUIRE` matches of the last matched of either `MODULE_EXPORTS_ASSIGN` or `EXPORTS_LITERAL`.
140
+ 2. All _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`.
141
+
142
+ ### Parsing Examples
143
+
144
+ #### Named Exports Parsing
145
+
146
+ The basic matching rules for named exports are `exports.name`, `exports['name']` or `Object.defineProperty(exports, 'name', ...)`. This matching is done without scope analysis and regardless of the expression position:
147
+
148
+ ```js
149
+ // DETECTS EXPORTS: a, b
150
+ (function (exports) {
151
+ exports.a = 'a';
152
+ exports['b'] = 'b';
153
+ })(exports);
154
+ ```
155
+
156
+ Because there is no scope analysis, the above detection may overclassify:
157
+
158
+ ```js
159
+ // DETECTS EXPORTS: a, b, c
160
+ (function (exports, Object) {
161
+ exports.a = 'a';
162
+ exports['b'] = 'b';
163
+ if (false)
164
+ exports.c = 'c';
165
+ })(NOT_EXPORTS, NOT_OBJECT);
166
+ ```
167
+
168
+ It will in turn underclassify in cases where the identifiers are renamed:
169
+
170
+ ```js
171
+ // DETECTS: NO EXPORTS
172
+ (function (e) {
173
+ e.a = 'a';
174
+ e['b'] = 'b';
175
+ })(exports);
176
+ ```
177
+
178
+ #### Getter Exports Parsing
179
+
180
+ `Object.defineProperty` is detected for specifically value and getter forms returning an identifier or member expression:
181
+
182
+ ```js
183
+ // DETECTS: a, b, c, d, __esModule
184
+ Object.defineProperty(exports, 'a', {
185
+ enumerable: true,
186
+ get: function () {
187
+ return q.p;
188
+ }
189
+ });
190
+ Object.defineProperty(exports, 'b', {
191
+ enumerable: true,
192
+ get: function () {
193
+ return q['p'];
194
+ }
195
+ });
196
+ Object.defineProperty(exports, 'c', {
197
+ enumerable: true,
198
+ get () {
199
+ return b;
200
+ }
201
+ });
202
+ Object.defineProperty(exports, 'd', { value: 'd' });
203
+ Object.defineProperty(exports, '__esModule', { value: true });
204
+ ```
205
+
206
+ Value properties are also detected specifically:
207
+
208
+ ```js
209
+ Object.defineProperty(exports, 'a', {
210
+ value: 'no problem'
211
+ });
212
+ ```
213
+
214
+ To avoid matching getters that have side effects, any getter for an export name that does not support the forms above will
215
+ opt-out of the getter matching:
216
+
217
+ ```js
218
+ // DETECTS: NO EXPORTS
219
+ Object.defineProperty(exports, 'a', {
220
+ get () {
221
+ return 'nope';
222
+ }
223
+ });
224
+
225
+ if (false) {
226
+ Object.defineProperty(module.exports, 'a', {
227
+ get () {
228
+ return dynamic();
229
+ }
230
+ })
231
+ }
232
+ ```
233
+
234
+ Alternative object definition structures or getter function bodies are not detected:
235
+
236
+ ```js
237
+ // DETECTS: NO EXPORTS
238
+ Object.defineProperty(exports, 'a', {
239
+ enumerable: false,
240
+ get () {
241
+ return p;
242
+ }
243
+ });
244
+ Object.defineProperty(exports, 'b', {
245
+ configurable: true,
246
+ get () {
247
+ return p;
248
+ }
249
+ });
250
+ Object.defineProperty(exports, 'c', {
251
+ get: () => p
252
+ });
253
+ Object.defineProperty(exports, 'd', {
254
+ enumerable: true,
255
+ get: function () {
256
+ return dynamic();
257
+ }
258
+ });
259
+ Object.defineProperty(exports, 'e', {
260
+ enumerable: true,
261
+ get () {
262
+ return 'str';
263
+ }
264
+ });
265
+ ```
266
+
267
+ `Object.defineProperties` is also not supported.
268
+
269
+ #### Exports Object Assignment
270
+
271
+ A best-effort is made to detect `module.exports` object assignments, but because this is not a full parser, arbitrary expressions are not handled in the
272
+ object parsing process.
273
+
274
+ Simple object definitions are supported:
275
+
276
+ ```js
277
+ // DETECTS EXPORTS: a, b, c
278
+ module.exports = {
279
+ a,
280
+ 'b': b,
281
+ c: c,
282
+ ...d
283
+ };
284
+ ```
285
+
286
+ Object properties that are not identifiers or string expressions will bail out of the object detection, while spreads are ignored:
287
+
288
+ ```js
289
+ // DETECTS EXPORTS: a, b
290
+ module.exports = {
291
+ a,
292
+ ...d,
293
+ b: require('c'),
294
+ c: "not detected since require('c') above bails the object detection"
295
+ }
296
+ ```
297
+
298
+ `Object.defineProperties` is not currently supported either.
299
+
300
+ #### module.exports reexport assignment
301
+
302
+ Any `module.exports = require('mod')` assignment is detected as a reexport, but only the last one is returned:
303
+
304
+ ```js
305
+ // DETECTS REEXPORTS: c
306
+ module.exports = require('a');
307
+ (module => module.exports = require('b'))(NOT_MODULE);
308
+ if (false) module.exports = require('c');
309
+ ```
310
+
311
+ This is to avoid over-classification in Webpack bundles with externals which include `module.exports = require('external')` in their source for every external dependency.
312
+
313
+ In exports object assignment, any spread of `require()` are detected as multiple separate reexports:
314
+
315
+ ```js
316
+ // DETECTS REEXPORTS: a, b
317
+ module.exports = require('ignored');
318
+ module.exports = {
319
+ ...require('a'),
320
+ ...require('b')
321
+ };
322
+ ```
323
+
324
+ #### Transpiler Re-exports
325
+
326
+ For named exports, transpiler output works well with the rules described above.
327
+
328
+ But for star re-exports, special care is taken to support common patterns of transpiler outputs from Babel and TypeScript as well as bundlers like RollupJS.
329
+ These reexport and star reexport patterns are restricted to only be detected at the top-level as provided by the direct output of these tools.
330
+
331
+ For example, `export * from 'external'` is output by Babel as:
332
+
333
+ ```js
334
+ "use strict";
335
+
336
+ exports.__esModule = true;
337
+
338
+ var _external = require("external");
339
+
340
+ Object.keys(_external).forEach(function (key) {
341
+ if (key === "default" || key === "__esModule") return;
342
+ exports[key] = _external[key];
343
+ });
344
+ ```
345
+
346
+ Where the `var _external = require("external")` is specifically detected as well as the `Object.keys(_external)` statement, down to the exact
347
+ for of that entire expression including minor variations of the output. The `_external` and `key` identifiers are carefully matched in this
348
+ detection.
349
+
350
+ Similarly for TypeScript, `export * from 'external'` is output as:
351
+
352
+ ```js
353
+ "use strict";
354
+ function __export(m) {
355
+ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
356
+ }
357
+ Object.defineProperty(exports, "__esModule", { value: true });
358
+ __export(require("external"));
359
+ ```
360
+
361
+ Where the `__export(require("external"))` statement is explicitly detected as a reexport, including variations `tslib.__export` and `__exportStar`.
362
+
363
+ ### Environment Support
364
+
365
+ Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).
366
+
367
+ ### JS Grammar Support
368
+
369
+ * Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators.
370
+ * Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking.
371
+ * Always correctly parses valid JS source, but may parse invalid JS source without errors.
372
+
373
+ ### Benchmarks
374
+
375
+ Benchmarks can be run with `npm run bench`.
376
+
377
+ Current results:
378
+
379
+ JS Build:
380
+
381
+ ```
382
+ Module load time
383
+ > 4ms
384
+ Cold Run, All Samples
385
+ test/samples/*.js (3635 KiB)
386
+ > 299ms
387
+
388
+ Warm Runs (average of 25 runs)
389
+ test/samples/angular.js (1410 KiB)
390
+ > 13.96ms
391
+ test/samples/angular.min.js (303 KiB)
392
+ > 4.72ms
393
+ test/samples/d3.js (553 KiB)
394
+ > 6.76ms
395
+ test/samples/d3.min.js (250 KiB)
396
+ > 4ms
397
+ test/samples/magic-string.js (34 KiB)
398
+ > 0.64ms
399
+ test/samples/magic-string.min.js (20 KiB)
400
+ > 0ms
401
+ test/samples/rollup.js (698 KiB)
402
+ > 8.48ms
403
+ test/samples/rollup.min.js (367 KiB)
404
+ > 5.36ms
405
+
406
+ Warm Runs, All Samples (average of 25 runs)
407
+ test/samples/*.js (3635 KiB)
408
+ > 40.28ms
409
+ ```
410
+
411
+ Wasm Build:
412
+ ```
413
+ Module load time
414
+ > 10ms
415
+ Cold Run, All Samples
416
+ test/samples/*.js (3635 KiB)
417
+ > 43ms
418
+
419
+ Warm Runs (average of 25 runs)
420
+ test/samples/angular.js (1410 KiB)
421
+ > 9.32ms
422
+ test/samples/angular.min.js (303 KiB)
423
+ > 3.16ms
424
+ test/samples/d3.js (553 KiB)
425
+ > 5ms
426
+ test/samples/d3.min.js (250 KiB)
427
+ > 2.32ms
428
+ test/samples/magic-string.js (34 KiB)
429
+ > 0.16ms
430
+ test/samples/magic-string.min.js (20 KiB)
431
+ > 0ms
432
+ test/samples/rollup.js (698 KiB)
433
+ > 6.28ms
434
+ test/samples/rollup.min.js (367 KiB)
435
+ > 3.6ms
436
+
437
+ Warm Runs, All Samples (average of 25 runs)
438
+ test/samples/*.js (3635 KiB)
439
+ > 27.76ms
440
+ ```
441
+
442
+ ### Wasm Build Steps
443
+
444
+ To build download the WASI SDK from https://github.com/WebAssembly/wasi-sdk/releases.
445
+
446
+ The Makefile assumes the existence of "wasi-sdk-11.0" and "wabt" (optional) as sibling folders to this project.
447
+
448
+ The build through the Makefile is then run via `make lib/lexer.wasm`, which can also be triggered via `npm run build-wasm` to create `dist/lexer.js`.
449
+
450
+ On Windows it may be preferable to use the Linux subsystem.
451
+
452
+ After the Web Assembly build, the CJS build can be triggered via `npm run build`.
453
+
454
+ Optimization passes are run with [Binaryen](https://github.com/WebAssembly/binaryen) prior to publish to reduce the Web Assembly footprint.
455
+
456
+ ### License
457
+
458
+ MIT
459
+
460
+ [travis-url]: https://travis-ci.org/guybedford/es-module-lexer
461
+ [travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master