summaryrefslogtreecommitdiff
path: root/test/fixtures
diff options
context:
space:
mode:
authorJan Krems <jan.krems@gmail.com>2019-08-19 20:59:25 -0700
committerMichaël Zasso <targos@protonmail.com>2019-10-28 07:08:02 +0100
commitedfbee37271d288d5dca7a33f06b14ef704f599d (patch)
tree14e65ba276dc286840442cd10553de9d602546f5 /test/fixtures
parent0d9ae1b8f62082ada91f032dbda200edf67d9646 (diff)
downloadnode-new-edfbee37271d288d5dca7a33f06b14ef704f599d.tar.gz
module: resolve self-references
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 <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Diffstat (limited to 'test/fixtures')
-rw-r--r--test/fixtures/node_modules/pkgexports/lib/hole.js10
-rw-r--r--test/fixtures/node_modules/pkgexports/not-exported.js3
-rw-r--r--test/fixtures/node_modules/pkgexports/package.json2
3 files changed, 15 insertions, 0 deletions
diff --git a/test/fixtures/node_modules/pkgexports/lib/hole.js b/test/fixtures/node_modules/pkgexports/lib/hole.js
new file mode 100644
index 0000000000..124b0932ad
--- /dev/null
+++ b/test/fixtures/node_modules/pkgexports/lib/hole.js
@@ -0,0 +1,10 @@
+'use strict';
+
+module.exports = {
+ async importFromInside(specifier) {
+ return import(specifier);
+ },
+ async requireFromInside(specifier) {
+ return { default: require(specifier) };
+ },
+};
diff --git a/test/fixtures/node_modules/pkgexports/not-exported.js b/test/fixtures/node_modules/pkgexports/not-exported.js
new file mode 100644
index 0000000000..02e146dbe9
--- /dev/null
+++ b/test/fixtures/node_modules/pkgexports/not-exported.js
@@ -0,0 +1,3 @@
+'use strict';
+
+module.exports = 'not-exported';
diff --git a/test/fixtures/node_modules/pkgexports/package.json b/test/fixtures/node_modules/pkgexports/package.json
index 2b190521e5..38e2fc1a5c 100644
--- a/test/fixtures/node_modules/pkgexports/package.json
+++ b/test/fixtures/node_modules/pkgexports/package.json
@@ -1,6 +1,8 @@
{
+ "name": "@pkgexports/name",
"exports": {
".": "./asdf.js",
+ "./hole": "./lib/hole.js",
"./space": "./sp%20ce.js",
"./valid-cjs": "./asdf.js",
"./sub/": "./",