From 0b495a899bdf9a26b25a6e31177512531c841e0e Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 27 Sep 2019 12:12:18 -0500 Subject: esm: remove proxy for builtin exports PR-URL: https://github.com/nodejs/node/pull/29737 Reviewed-By: Guy Bedford Reviewed-By: Gus Caplan Reviewed-By: Jan Krems Reviewed-By: Matteo Collina Reviewed-By: Minwoo Jung Reviewed-By: Benjamin Gruenbaum --- doc/api/esm.md | 12 ++++++++---- doc/api/modules.md | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) (limited to 'doc') diff --git a/doc/api/esm.md b/doc/api/esm.md index 5805284016..e0a548f042 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -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'; @@ -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; ``` @@ -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 diff --git a/doc/api/modules.md b/doc/api/modules.md index 32490cf47f..9ed9273e0b 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -984,6 +984,36 @@ const requireUtil = createRequireFromPath('../src/utils/'); requireUtil('./some-tool'); ``` +### module.syncBuiltinESMExports() + + +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 -- cgit v1.2.3