From 316d8d39a990420f2c21273ed4c1fbeb3d7924ed Mon Sep 17 00:00:00 2001 From: Jan Krems Date: Mon, 19 Aug 2019 20:59:25 -0700 Subject: [PATCH] module: resolve self-references MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: https://github.com/nodejs/node/pull/29327 Reviewed-By: Guy Bedford Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso --- doc/api/cli.md | 9 + doc/api/esm.md | 28 ++- doc/api/modules.md | 12 +- lib/internal/modules/cjs/loader.js | 166 ++++++++++++++---- src/env.h | 3 + src/module_wrap.cc | 79 +++++++++ src/node_options.cc | 4 + src/node_options.h | 1 + test/es-module/test-esm-exports.mjs | 21 ++- .../node_modules/pkgexports/lib/hole.js | 10 ++ .../node_modules/pkgexports/not-exported.js | 3 + .../node_modules/pkgexports/package.json | 2 + 12 files changed, 294 insertions(+), 44 deletions(-) create mode 100644 test/fixtures/node_modules/pkgexports/lib/hole.js create mode 100644 test/fixtures/node_modules/pkgexports/not-exported.js diff --git a/doc/api/cli.md b/doc/api/cli.md index e0e23c4d2c7873..2618aa83f659f7 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -196,6 +196,14 @@ added: v11.8.0 Enable experimental diagnostic report feature. +### `--experimental-resolve-self` + + +Enable experimental support for a package using `require` or `import` to load +itself. + ### `--experimental-vm-modules`