Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs
  • Loading branch information
bfarias-godaddy committed Sep 27, 2019
commit 6c49aa3435b6564f426abda4e3307ab326402cd2
12 changes: 8 additions & 4 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,11 @@ cjs === 'cjs'; // true

## Builtin modules

Builtin modules will provide named exports of their public API, as well as a
default export which can be used for, among other things, modifying the named
exports. Named exports of builtin modules are updated when the corresponding
exports property is accessed, redefined, or deleted.
Builtin modules will provide named exports of their public API. A
default export is also provided which is the value of the CommonJS exports.
The default export can be used for, among other things, modifying the named
exports. Named exports of builtin modules are updated only by calling
[`module.syncBuiltinESMExports()`][].

```js
import EventEmitter from 'events';
Expand All @@ -578,8 +579,10 @@ readFile('./foo.txt', (err, source) => {

```js
import fs, { readFileSync } from 'fs';
import { syncBuiltinESMExports } from 'module';

fs.readFileSync = () => Buffer.from('Hello, ESM');
syncBuiltinESMExports();

fs.readFileSync === readFileSync;
```
Expand Down Expand Up @@ -1008,6 +1011,7 @@ success!
[`import.meta.url`]: #esm_import_meta
[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
[`module.createRequire()`]: modules.html#modules_module_createrequire_filename
[`module.syncBuiltinESMExports()`]: modules.html#modules_module_syncbuiltinesmexports
[dynamic instantiate hook]: #esm_dynamic_instantiate_hook
[package exports]: #esm_package_exports
[special scheme]: https://url.spec.whatwg.org/#special-scheme
Expand Down
30 changes: 30 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,36 @@ const requireUtil = createRequireFromPath('../src/utils/');
requireUtil('./some-tool');
```

### module.syncBuiltinESMExports()
<!-- YAML
added: FIXME
Comment thread
bmeck marked this conversation as resolved.
Outdated
-->

The `module.syncBuiltinESMExports()` method updates all the live bindings for
builtin ES Modules to match the properties of the CommonJS exports. It does
not add or remove exported names from the ES Modules.

```js
const fs = require('fs');
const { syncBuiltinESMExports } = require('module');

fs.readFile = null;

delete fs.readFileSync;

fs.newAPI = function newAPI() {
// ...
};

syncBuiltinESMExports();

import('fs').then((esmFS) => {
assert.strictEqual(esmFS.readFile, null);
assert.strictEqual('readFileSync' in fs, true);
assert.strictEqual(esmFS.newAPI, undefined);
});
```

[GLOBAL_FOLDERS]: #modules_loading_from_the_global_folders
[`Error`]: errors.html#errors_class_error
[`__dirname`]: #modules_dirname
Expand Down
54 changes: 2 additions & 52 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,9 @@ NativeModule.prototype.getESMFacade = function() {

// Provide named exports for all builtin libraries so that the libraries
// may be imported in a nicer way for ESM users. The default export is left
// as the entire namespace (module.exports) and wrapped in a proxy such
// that APMs and other behavior are still left intact.
// as the entire namespace (module.exports) and updates when this function is
// called so that APMs and other behavior are supported.
NativeModule.prototype.syncExports = function() {

// const update = (property, value) => {
// if (this.reflect !== undefined &&
// ObjectPrototype.hasOwnProperty(this.reflect.exports, property))
// this.reflect.exports[property].set(value);
// };

const names = this.exportKeys;
if (this.reflect) {
for (let i = 0; i < names.length; i++) {
Expand All @@ -274,49 +267,6 @@ NativeModule.prototype.syncExports = function() {
);
}
}

// const handler = {
// __proto__: null,
// defineProperty: (target, prop, descriptor) => {
// // Use `Object.defineProperty` instead of `Reflect.defineProperty`
// // to throw the appropriate error if something goes wrong.
// Object.defineProperty(target, prop, descriptor);
// if (typeof descriptor.get === 'function' &&
// !Reflect.has(handler, 'get')) {
// handler.get = (target, prop, receiver) => {
// const value = Reflect.get(target, prop, receiver);
// if (ObjectPrototype.hasOwnProperty(target, prop))
// update(prop, value);
// return value;
// };
// }
// update(prop, getOwn(target, prop));
// return true;
// },
// deleteProperty: (target, prop) => {
// if (Reflect.deleteProperty(target, prop)) {
// update(prop, undefined);
// return true;
// }
// return false;
// },
// set: (target, prop, value, receiver) => {
// const descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
// if (Reflect.set(target, prop, value, receiver)) {
// if (descriptor && typeof descriptor.set === 'function') {
// for (const key of this.exportKeys) {
// update(key, getOwn(target, key, receiver));
// }
// } else {
// update(prop, getOwn(target, prop, receiver));
// }
// return true;
// }
// return false;
// }
// };

// this.exports = new Proxy(this.exports, handler);
};

NativeModule.prototype.compile = function() {
Expand Down