From b66496d5fe449813690580ad3d8c2e1cbe463368 Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Fri, 6 Mar 2020 02:11:13 -0500 Subject: [PATCH] module: bring back support for exports: false Refs: https://github.com/nodejs/node/pull/31625 Refs: https://github.com/nodejs/node/issues/32107 Refs: https://github.com/babel/babel/issues/11216 --- lib/internal/modules/cjs/loader.js | 2 +- src/module_wrap.cc | 36 ++++++++++--------- test/es-module/test-esm-exports.mjs | 8 +++++ .../pkgexports-false-cjs/main.cjs | 1 + .../pkgexports-false-cjs/package.json | 4 +++ .../pkgexports-false-mjs/main.mjs | 1 + .../pkgexports-false-mjs/package.json | 4 +++ 7 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 test/fixtures/node_modules/pkgexports-false-cjs/main.cjs create mode 100644 test/fixtures/node_modules/pkgexports-false-cjs/package.json create mode 100644 test/fixtures/node_modules/pkgexports-false-mjs/main.mjs create mode 100644 test/fixtures/node_modules/pkgexports-false-mjs/package.json diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index a73101aa1feaf1..752cf46e8cbfdc 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -495,7 +495,7 @@ function applyExports(basePath, expansion) { const mappingKey = `.${expansion}`; let pkgExports = readPackageExports(basePath); - if (pkgExports === undefined || pkgExports === null) + if (pkgExports === undefined || pkgExports === null || pkgExports === false) return path.resolve(basePath, mappingKey); if (isConditionalDotExportSugar(pkgExports, basePath)) diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 2b1708088bf449..839864f71cb05b 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -1145,25 +1145,27 @@ Maybe PackageMainResolve(Environment* env, if (!pcfg.exports.IsEmpty()) { Local exports = pcfg.exports.Get(isolate); - Maybe isConditionalExportsMainSugar = - IsConditionalExportsMainSugar(env, exports, pjson_url, base); - if (isConditionalExportsMainSugar.IsNothing()) - return Nothing(); - if (isConditionalExportsMainSugar.FromJust()) { - return ResolveExportsTarget(env, pjson_url, exports, "", "", base); - } else if (exports->IsObject()) { - Local exports_obj = exports.As(); - if (exports_obj->HasOwnProperty(env->context(), env->dot_string()) - .FromJust()) { - Local target = - exports_obj->Get(env->context(), env->dot_string()) - .ToLocalChecked(); - return ResolveExportsTarget(env, pjson_url, target, "", "", base); + if (!exports->IsFalse()) { + Maybe isConditionalExportsMainSugar = + IsConditionalExportsMainSugar(env, exports, pjson_url, base); + if (isConditionalExportsMainSugar.IsNothing()) + return Nothing(); + if (isConditionalExportsMainSugar.FromJust()) { + return ResolveExportsTarget(env, pjson_url, exports, "", "", base); + } else if (exports->IsObject()) { + Local exports_obj = exports.As(); + if (exports_obj->HasOwnProperty(env->context(), env->dot_string()) + .FromJust()) { + Local target = + exports_obj->Get(env->context(), env->dot_string()) + .ToLocalChecked(); + return ResolveExportsTarget(env, pjson_url, target, "", "", base); + } } + std::string msg = "No \"exports\" main resolved in " + + pjson_url.ToFilePath(); + node::THROW_ERR_PACKAGE_PATH_NOT_EXPORTED(env, msg.c_str()); } - std::string msg = "No \"exports\" main resolved in " + - pjson_url.ToFilePath(); - node::THROW_ERR_PACKAGE_PATH_NOT_EXPORTED(env, msg.c_str()); } if (pcfg.has_main == HasMain::Yes) { URL resolved(pcfg.main, pjson_url); diff --git a/test/es-module/test-esm-exports.mjs b/test/es-module/test-esm-exports.mjs index 7dbc963502824e..a1163452c6932e 100644 --- a/test/es-module/test-esm-exports.mjs +++ b/test/es-module/test-esm-exports.mjs @@ -33,6 +33,14 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; { default: 'self-cjs' } : { default: 'self-mjs' }], // Resolve self sugar ['pkgexports-sugar', { default: 'main' }], + // Resolve exports false + isRequire ? [ + 'pkgexports-false-cjs', + { default: 'false-exports-cjs' + }] : [ + 'pkgexports-false-mjs', + { default: 'false-exports-mjs' + }], ]); for (const [validSpecifier, expected] of validSpecifiers) { diff --git a/test/fixtures/node_modules/pkgexports-false-cjs/main.cjs b/test/fixtures/node_modules/pkgexports-false-cjs/main.cjs new file mode 100644 index 00000000000000..48ff9c2b91f860 --- /dev/null +++ b/test/fixtures/node_modules/pkgexports-false-cjs/main.cjs @@ -0,0 +1 @@ +module.exports = 'false-exports-cjs'; diff --git a/test/fixtures/node_modules/pkgexports-false-cjs/package.json b/test/fixtures/node_modules/pkgexports-false-cjs/package.json new file mode 100644 index 00000000000000..2b5d92a21e56b8 --- /dev/null +++ b/test/fixtures/node_modules/pkgexports-false-cjs/package.json @@ -0,0 +1,4 @@ +{ + "main": "./main.cjs", + "exports": false +} diff --git a/test/fixtures/node_modules/pkgexports-false-mjs/main.mjs b/test/fixtures/node_modules/pkgexports-false-mjs/main.mjs new file mode 100644 index 00000000000000..9495fea4907216 --- /dev/null +++ b/test/fixtures/node_modules/pkgexports-false-mjs/main.mjs @@ -0,0 +1 @@ +export default 'false-exports-mjs'; diff --git a/test/fixtures/node_modules/pkgexports-false-mjs/package.json b/test/fixtures/node_modules/pkgexports-false-mjs/package.json new file mode 100644 index 00000000000000..aa78413bdad28d --- /dev/null +++ b/test/fixtures/node_modules/pkgexports-false-mjs/package.json @@ -0,0 +1,4 @@ +{ + "main": "./main.mjs", + "exports": false +}