Skip to content
/ node Public
forked from nodejs/node

Commit

Permalink
loader,docs,test: set named exports based on keys from module.exports
Browse files Browse the repository at this point in the history
I know a lot of discussion went into the original decision on how mjs
would handle cjs modules but after using the system for a while, and
talking with a lot of other people in the community, it just seems
like the expected behavior and the wanted behavior is to export named
based on the keys. This PR implements that in what is hopefully a
performant enough solution, although that shouldn't be too much of a
problem since this code only runs during initial module loading.

This implementation remains safe with regard to named exports that are
also reserved keywords such as `class` or `delete`.

Refs: nodejs/node-eps#57
  • Loading branch information
devsnek committed Nov 1, 2017
1 parent d1a9c02 commit 81fef20
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
10 changes: 8 additions & 2 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ All CommonJS, JSON, and C++ modules can be used with `import`.
Modules loaded this way will only be loaded once, even if their query
or fragment string differs between `import` statements.

When loaded via `import` these modules will provide a single `default` export
representing the value of `module.exports` at the time they finished evaluating.
When loaded via `import` these modules will provide a `default` export
representing the value of `module.exports` at the time they finished evaluating,
and named exports for each key of `module.exports`.

```js
import fs from 'fs';
Expand All @@ -97,6 +98,11 @@ fs.readFile('./foo.txt', (err, body) => {
});
```

```js
import { readFileSync } from 'fs';
console.log(readFileSync('./foo.txt').toString());
```

## Loader hooks

<!-- type=misc -->
Expand Down
33 changes: 20 additions & 13 deletions lib/internal/loader/ModuleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,38 @@ loaders.set('esm', async (url) => {

// Strategy for loading a node-style CommonJS module
loaders.set('cjs', async (url) => {
return createDynamicModule(['default'], url, (reflect) => {
debug(`Loading CJSModule ${url}`);
const CJSModule = require('module');
const pathname = internalURLModule.getPathFromURL(new URL(url));
CJSModule._load(pathname);
debug(`Loading CJSModule ${url}`);
const CJSModule = require('module');
const pathname = internalURLModule.getPathFromURL(new URL(url));
const exports = CJSModule._load(pathname);
const keys = Object.keys(exports);
return createDynamicModule(['default', ...keys], url, (reflect) => {
reflect.exports.default.set(exports);
for (const key of keys) reflect.exports[key].set(exports[key]);
});
});

// Strategy for loading a node builtin CommonJS module that isn't
// through normal resolution
loaders.set('builtin', async (url) => {
return createDynamicModule(['default'], url, (reflect) => {
debug(`Loading BuiltinModule ${url}`);
const exports = NativeModule.require(url.substr(5));
debug(`Loading BuiltinModule ${url}`);
const exports = NativeModule.require(url.substr(5));
const keys = Object.keys(exports);
return createDynamicModule(['default', ...keys], url, (reflect) => {
reflect.exports.default.set(exports);
for (const key of keys) reflect.exports[key].set(exports[key]);
});
});

loaders.set('addon', async (url) => {
const ctx = createDynamicModule(['default'], url, (reflect) => {
debug(`Loading NativeModule ${url}`);
const module = { exports: {} };
const pathname = internalURLModule.getPathFromURL(new URL(url));
process.dlopen(module, _makeLong(pathname));
debug(`Loading NativeModule ${url}`);
const module = { exports: {} };
const pathname = internalURLModule.getPathFromURL(new URL(url));
process.dlopen(module, _makeLong(pathname));
const keys = Object.keys(module.exports);
const ctx = createDynamicModule(['default', ...keys], url, (reflect) => {
reflect.exports.default.set(module.exports);
for (const key of keys) reflect.exports[key].set(module.exports[key]);
});
return ctx;
});
Expand Down
6 changes: 4 additions & 2 deletions test/es-module/test-esm-namespace.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Flags: --experimental-modules
/* eslint-disable required-modules */

import * as fs from 'fs';
import assert from 'assert';
import fs, { readFile } from 'fs';

assert.deepStrictEqual(Object.keys(fs), ['default']);
assert(fs);
assert(fs.readFile);
assert(readFile);

0 comments on commit 81fef20

Please sign in to comment.