module: named exports for CJS via static analysis

PR-URL: https://github.com/nodejs/node/pull/35249
Reviewed-By: Mary Marchini <oss@mmarchini.me>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
Guy Bedford 2020-05-14 22:40:37 -07:00
parent 062b35d4e8
commit 1e8cb08edc
20 changed files with 1935 additions and 94 deletions

14
LICENSE
View File

@ -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)

11
deps/cjs-module-lexer/.gitignore vendored Executable file
View File

@ -0,0 +1,11 @@
node_modules
*.lock
test
.*
Makefile
bench
build.js
include-wasm
include
lib
src

10
deps/cjs-module-lexer/LICENSE vendored Executable file
View File

@ -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.

331
deps/cjs-module-lexer/README.md vendored Executable file
View File

@ -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

1152
deps/cjs-module-lexer/lexer.js vendored Executable file

File diff suppressed because it is too large Load Diff

41
deps/cjs-module-lexer/package.json vendored Executable file
View File

@ -0,0 +1,41 @@
{
"name": "cjs-module-lexer",
"version": "0.3.3",
"description": "Lexes CommonJS modules, returning their named exports metadata",
"main": "lexer.js",
"exports": {
"import": "./dist/lexer.mjs",
"default": "./lexer.js"
},
"scripts": {
"test-js": "mocha -b -u tdd test/*.js",
"test-wasm": "WASM=1 mocha -b -u tdd test/*.js",
"test": "npm run test-wasm && npm run test-js",
"bench": "node --expose-gc bench/index.mjs",
"build": "node build.js && babel dist/lexer.mjs | terser -o dist/lexer.js",
"build-wasm": "make lib/lexer.wasm && node build.js",
"prepublishOnly": "make optimize && npm run build",
"footprint": "make optimize && npm run build && cat dist/lexer.js | gzip -9f | wc -c"
},
"author": "Guy Bedford",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
"kleur": "^2.0.2",
"mocha": "^5.2.0",
"terser": "^4.1.4"
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/guybedford/cjs-module-lexer.git"
},
"bugs": {
"url": "https://github.com/guybedford/cjs-module-lexer/issues"
},
"homepage": "https://github.com/guybedford/cjs-module-lexer#readme"
}

View File

@ -241,13 +241,20 @@ To include an ES module into CommonJS, use [`import()`][].
### `import` statements
An `import` statement can reference an ES module or a CommonJS module. Other
file types such as JSON or native modules are not supported. For those, use
[`module.createRequire()`][].
An `import` statement can reference an ES module or a CommonJS module.
`import` statements are permitted only in ES modules. For similar functionality
in CommonJS, see [`import()`][].
When importing [CommonJS modules](#esm_commonjs_namespaces), the
`module.exports` object is provided as the default export. Named exports may be
available, provided by static analysis as a convenience for better ecosystem
compatibility.
Additional experimental flags are available for importing
[Wasm modules](#esm_experimental_wasm_modules) or
[JSON modules](#esm_experimental_json_modules). For importing native modules or
JSON modules unflagged, see [`module.createRequire()`][].
The _specifier_ of an `import` statement (the string after the `from` keyword)
can either be an URL-style relative path like `'./file.mjs'` or a package name
like `'fs'`.
@ -261,41 +268,94 @@ via the path defined in [`"exports"`][].
import { sin, cos } from 'geometry/trigonometry-functions.mjs';
```
Only the “default export” is supported for CommonJS files or packages:
<!-- eslint-disable no-duplicate-imports -->
```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:
<!-- eslint-disable no-duplicate-imports -->
```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:
// <module.exports>
// 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:
<!-- eslint-skip -->
```js
import * as m from 'cjs';
console.log(m);
console.log(m === await import('cjs'));
// Prints:
// [Module] { default: <module.exports> }
// 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:
<!-- eslint-disable no-duplicate-imports -->
```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!
```
<!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version -->
[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

View File

@ -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);
};

View File

@ -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');

View File

@ -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) {

View File

@ -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)',

View File

@ -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<std::string> {
#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);
}
}

View File

@ -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');
}));

View File

@ -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`);

View File

@ -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');

View File

@ -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' });

View File

@ -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';

View File

@ -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];
}
});
});

View File

@ -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',

View File

@ -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" \