From 1e8cb08edcbbfe01e7ef186a09d4781b33b490cc Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 14 May 2020 22:40:37 -0700 Subject: [PATCH] module: named exports for CJS via static analysis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/35249 Reviewed-By: Mary Marchini Reviewed-By: Geoffrey Booth Reviewed-By: Matteo Collina Reviewed-By: Myles Borins Reviewed-By: Michaël Zasso Reviewed-By: Zeyu Yang Reviewed-By: Richard Lau --- LICENSE | 14 + deps/cjs-module-lexer/.gitignore | 11 + deps/cjs-module-lexer/LICENSE | 10 + deps/cjs-module-lexer/README.md | 331 ++++++ deps/cjs-module-lexer/lexer.js | 1152 +++++++++++++++++++ deps/cjs-module-lexer/package.json | 41 + doc/api/esm.md | 137 ++- lib/internal/modules/cjs/loader.js | 38 +- lib/internal/modules/esm/module_job.js | 27 +- lib/internal/modules/esm/translators.js | 101 +- node.gyp | 1 + src/node_native_module.cc | 6 +- test/es-module/test-esm-cjs-exports.js | 21 + test/es-module/test-esm-cjs-named-error.mjs | 42 +- test/fixtures/es-modules/cjs-exports.mjs | 34 + test/fixtures/es-modules/exports-cases.js | 7 + test/fixtures/es-modules/exports-cases2.js | 29 + test/fixtures/es-modules/exports-cases3.js | 25 + test/parallel/test-bootstrap-modules.js | 1 + tools/license-builder.sh | 1 + 20 files changed, 1935 insertions(+), 94 deletions(-) create mode 100755 deps/cjs-module-lexer/.gitignore create mode 100755 deps/cjs-module-lexer/LICENSE create mode 100755 deps/cjs-module-lexer/README.md create mode 100755 deps/cjs-module-lexer/lexer.js create mode 100755 deps/cjs-module-lexer/package.json create mode 100644 test/es-module/test-esm-cjs-exports.js create mode 100644 test/fixtures/es-modules/cjs-exports.mjs create mode 100644 test/fixtures/es-modules/exports-cases.js create mode 100644 test/fixtures/es-modules/exports-cases2.js create mode 100644 test/fixtures/es-modules/exports-cases3.js diff --git a/LICENSE b/LICENSE index 12114764d26..d0674bb7eb6 100644 --- a/LICENSE +++ b/LICENSE @@ -114,6 +114,20 @@ The externally maintained libraries used by Node.js are: purpose. It is provided "as is" without express or implied warranty. """ +- cjs-module-lexer, located at deps/cjs-module-lexer, is licensed as follows: + """ + MIT License + ----------- + + Copyright (C) 2018-2020 Guy Bedford + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + """ + - ICU, located at deps/icu-small, is licensed as follows: """ COPYRIGHT AND PERMISSION NOTICE (ICU 58 and later) diff --git a/deps/cjs-module-lexer/.gitignore b/deps/cjs-module-lexer/.gitignore new file mode 100755 index 00000000000..55ee2f0d6cf --- /dev/null +++ b/deps/cjs-module-lexer/.gitignore @@ -0,0 +1,11 @@ +node_modules +*.lock +test +.* +Makefile +bench +build.js +include-wasm +include +lib +src diff --git a/deps/cjs-module-lexer/LICENSE b/deps/cjs-module-lexer/LICENSE new file mode 100755 index 00000000000..935b357962d --- /dev/null +++ b/deps/cjs-module-lexer/LICENSE @@ -0,0 +1,10 @@ +MIT License +----------- + +Copyright (C) 2018-2020 Guy Bedford + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/deps/cjs-module-lexer/README.md b/deps/cjs-module-lexer/README.md new file mode 100755 index 00000000000..726dd407769 --- /dev/null +++ b/deps/cjs-module-lexer/README.md @@ -0,0 +1,331 @@ +# CJS Module Lexer + +[![Build Status][travis-image]][travis-url] + +A [very fast](#benchmarks) JS CommonJS module syntax lexer used to detect the most likely list of named exports of a CommonJS module. + +Outputs the list of named exports (`exports.name = ...`) and possible module reexports (`module.exports = require('...')`), including the common transpiler variations of these cases. + +Forked from https://github.com/guybedford/es-module-lexer. + +_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._ + +### Usage + +``` +npm install cjs-module-lexer +``` + +For use in CommonJS: + +```js +const parse = require('cjs-module-lexer'); + +const { exports, reexports } = parse(` + // named exports detection + module.exports.a = 'a'; + (function () { + exports.b = 'b'; + })(); + Object.defineProperty(exports, 'c', { value: 'c' }); + /* exports.d = 'not detected'; */ + + // reexports detection + if (maybe) module.exports = require('./dep1.js'); + if (another) module.exports = require('./dep2.js'); + + // literal exports assignments + module.exports = { a, b: c, d, 'e': f } + + // __esModule detection + Object.defineProperty(module.exports, '__esModule', { value: true }) +`); + +// exports === ['a', 'b', 'c', '__esModule'] +// reexports === ['./dep1.js', './dep2.js'] +``` + +When using the ESM version, Wasm is supported instead: + +```js +import { parse, init } from 'cjs-module-lexer'; +// init needs to be called and waited upon +await init(); +const { exports, reexports } = parse(source); +``` + +The Wasm build is around 1.5x faster and without a cold start. + +### Grammar + +CommonJS exports matches are run against the source token stream. + +The token grammar is: + +``` +IDENTIFIER: As defined by ECMA-262, without support for identifier `\` escapes, filtered to remove strict reserved words: + "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "enum" + +STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal. + +IDENTIFIER_STRING: ( `"` IDENTIFIER `"` | `'` IDENTIFIER `'` ) + +COMMENT_SPACE: Any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment + +MODULE_EXPORTS: `module` COMMENT_SPACE `.` COMMENT_SPACE `exports` + +EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports` + +EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER COMMENT_SPACE `.` COMMENT_SPACE IDENTIFIER COMMENT_SPACE `=` + +EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER COMMENT_SPACE `[` COMMENT_SPACE IDENTIFIER_STRING COMMENT_SPACE `]` COMMENT_SPACE `=` + +EXPORTS_LITERAL_PROP: (IDENTIFIER (COMMENT_SPACE `:` COMMENT_SPACE IDENTIFIER)?) | (IDENTIFIER_STRING COMMENT_SPACE `:` COMMENT_SPACE IDENTIFIER) + +EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN + +EXPORTS_DEFINE: `Object` COMMENT_SPACE `.` COMMENT_SPACE `defineProperty COMMENT_SPACE `(` EXPORTS_IDENTIFIER COMMENT_SPACE `,` COMMENT_SPACE IDENTIFIER_STRING + +EXPORTS_LITERAL: MODULE_EXPORTS COMMENT_SPACE `=` COMMENT_SPACE `{` COMMENT_SPACE (EXPORTS_LITERAL_PROP COMMENT_SPACE `,` COMMENT_SPACE)+ `}` + +REQUIRE: `require` COMMENT_SPACE `(` COMMENT_SPACE STRING_LITERAL COMMENT_SPACE `)` + +EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` REQUIRE + +MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS COMMENT_SPACE `=` COMMENT_SPACE REQUIRE + +EXPORT_STAR: (`__export` | `__exportStar`) `(` REQUIRE + +EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2 `) {` + ( + `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`? | + `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) `)` + ) + ( + EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? | + `Object.defineProperty(` EXPORTS_IDENTIFIER `, ` IDENTIFIER$2 `, { enumerable: true, get: function () { return ` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? } })` `;`? + ) + `})` +``` + +* The returned export names are the matched `IDENTIFIER` and `IDENTIFIER_STRING` slots for all `EXPORTS_MEMBER`, `EXPORTS_DEFINE` and `EXPORTS_LITERAL` matches. +* The reexport specifiers are taken to be the `STRING_LITERAL` slots of all `MODULE_EXPORTS_ASSIGN` as well as all _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`. + +### Parsing Examples + +#### Named Exports Parsing + +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: + +```js +// DETECTS EXPORTS: a, b, c +(function (exports) { + exports.a = 'a'; + exports['b'] = 'b'; + Object.defineProperty(exports, 'c', { value: 'c' }); +})(exports); +``` + +Because there is no scope analysis, the above detection may overclassify: + +```js +// DETECTS EXPORTS: a, b, c +(function (exports, Object) { + exports.a = 'a'; + exports['b'] = 'b'; + if (false) + Object.defineProperty(exports, 'c', { value: 'c' }); +})(NOT_EXPORTS, NOT_OBJECT); +``` + +It will in turn underclassify in cases where the identifiers are renamed: + +```js +// DETECTS: NO EXPORTS +(function (e, defineProperty) { + e.a = 'a'; + e['b'] = 'b'; + defineProperty(e, 'c', { value: 'c' }); +})(exports, defineProperty); +``` + +#### Exports Object Assignment + +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 +object parsing process. + +Simple object definitions are supported: + +```js +// DETECTS EXPORTS: a, b, c +module.exports = { + a, + b: 'c', + c: c +}; +``` + +Object properties that are not identifiers or string expressions will bail out of the object detection: + +```js +// DETECTS EXPORTS: a, b +module.exports = { + a, + b: require('c'), + c: "not detected since require('c') above bails the object detection" +} +``` + +`Object.defineProperties` is not currently supported either. + +#### module.exports reexport assignment + +Any `module.exports = require('mod')` assignment is detected as a reexport: + +```js +// DETECTS REEXPORTS: a, b, c +module.exports = require('a'); +(module => module.exports = require('b'))(NOT_MODULE); +if (false) module.exports = require('c'); +``` + +As a result, the total list of exports would be inferred as the union of all of these reexported modules, which can lead to possible over-classification. + +#### Transpiler Re-exports + +For named exports, transpiler output works well with the rules described above. + +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. +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. + +For example, `export * from 'external'` is output by Babel as: + +```js +"use strict"; + +exports.__esModule = true; + +var _external = require("external"); + +Object.keys(_external).forEach(function (key) { + if (key === "default" || key === "__esModule") return; + exports[key] = _external[key]; +}); +``` + +Where the `var _external = require("external")` is specifically detected as well as the `Object.keys(_external)` statement, down to the exact +for of that entire expression including minor variations of the output. The `_external` and `key` identifiers are carefully matched in this +detection. + +Similarly for TypeScript, `export * from 'external'` is output as: + +```js +"use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +Object.defineProperty(exports, "__esModule", { value: true }); +__export(require("external")); +``` + +Where the `__export(require("external"))` statement is explicitly detected as a reexport, including variations `tslib.__export` and `__exportStar`. + +### Environment Support + +Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm). + +### JS Grammar Support + +* Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators. +* Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking. +* Always correctly parses valid JS source, but may parse invalid JS source without errors. + +### Benchmarks + +Benchmarks can be run with `npm run bench`. + +Current results: + +JS Build: + +``` +Module load time +> 2ms +Cold Run, All Samples +test/samples/*.js (3635 KiB) +> 333ms + +Warm Runs (average of 25 runs) +test/samples/angular.js (1410 KiB) +> 16.48ms +test/samples/angular.min.js (303 KiB) +> 5.36ms +test/samples/d3.js (553 KiB) +> 8.32ms +test/samples/d3.min.js (250 KiB) +> 4.28ms +test/samples/magic-string.js (34 KiB) +> 1ms +test/samples/magic-string.min.js (20 KiB) +> 0.36ms +test/samples/rollup.js (698 KiB) +> 10.48ms +test/samples/rollup.min.js (367 KiB) +> 6.64ms + +Warm Runs, All Samples (average of 25 runs) +test/samples/*.js (3635 KiB) +> 49.28ms +``` + +Wasm Build: +``` +Module load time +> 11ms +Cold Run, All Samples +test/samples/*.js (3635 KiB) +> 48ms + +Warm Runs (average of 25 runs) +test/samples/angular.js (1410 KiB) +> 12.32ms +test/samples/angular.min.js (303 KiB) +> 3.76ms +test/samples/d3.js (553 KiB) +> 6.08ms +test/samples/d3.min.js (250 KiB) +> 3ms +test/samples/magic-string.js (34 KiB) +> 0.24ms +test/samples/magic-string.min.js (20 KiB) +> 0ms +test/samples/rollup.js (698 KiB) +> 7.2ms +test/samples/rollup.min.js (367 KiB) +> 4.2ms + +Warm Runs, All Samples (average of 25 runs) +test/samples/*.js (3635 KiB) +> 33.6ms +``` + +### Wasm Build Steps + +To build download the WASI SDK from https://github.com/CraneStation/wasi-sdk/releases. + +The Makefile assumes the existence of "wasi-sdk-10.0", "binaryen" and "wabt" (both optional) as sibling folders to this project. + +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`. + +On Windows it may be preferable to use the Linux subsystem. + +After the Web Assembly build, the CJS build can be triggered via `npm run build`. + +Optimization passes are run with [Binaryen](https://github.com/WebAssembly/binaryen) prior to publish to reduce the Web Assembly footprint. + +### License + +MIT + +[travis-url]: https://travis-ci.org/guybedford/es-module-lexer +[travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master diff --git a/deps/cjs-module-lexer/lexer.js b/deps/cjs-module-lexer/lexer.js new file mode 100755 index 00000000000..6a9eef9edd7 --- /dev/null +++ b/deps/cjs-module-lexer/lexer.js @@ -0,0 +1,1152 @@ +let source, pos, end; +let openTokenDepth, + templateDepth, + lastTokenPos, + lastSlashWasDivision, + templateStack, + templateStackDepth, + openTokenPosStack, + openClassPosStack, + nextBraceIsClass, + starExportMap, + lastStarExportSpecifier, + _exports, + reexports; + +function resetState () { + openTokenDepth = 0; + templateDepth = -1; + lastTokenPos = -1; + lastSlashWasDivision = false; + templateStack = new Array(1024); + templateStackDepth = 0; + openTokenPosStack = new Array(1024); + openClassPosStack = new Array(1024); + nextBraceIsClass = false; + starExportMap = Object.create(null); + lastStarExportSpecifier = null; + + _exports = new Set(); + reexports = new Set(); +} + +const strictReserved = new Set(['implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield', 'enum']); + +module.exports = function parseCJS (source, name = '@') { + resetState(); + try { + parseSource(source); + } + catch (e) { + e.message += `\n at ${name}:${source.slice(0, pos).split('\n').length}:${pos - source.lastIndexOf('\n', pos - 1)}`; + e.loc = pos; + throw e; + } + const result = { exports: [..._exports], reexports: [...reexports] }; + resetState(); + return result; +} + +function addExport (name) { + if (!strictReserved.has(name)) + _exports.add(name); +} + +function parseSource (cjsSource) { + source = cjsSource; + pos = -1; + end = source.length - 1; + let ch = 0; + + // Handle #! + if (source.charCodeAt(0) === 35/*#*/ && source.charCodeAt(1) === 33/*!*/) { + if (source.length === 2) + return true; + pos += 2; + while (pos++ < end) { + ch = source.charCodeAt(pos); + if (ch === 10/*\n*/ || ch === 13/*\r*/) + break; + } + } + + while (pos++ < end) { + ch = source.charCodeAt(pos); + + if (ch === 32 || ch < 14 && ch > 8) + continue; + + if (openTokenDepth === 0) { + switch (ch) { + case 105/*i*/: + if (source.slice(pos + 1, pos + 6) === 'mport' && keywordStart(pos)) + throwIfImportStatement(); + lastTokenPos = pos; + continue; + case 114/*r*/: + const startPos = pos; + if (tryParseRequire(false) && keywordStart(startPos)) + tryBacktrackAddStarExportBinding(startPos - 1); + lastTokenPos = pos; + continue; + case 95/*_*/: + if (source.slice(pos + 1, pos + 8) === '_export' && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) { + pos += 8; + if (source.slice(pos, pos + 4) === 'Star') + pos += 4; + if (source.charCodeAt(pos) === 40/*(*/) { + openTokenPosStack[openTokenDepth++] = lastTokenPos; + if (source.charCodeAt(++pos) === 114/*r*/) + tryParseRequire(true); + } + } + lastTokenPos = pos; + continue; + } + } + + switch (ch) { + case 101/*e*/: + if (source.slice(pos + 1, pos + 6) === 'xport' && keywordStart(pos)) { + if (source.charCodeAt(pos + 6) === 115/*s*/) + tryParseExportsDotAssign(false); + else if (openTokenDepth === 0) + throwIfExportStatement(); + } + break; + case 99/*c*/: + if (keywordStart(pos) && source.slice(pos + 1, pos + 5) === 'lass' && isBrOrWs(source.charCodeAt(pos + 5))) + nextBraceIsClass = true; + break; + case 109/*m*/: + if (source.slice(pos + 1, pos + 6) === 'odule' && keywordStart(pos)) + tryParseModuleExportsDotAssign(); + break; + case 79/*O*/: + if (source.slice(pos + 1, pos + 6) === 'bject' && keywordStart(pos)) + tryParseObjectDefineOrKeys(openTokenDepth === 0); + break; + case 40/*(*/: + openTokenPosStack[openTokenDepth++] = lastTokenPos; + break; + case 41/*)*/: + if (openTokenDepth === 0) + throw new Error('Unexpected closing bracket.'); + openTokenDepth--; + break; + case 123/*{*/: + openClassPosStack[openTokenDepth] = nextBraceIsClass; + nextBraceIsClass = false; + openTokenPosStack[openTokenDepth++] = lastTokenPos; + break; + case 125/*}*/: + if (openTokenDepth === 0) + throw new Error('Unexpected closing brace.'); + if (openTokenDepth-- === templateDepth) { + templateDepth = templateStack[--templateStackDepth]; + templateString(); + } + else { + if (templateDepth !== -1 && openTokenDepth < templateDepth) + throw new Error('Unexpected closing brace.'); + } + break; + case 60/*>*/: + // TODO: -```js -import packageMain from 'commonjs-package'; // Works - -import { method } from 'commonjs-package'; // Errors -``` - -It is also possible to -[import an ES or CommonJS module for its side effects only][]. - ### `import()` expressions [Dynamic `import()`][] is supported in both CommonJS and ES modules. It can be used to include ES module files from CommonJS code. -## CommonJS, JSON, and native modules +## CommonJS Namespaces -CommonJS, JSON, and native modules can be used with -[`module.createRequire()`][]. +CommonJS modules consist of a `module.exports` object which can be of any type. + +When importing a CommonJS module, it can be reliably imported using the ES +module default import or its corresponding sugar syntax: + + +```js +import { default as cjs } from 'cjs'; + +// The following import statement is "syntax sugar" (equivalent but sweeter) +// for `{ default as cjsSugar }` in the above import statement: +import cjsSugar from 'cjs'; + +console.log(cjs); +console.log(cjs === cjsSugar); +// Prints: +// +// true +``` + +The ECMAScript Module Namespace representation of a CommonJS module will always +be a namespace with a `default` export key pointing to the CommonJS +`module.exports` value. + +This Module Namespace Exotic Object can be directly observed either when using +`import * as m from 'cjs'` or a dynamic import: + + +```js +import * as m from 'cjs'; +console.log(m); +console.log(m === await import('cjs')); +// Prints: +// [Module] { default: } +// true +``` + +For better compatibility with existing usage in the JS ecosystem, Node.js will +in addition attempt to determine the CommonJS named exports of every imported +CommonJS module to provide them as separate ES module exports using a static +analysis process. + +For example, a CommonJS module written: ```js // cjs.cjs -module.exports = 'cjs'; - -// esm.mjs -import { createRequire } from 'module'; - -const require = createRequire(import.meta.url); - -const cjs = require('./cjs.cjs'); -cjs === 'cjs'; // true +exports.name = 'exported'; ``` +will support named imports in ES modules: + + +```js +import { name } from './cjs.cjs'; +console.log(name); +// Prints: 'exported' + +import cjs from './cjs.cjs'; +console.log(cjs); +// Prints: { name: 'exported' } + +import * as m from './cjs.cjs'; +console.log(m); +// Prints: [Module] { default: { name: 'exported' }, name: 'exported' } +``` + +As can be seen from the last example of the Module Namespace Exotic Object being +logged, the `name` export is copied off of the `module.exports` object and set +directly on the ES module namespace when the module is imported. + +Live binding updates or new exports added to `module.exports` are not detected +for these named exports. + +The detection of named exports is based on common syntax patterns but will not +always correctly detect named exports, in these cases using the default +import form described above can be a better option. + +Named exports detection covers many common export patterns, reexport patterns +and build tool and transpiler outputs. See [cjs-module-lexer][] for the exact +semantics implemented. + ## Builtin modules [Core modules][] will provide named exports of their public API. A @@ -330,6 +390,24 @@ syncBuiltinESMExports(); fs.readFileSync === readFileSync; ``` +## CommonJS, JSON, and native modules + +CommonJS, JSON, and native modules can be used with +[`module.createRequire()`][]. + +```js +// cjs.cjs +module.exports = 'cjs'; + +// esm.mjs +import { createRequire } from 'module'; + +const require = createRequire(import.meta.url); + +const cjs = require('./cjs.cjs'); +cjs === 'cjs'; // true +``` + ## Experimental JSON modules Currently importing JSON modules are only supported in the `commonjs` mode @@ -1150,6 +1228,7 @@ $ node --experimental-specifier-resolution=node index success! ``` + [CommonJS]: modules.html [Conditional exports]: packages.html#packages_conditional_exports [Dynamic `import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports @@ -1173,7 +1252,7 @@ success! [`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray [`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array [`util.TextDecoder`]: util.html#util_class_util_textdecoder -[import an ES or CommonJS module for its side effects only]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only +[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/0.3.1 [special scheme]: https://url.spec.whatwg.org/#special-scheme [the official standard format]: https://tc39.github.io/ecma262/#sec-modules [transpiler loader example]: #esm_transpiler_loader diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 34507ebdad3..a745c66c614 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -21,12 +21,6 @@ 'use strict'; -// Set first due to cycle with ESM loader functions. -module.exports = { - wrapSafe, Module, toRealPath, readPackageScope, - get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; } -}; - const { ArrayIsArray, ArrayPrototypeJoin, @@ -54,6 +48,15 @@ const { StringPrototypeStartsWith, } = primordials; +// Map used to store CJS parsing data. +const cjsParseCache = new SafeWeakMap(); + +// Set first due to cycle with ESM loader functions. +module.exports = { + wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache, + get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; } +}; + const { NativeModule } = require('internal/bootstrap/loaders'); const { getSourceMapsEnabled, @@ -758,16 +761,21 @@ Module._load = function(request, parent, isMain) { const cachedModule = Module._cache[filename]; if (cachedModule !== undefined) { updateChildren(parent, cachedModule, true); - if (!cachedModule.loaded) - return getExportsForCircularRequire(cachedModule); - return cachedModule.exports; + if (!cachedModule.loaded) { + const parseCachedModule = cjsParseCache.get(cachedModule); + if (!parseCachedModule || parseCachedModule.loaded) + return getExportsForCircularRequire(cachedModule); + parseCachedModule.loaded = true; + } else { + return cachedModule.exports; + } } const mod = loadNativeModule(filename, request); if (mod && mod.canBeRequiredByUsers) return mod.exports; // Don't call updateChildren(), Module constructor already does. - const module = new Module(filename, parent); + const module = cachedModule || new Module(filename, parent); if (isMain) { process.mainModule = module; @@ -1106,7 +1114,15 @@ Module._extensions['.js'] = function(module, filename) { throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); } } - const content = fs.readFileSync(filename, 'utf8'); + // If already analyzed the source, then it will be cached. + const cached = cjsParseCache.get(module); + let content; + if (cached && cached.source) { + content = cached.source; + cached.source = undefined; + } else { + content = fs.readFileSync(filename, 'utf8'); + } module._compile(content, filename); }; diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index dedfc54e7f3..7b8f146771c 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -103,28 +103,27 @@ class ModuleJob { ' does not provide an export named')) { const splitStack = StringPrototypeSplit(e.stack, '\n'); const parentFileUrl = splitStack[0]; - const childSpecifier = StringPrototypeMatch(e.message, /module '(.*)' does/)[1]; + const [, childSpecifier, name] = StringPrototypeMatch(e.message, + /module '(.*)' does not provide an export named '(.+)'/); const childFileURL = - await this.loader.resolve(childSpecifier, parentFileUrl); + await this.loader.resolve(childSpecifier, parentFileUrl); const format = await this.loader.getFormat(childFileURL); if (format === 'commonjs') { - e.message = `The requested module '${childSpecifier}' is expected ` + - 'to be of type CommonJS, which does not support named exports. ' + - 'CommonJS modules can be imported by importing the default ' + - 'export.'; + const importStatement = splitStack[1]; // TODO(@ctavan): The original error stack only provides the single // line which causes the error. For multi-line import statements we // cannot generate an equivalent object descructuring assignment by // just parsing the error stack. - const importStatement = splitStack[1]; const oneLineNamedImports = StringPrototypeMatch(importStatement, /{.*}/); - if (oneLineNamedImports) { - const destructuringAssignment = - StringPrototypeReplace(oneLineNamedImports[0], /\s+as\s+/g, ': '); - e.message += '\nFor example:\n' + - `import pkg from '${childSpecifier}';\n` + - `const ${destructuringAssignment} = pkg;`; - } + const destructuringAssignment = oneLineNamedImports && + StringPrototypeReplace(oneLineNamedImports, /\s+as\s+/g, ': '); + e.message = `Named export '${name}' not found. The requested module` + + ` '${childSpecifier}' is a CommonJS module, which may not support` + + ' all module.exports as named exports.\nCommonJS modules can ' + + 'always be imported via the default export, for example using:' + + `\n\nimport pkg from '${childSpecifier}';\n${ + destructuringAssignment ? + `const ${destructuringAssignment} = pkg;\n` : ''}`; const newStack = StringPrototypeSplit(e.stack, '\n'); newStack[3] = `SyntaxError: ${e.message}`; e.stack = ArrayPrototypeJoin(newStack, '\n'); diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index bb095446bc2..5e4de5d5af0 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -3,11 +3,14 @@ /* global WebAssembly */ const { + Boolean, JSONParse, + ObjectPrototypeHasOwnProperty, ObjectKeys, PromisePrototypeCatch, PromiseReject, SafeMap, + SafeSet, StringPrototypeReplace, } = primordials; @@ -17,11 +20,16 @@ function lazyTypes() { return _TYPES = require('internal/util/types'); } +const { readFileSync } = require('fs'); +const { extname } = require('path'); const { stripBOM, loadNativeModule } = require('internal/modules/cjs/helpers'); -const CJSModule = require('internal/modules/cjs/loader').Module; +const { + Module: CJSModule, + cjsParseCache +} = require('internal/modules/cjs/loader'); const internalURLModule = require('internal/url'); const { defaultGetSource } = require( 'internal/modules/esm/get_source'); @@ -44,12 +52,12 @@ const { ModuleWrap } = moduleWrap; const { getOptionValue } = require('internal/options'); const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve'); +const asyncESM = require('internal/process/esm_loader'); +const cjsParse = require('internal/deps/cjs-module-lexer/lexer'); const translators = new SafeMap(); exports.translators = translators; -const asyncESM = require('internal/process/esm_loader'); - let DECODER = null; function assertBufferSource(body, allowString, hookName) { if (allowString && typeof body === 'string') { @@ -104,7 +112,7 @@ function initializeImportMeta(meta, { url }) { meta.url = url; } -// Strategy for loading a standard JavaScript module +// Strategy for loading a standard JavaScript module. translators.set('module', async function moduleStrategy(url) { let { source } = await this._getSource( url, { format: 'module' }, defaultGetSource); @@ -125,23 +133,92 @@ translators.set('module', async function moduleStrategy(url) { // Strategy for loading a node-style CommonJS module const isWindows = process.platform === 'win32'; const winSepRegEx = /\//g; -translators.set('commonjs', function commonjsStrategy(url, isMain) { +translators.set('commonjs', async function commonjsStrategy(url, isMain) { debug(`Translating CJSModule ${url}`); - return new ModuleWrap(url, undefined, ['default'], function() { + + let filename = internalURLModule.fileURLToPath(new URL(url)); + if (isWindows) + filename = StringPrototypeReplace(filename, winSepRegEx, '\\'); + + const { module, exportNames } = cjsPreparseModuleExports(filename); + const namesWithDefault = exportNames.has('default') ? + [...exportNames] : ['default', ...exportNames]; + + return new ModuleWrap(url, undefined, namesWithDefault, function() { debug(`Loading CJSModule ${url}`); - const pathname = internalURLModule.fileURLToPath(new URL(url)); + let exports; - const cachedModule = CJSModule._cache[pathname]; - if (cachedModule && asyncESM.ESMLoader.cjsCache.has(cachedModule)) { - exports = asyncESM.ESMLoader.cjsCache.get(cachedModule); - asyncESM.ESMLoader.cjsCache.delete(cachedModule); + if (asyncESM.ESMLoader.cjsCache.has(module)) { + exports = asyncESM.ESMLoader.cjsCache.get(module); + asyncESM.ESMLoader.cjsCache.delete(module); } else { - exports = CJSModule._load(pathname, undefined, isMain); + exports = CJSModule._load(filename, undefined, isMain); + } + + for (const exportName of exportNames) { + if (!ObjectPrototypeHasOwnProperty(exports, exportName) || + exportName === 'default') + continue; + // We might trigger a getter -> dont fail. + let value; + try { + value = exports[exportName]; + } catch {} + this.setExport(exportName, value); } this.setExport('default', exports); }); }); +function cjsPreparseModuleExports(filename) { + let module = CJSModule._cache[filename]; + if (module) { + const cached = cjsParseCache.get(module); + if (cached) + return { module, exportNames: cached.exportNames }; + } + const loaded = Boolean(module); + if (!loaded) { + module = new CJSModule(filename); + module.filename = filename; + module.paths = CJSModule._nodeModulePaths(module.path); + CJSModule._cache[filename] = module; + } + + let source; + try { + source = readFileSync(filename, 'utf8'); + } catch {} + + const { exports, reexports } = cjsParse(source || ''); + + const exportNames = new SafeSet(exports); + + // Set first for cycles. + cjsParseCache.set(module, { source, exportNames, loaded }); + + if (reexports.length) { + module.filename = filename; + module.paths = CJSModule._nodeModulePaths(module.path); + } + for (const reexport of reexports) { + let resolved; + try { + resolved = CJSModule._resolveFilename(reexport, module); + } catch { + continue; + } + const ext = extname(resolved); + if (ext === '.js' || ext === '.cjs' || !CJSModule._extensions[ext]) { + const { exportNames: reexportNames } = cjsPreparseModuleExports(resolved); + for (const name of reexportNames) + exportNames.add(name); + } + } + + return { module, exportNames }; +} + // Strategy for loading a node builtin CommonJS module that isn't // through normal resolution translators.set('builtin', async function builtinStrategy(url) { diff --git a/node.gyp b/node.gyp index b0b045b02ba..8f156e52285 100644 --- a/node.gyp +++ b/node.gyp @@ -266,6 +266,7 @@ 'deps/acorn-plugins/acorn-private-class-elements/index.js', 'deps/acorn-plugins/acorn-private-methods/index.js', 'deps/acorn-plugins/acorn-static-class-features/index.js', + 'deps/cjs-module-lexer/lexer.js', ], 'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)', 'mkcodecache_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mkcodecache<(EXECUTABLE_SUFFIX)', diff --git a/src/node_native_module.cc b/src/node_native_module.cc index 17abd057eaa..213379fde4a 100644 --- a/src/node_native_module.cc +++ b/src/node_native_module.cc @@ -78,6 +78,9 @@ void NativeModuleLoader::InitializeModuleCategories() { "internal/main/" }; + module_categories_.can_be_required.emplace( + "internal/deps/cjs-module-lexer/lexer"); + module_categories_.cannot_be_required = std::set { #if !HAVE_INSPECTOR "inspector", @@ -118,7 +121,8 @@ void NativeModuleLoader::InitializeModuleCategories() { if (prefix.length() > id.length()) { continue; } - if (id.find(prefix) == 0) { + if (id.find(prefix) == 0 && + module_categories_.can_be_required.count(id) == 0) { module_categories_.cannot_be_required.emplace(id); } } diff --git a/test/es-module/test-esm-cjs-exports.js b/test/es-module/test-esm-cjs-exports.js new file mode 100644 index 00000000000..37aa70d3880 --- /dev/null +++ b/test/es-module/test-esm-cjs-exports.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common'); +const fixtures = require('../common/fixtures'); +const { spawn } = require('child_process'); +const assert = require('assert'); + +const entry = fixtures.path('/es-modules/cjs-exports.mjs'); + +const child = spawn(process.execPath, [entry]); +child.stderr.setEncoding('utf8'); +let stdout = ''; +child.stdout.setEncoding('utf8'); +child.stdout.on('data', (data) => { + stdout += data; +}); +child.on('close', common.mustCall((code, signal) => { + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + assert.strictEqual(stdout, 'ok\n'); +})); diff --git a/test/es-module/test-esm-cjs-named-error.mjs b/test/es-module/test-esm-cjs-named-error.mjs index e9ddc67c0fb..4ef75a22f92 100644 --- a/test/es-module/test-esm-cjs-named-error.mjs +++ b/test/es-module/test-esm-cjs-named-error.mjs @@ -3,37 +3,25 @@ import { rejects } from 'assert'; const fixtureBase = '../fixtures/es-modules/package-cjs-named-error'; -const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' + - 'be of type CommonJS, which does not support named exports. CommonJS ' + - 'modules can be imported by importing the default export.\n' + - 'For example:\n' + - 'import pkg from \'./fail.cjs\';\n' + - 'const { comeOn } = pkg;'; +const errTemplate = (specifier, name, namedImports) => + `Named export '${name}' not found. The requested module` + + ` '${specifier}' is a CommonJS module, which may not support ` + + 'all module.exports as named exports.\nCommonJS modules can ' + + 'always be imported via the default export, for example using:' + + `\n\nimport pkg from '${specifier}';\n` + (namedImports ? + `const ${namedImports} = pkg;\n` : ''); -const expectedWithoutExample = 'The requested module \'./fail.cjs\' is ' + - 'expected to be of type CommonJS, which does not support named exports. ' + - 'CommonJS modules can be imported by importing the default export.'; +const expectedWithoutExample = errTemplate('./fail.cjs', 'comeOn'); -const expectedRenamed = 'The requested module \'./fail.cjs\' is expected to ' + - 'be of type CommonJS, which does not support named exports. CommonJS ' + - 'modules can be imported by importing the default export.\n' + - 'For example:\n' + - 'import pkg from \'./fail.cjs\';\n' + - 'const { comeOn: comeOnRenamed } = pkg;'; +const expectedRelative = errTemplate('./fail.cjs', 'comeOn', '{ comeOn }'); -const expectedPackageHack = 'The requested module \'./json-hack/fail.js\' is ' + - 'expected to be of type CommonJS, which does not support named exports. ' + - 'CommonJS modules can be imported by importing the default export.\n' + - 'For example:\n' + - 'import pkg from \'./json-hack/fail.js\';\n' + - 'const { comeOn } = pkg;'; +const expectedRenamed = errTemplate('./fail.cjs', 'comeOn', + '{ comeOn: comeOnRenamed }'); -const expectedBare = 'The requested module \'deep-fail\' is expected to ' + - 'be of type CommonJS, which does not support named exports. CommonJS ' + - 'modules can be imported by importing the default export.\n' + - 'For example:\n' + - 'import pkg from \'deep-fail\';\n' + - 'const { comeOn } = pkg;'; +const expectedPackageHack = + errTemplate('./json-hack/fail.js', 'comeOn', '{ comeOn }'); + +const expectedBare = errTemplate('deep-fail', 'comeOn', '{ comeOn }'); rejects(async () => { await import(`${fixtureBase}/single-quote.mjs`); diff --git a/test/fixtures/es-modules/cjs-exports.mjs b/test/fixtures/es-modules/cjs-exports.mjs new file mode 100644 index 00000000000..47bb4926af0 --- /dev/null +++ b/test/fixtures/es-modules/cjs-exports.mjs @@ -0,0 +1,34 @@ +import { strictEqual, deepEqual } from 'assert'; + +import m, { π, z } from './exports-cases.js'; +import * as ns from './exports-cases.js'; + +deepEqual(Object.keys(ns), ['default', 'isObject', 'z', 'π']); +strictEqual(π, 'yes'); +strictEqual(z, 'yes'); +strictEqual(typeof m.isObject, 'undefined'); +strictEqual(m.π, 'yes'); +strictEqual(m.z, 'yes'); + +import m2, { __esModule as __esModule2, name as name2 } from './exports-cases2.js'; +import * as ns2 from './exports-cases2.js'; + +strictEqual(__esModule2, true); +strictEqual(name2, 'name'); +strictEqual(typeof m2, 'object'); +strictEqual(m2.default, 'the default'); +strictEqual(ns2.__esModule, true); +strictEqual(ns2.name, 'name'); +deepEqual(Object.keys(ns2), ['__esModule', 'case2', 'default', 'name', 'pi']); + +import m3, { __esModule as __esModule3, name as name3 } from './exports-cases3.js'; +import * as ns3 from './exports-cases3.js'; + +strictEqual(__esModule3, true); +strictEqual(name3, 'name'); +deepEqual(Object.keys(m3), ['name', 'default', 'pi', 'case2']); +strictEqual(ns3.__esModule, true); +strictEqual(ns3.name, 'name'); +strictEqual(ns3.case2, 'case2'); + +console.log('ok'); diff --git a/test/fixtures/es-modules/exports-cases.js b/test/fixtures/es-modules/exports-cases.js new file mode 100644 index 00000000000..eec3d31bc72 --- /dev/null +++ b/test/fixtures/es-modules/exports-cases.js @@ -0,0 +1,7 @@ +if (global.maybe) + module.exports = require('../is-object'); +exports['invalid identifier'] = 'no'; +module.exports['?invalid'] = 'no'; +module.exports['π'] = 'yes'; +exports.package = 10; // reserved word -> not used +Object.defineProperty(exports, 'z', { value: 'yes' }); diff --git a/test/fixtures/es-modules/exports-cases2.js b/test/fixtures/es-modules/exports-cases2.js new file mode 100644 index 00000000000..189eebb9f3b --- /dev/null +++ b/test/fixtures/es-modules/exports-cases2.js @@ -0,0 +1,29 @@ +/* + * Transpiled with Babel from: + * + * export { π as pi } from './exports-cases.js'; + * export default 'the default'; + * export const name = 'name'; + */ + +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "pi", { + enumerable: true, + get: function () { + return _exportsCases.π; + } +}); +exports.name = exports.default = void 0; + +var _exportsCases = require("./exports-cases.js"); + +var _default = 'the default'; +exports.default = _default; +const name = 'name'; +exports.name = name; + +exports.case2 = 'case2'; diff --git a/test/fixtures/es-modules/exports-cases3.js b/test/fixtures/es-modules/exports-cases3.js new file mode 100644 index 00000000000..c48b78cc410 --- /dev/null +++ b/test/fixtures/es-modules/exports-cases3.js @@ -0,0 +1,25 @@ +/* + * Transpiled with TypeScript from: + * + * export { π as pi } from './exports-cases.js'; + * export default 'the default'; + * export const name = 'name'; + */ + +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.name = void 0; +exports.default = 'the default'; +exports.name = 'name'; + +var _external = require("./exports-cases2.js"); + +Object.keys(_external).forEach(function (key) { + if (key === "default" || key === "__esModule") return; + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return _external[key]; + } + }); +}); \ No newline at end of file diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 899d6ce3632..ee9a62fbfa6 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -55,6 +55,7 @@ const expectedModules = new Set([ 'NativeModule internal/modules/cjs/helpers', 'NativeModule internal/modules/cjs/loader', 'NativeModule internal/modules/esm/create_dynamic_module', + 'NativeModule internal/deps/cjs-module-lexer/lexer', 'NativeModule internal/modules/esm/get_format', 'NativeModule internal/modules/esm/get_source', 'NativeModule internal/modules/esm/loader', diff --git a/tools/license-builder.sh b/tools/license-builder.sh index 2ee78b69f46..e6d66a03770 100755 --- a/tools/license-builder.sh +++ b/tools/license-builder.sh @@ -32,6 +32,7 @@ fi addlicense "Acorn" "deps/acorn" "$(cat ${rootdir}/deps/acorn/acorn/LICENSE)" addlicense "Acorn plugins" "deps/acorn-plugins" "$(cat ${rootdir}/deps/acorn-plugins/acorn-class-fields/LICENSE)" addlicense "c-ares" "deps/cares" "$(tail -n +3 ${rootdir}/deps/cares/LICENSE.md)" +addlicense "cjs-module-lexer" "deps/cjs-module-lexer" "$(cat ${rootdir}/deps/cjs-module-lexer/LICENSE)" if [ -f "${rootdir}/deps/icu/LICENSE" ]; then # ICU 57 and following. Drop the BOM addlicense "ICU" "deps/icu" \