From d24399c4b3883cfbd87686baea7e371e1a5b7043 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 4 Dec 2019 21:14:48 -0500 Subject: [PATCH 1/8] module: conditional exports usability updates --- doc/api/esm.md | 41 +++++++++---------- doc/api/modules.md | 3 +- lib/internal/modules/cjs/loader.js | 18 +++----- src/env.h | 1 - src/module_wrap.cc | 4 +- test/es-module/test-esm-exports.mjs | 13 +++++- .../node_modules/pkgexports-main/main.cjs | 1 + .../node_modules/pkgexports-main/module.mjs | 1 + .../node_modules/pkgexports-main/package.json | 6 +++ .../pkgexports-sugar-fail/package.json | 2 +- .../pkgexports-sugar2/package.json | 4 +- .../node_modules/pkgexports/package.json | 2 +- 12 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 test/fixtures/node_modules/pkgexports-main/main.cjs create mode 100644 test/fixtures/node_modules/pkgexports-main/module.mjs create mode 100644 test/fixtures/node_modules/pkgexports-main/package.json diff --git a/doc/api/esm.md b/doc/api/esm.md index f834f29fbbee85..d7076ad4020f2e 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -329,8 +329,8 @@ Node.js and the browser can be written: "main": "./index.js", "exports": { "./feature": { - "browser": "./feature-browser.js", - "default": "./feature-default.js" + "module": "./feature-default.js", + "browser": "./feature-browser.js" } } } @@ -341,17 +341,16 @@ will be used as the final fallback. The conditions supported in Node.js are matched in the following order: -1. `"require"` - matched when the package is loaded via `require()`. - _This is currently only supported behind the - `--experimental-conditional-exports` flag._ -2. `"node"` - matched for any Node.js environment. Can be a CommonJS or ES +1. `"node"` - matched for any Node.js environment. Can be a CommonJS or ES module file. _This is currently only supported behind the `--experimental-conditional-exports` flag._ -3. `"default"` - the generic fallback that will always match if no other - more specific condition is matched first. Can be a CommonJS or ES module - file. +2. `"commonjs"` - matched when the package is loaded via `require()`. + _This is currently only supported behind the + `--experimental-conditional-exports` flag._ +3. `"module"` - matched when the package is not loaded via `require()`. + Can be any module format, this field does not set the type interpretation. -Using the `"require"` condition it is possible to define a package that will +Using the `"commonjs"` condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a hazard in that it can result in having two separate instances of the same package in use in an application, which can cause a number of bugs. @@ -394,8 +393,8 @@ from exports subpaths. { "exports": { ".": { - "require": "./main.cjs", - "default": "./main.js" + "module": "./main.js", + "commonjs": "./main.cjs" } } } @@ -407,8 +406,8 @@ can be written: ```js { "exports": { - "require": "./main.cjs", - "default": "./main.js" + "module": "./main.js", + "commonjs": "./main.cjs" } } ``` @@ -422,8 +421,8 @@ thrown: // Throws on resolution! "exports": { "./feature": "./lib/feature.js", - "require": "./main.cjs", - "default": "./main.js" + "module": "./main.js", + "commonjs": "./main.cjs" } } ``` @@ -519,8 +518,8 @@ ES module wrapper is used for `import` and the CommonJS entry point for "type": "module", "main": "./index.cjs", "exports": { - "require": "./index.cjs", - "default": "./wrapper.mjs" + "commonjs": "./index.cjs", + "module": "./wrapper.mjs" } } ``` @@ -605,8 +604,8 @@ CommonJS and ES module entry points directly (requires "type": "module", "main": "./index.cjs", "exports": { - "require": "./index.cjs", - "default": "./index.mjs" + "module": "./index.mjs", + "commonjs": "./index.cjs" } } ``` @@ -1149,7 +1148,7 @@ of these top-level routines unless stated otherwise. _isMain_ is **true** when resolving the Node.js application entry point. _defaultEnv_ is the conditional environment name priority array, -`["node", "default"]`. +`["node", "module"]`.
Resolver algorithm specification diff --git a/doc/api/modules.md b/doc/api/modules.md index 42f42c07fd7b6d..298af58d37af18 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -241,7 +241,8 @@ RESOLVE_BARE_SPECIFIER(DIR, X) g. If no such key can be found, throw "not found". h. let RESOLVED_URL = PACKAGE_EXPORTS_TARGET_RESOLVE(pathToFileURL(DIR/name), exports[key], - subpath.slice(key.length)), as defined in the ESM resolver. + subpath.slice(key.length), ["node", "commonjs"]), as defined in the ESM + resolver. i. return fileURLToPath(RESOLVED_URL) 3. return DIR/X ``` diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index f4112a6a0e6717..77e4cf3b0b272e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -585,9 +585,9 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { } } else if (typeof target === 'object' && target !== null) { if (experimentalConditionalExports && - ObjectPrototypeHasOwnProperty(target, 'require')) { + ObjectPrototypeHasOwnProperty(target, 'node')) { try { - const result = resolveExportsTarget(pkgPath, target.require, subpath, + const result = resolveExportsTarget(pkgPath, target.node, subpath, basePath, mappingKey); emitExperimentalWarning('Conditional exports'); return result; @@ -596,24 +596,16 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { } } if (experimentalConditionalExports && - ObjectPrototypeHasOwnProperty(target, 'node')) { + ObjectPrototypeHasOwnProperty(target, 'commonjs')) { try { - const result = resolveExportsTarget(pkgPath, target.node, subpath, - basePath, mappingKey); + const result = resolveExportsTarget(pkgPath, target.commonjs, subpath, + basePath, mappingKey); emitExperimentalWarning('Conditional exports'); return result; } catch (e) { if (e.code !== 'MODULE_NOT_FOUND') throw e; } } - if (ObjectPrototypeHasOwnProperty(target, 'default')) { - try { - return resolveExportsTarget(pkgPath, target.default, subpath, - basePath, mappingKey); - } catch (e) { - if (e.code !== 'MODULE_NOT_FOUND') throw e; - } - } } let e; if (mappingKey !== '.') { diff --git a/src/env.h b/src/env.h index b3f1243f77584b..889ce2cad79865 100644 --- a/src/env.h +++ b/src/env.h @@ -201,7 +201,6 @@ constexpr size_t kFsStatsBufferLength = V(crypto_rsa_pss_string, "rsa-pss") \ V(cwd_string, "cwd") \ V(data_string, "data") \ - V(default_string, "default") \ V(dest_string, "dest") \ V(destroyed_string, "destroyed") \ V(detached_string, "detached") \ diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 2fa8ba498edcb7..47ea9e2e9d7a1c 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -967,10 +967,10 @@ Maybe ResolveExportsTarget(Environment* env, return resolved; } } - if (target_obj->HasOwnProperty(context, env->default_string()).FromJust()) { + if (target_obj->HasOwnProperty(context, env->module_string()).FromJust()) { matched = true; conditionalTarget = - target_obj->Get(context, env->default_string()).ToLocalChecked(); + target_obj->Get(context, env->module_string()).ToLocalChecked(); Maybe resolved = ResolveExportsTarget(env, pjson_url, conditionalTarget, subpath, pkg_subpath, base, false); if (!resolved.IsNothing()) { diff --git a/test/es-module/test-esm-exports.mjs b/test/es-module/test-esm-exports.mjs index 73a86793a23863..fd85e155b0af21 100644 --- a/test/es-module/test-esm-exports.mjs +++ b/test/es-module/test-esm-exports.mjs @@ -31,7 +31,7 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; ['pkgexports-sugar', { default: 'main' }], // Conditional object exports sugar ['pkgexports-sugar2', isRequire ? { default: 'not-exported' } : - { default: 'main' }] + { default: 'main' }], ]); for (const [validSpecifier, expected] of validSpecifiers) { @@ -51,7 +51,7 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; ['pkgexports-number/hidden.js', './hidden.js'], // Sugar cases still encapsulate ['pkgexports-sugar/not-exported.js', './not-exported.js'], - ['pkgexports-sugar2/not-exported.js', './not-exported.js'] + ['pkgexports-sugar2/not-exported.js', './not-exported.js'], ]); const invalidExports = new Map([ @@ -97,6 +97,15 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; })); } + // Conditional export, even with no match, should still be used instead + // of falling back to main + if (isRequire) { + loadFixture('pkgexports-main').catch(mustCall((err) => { + strictEqual(err.code, 'MODULE_NOT_FOUND'); + assertStartsWith(err.message, 'No valid export'); + })); + } + // Covering out bases - not a file is still not a file after dir mapping. loadFixture('pkgexports/sub/not-a-file.js').catch(mustCall((err) => { strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND'); diff --git a/test/fixtures/node_modules/pkgexports-main/main.cjs b/test/fixtures/node_modules/pkgexports-main/main.cjs new file mode 100644 index 00000000000000..b2825bd3c9949b --- /dev/null +++ b/test/fixtures/node_modules/pkgexports-main/main.cjs @@ -0,0 +1 @@ +module.exports = 'cjs'; diff --git a/test/fixtures/node_modules/pkgexports-main/module.mjs b/test/fixtures/node_modules/pkgexports-main/module.mjs new file mode 100644 index 00000000000000..bd1596224653a3 --- /dev/null +++ b/test/fixtures/node_modules/pkgexports-main/module.mjs @@ -0,0 +1 @@ +export default 'module'; diff --git a/test/fixtures/node_modules/pkgexports-main/package.json b/test/fixtures/node_modules/pkgexports-main/package.json new file mode 100644 index 00000000000000..5546af0c84d7fd --- /dev/null +++ b/test/fixtures/node_modules/pkgexports-main/package.json @@ -0,0 +1,6 @@ +{ + "main": "./main.cjs", + "exports": { + "module": "./module.mjs" + } +} diff --git a/test/fixtures/node_modules/pkgexports-sugar-fail/package.json b/test/fixtures/node_modules/pkgexports-sugar-fail/package.json index 0fb05a427a76e2..bfa040647c509e 100644 --- a/test/fixtures/node_modules/pkgexports-sugar-fail/package.json +++ b/test/fixtures/node_modules/pkgexports-sugar-fail/package.json @@ -1,6 +1,6 @@ { "exports": { - "default": "./main.js", + "module": "./main.js", "./main": "./main.js" } } diff --git a/test/fixtures/node_modules/pkgexports-sugar2/package.json b/test/fixtures/node_modules/pkgexports-sugar2/package.json index 139b06665d85e0..3c0957be3871a0 100644 --- a/test/fixtures/node_modules/pkgexports-sugar2/package.json +++ b/test/fixtures/node_modules/pkgexports-sugar2/package.json @@ -1,6 +1,6 @@ { "exports": { - "require": "./not-exported.js", - "default": "./main.js" + "commonjs": "./not-exported.js", + "module": "./main.js" } } diff --git a/test/fixtures/node_modules/pkgexports/package.json b/test/fixtures/node_modules/pkgexports/package.json index 37c28cdc1a950f..5013b134769083 100644 --- a/test/fixtures/node_modules/pkgexports/package.json +++ b/test/fixtures/node_modules/pkgexports/package.json @@ -19,6 +19,6 @@ "./nofallback1": [], "./nofallback2": [null, {}, "builtin:x"], "./nodemodules": "./node_modules/internalpkg/x.js", - "./condition": [{ "require": "./sp ce.js" }, "./asdf.js"] + "./condition": [{ "commonjs": "./sp ce.js" }, "./asdf.js"] } } From 6de8bc8f99dbfe2eebf5c8b17f29d86a5b725bb9 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 4 Dec 2019 22:20:59 -0500 Subject: [PATCH 2/8] module: support default condition until conditional exports unflag --- doc/api/esm.md | 2 ++ lib/internal/modules/cjs/loader.js | 8 ++++++++ src/env.h | 1 + src/module_wrap.cc | 13 ++++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index d7076ad4020f2e..f826067eed3a70 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -349,6 +349,8 @@ The conditions supported in Node.js are matched in the following order: `--experimental-conditional-exports` flag._ 3. `"module"` - matched when the package is not loaded via `require()`. Can be any module format, this field does not set the type interpretation. + _This is currently only supported behind the + `--experimental-conditional-exports` flag._ Using the `"commonjs"` condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 77e4cf3b0b272e..6c37332a1ec446 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -606,6 +606,14 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { if (e.code !== 'MODULE_NOT_FOUND') throw e; } } + if (ObjectPrototypeHasOwnProperty(target, 'default')) { + try { + return resolveExportsTarget(pkgPath, target.default, subpath, + basePath, mappingKey); + } catch (e) { + if (e.code !== 'MODULE_NOT_FOUND') throw e; + } + } } let e; if (mappingKey !== '.') { diff --git a/src/env.h b/src/env.h index 889ce2cad79865..b3f1243f77584b 100644 --- a/src/env.h +++ b/src/env.h @@ -201,6 +201,7 @@ constexpr size_t kFsStatsBufferLength = V(crypto_rsa_pss_string, "rsa-pss") \ V(cwd_string, "cwd") \ V(data_string, "data") \ + V(default_string, "default") \ V(dest_string, "dest") \ V(destroyed_string, "destroyed") \ V(detached_string, "detached") \ diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 47ea9e2e9d7a1c..edb53146360024 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -967,7 +967,8 @@ Maybe ResolveExportsTarget(Environment* env, return resolved; } } - if (target_obj->HasOwnProperty(context, env->module_string()).FromJust()) { + if (env->options()->experimental_conditional_exports && + target_obj->HasOwnProperty(context, env->module_string()).FromJust()) { matched = true; conditionalTarget = target_obj->Get(context, env->module_string()).ToLocalChecked(); @@ -977,6 +978,16 @@ Maybe ResolveExportsTarget(Environment* env, return resolved; } } + if (target_obj->HasOwnProperty(context, env->default_string()).FromJust()) { + matched = true; + conditionalTarget = + target_obj->Get(context, env->default_string()).ToLocalChecked(); + Maybe resolved = ResolveExportsTarget(env, pjson_url, + conditionalTarget, subpath, pkg_subpath, base, false); + if (!resolved.IsNothing()) { + return resolved; + } + } if (matched && throw_invalid) { Maybe resolved = ResolveExportsTarget(env, pjson_url, conditionalTarget, subpath, pkg_subpath, base, true); From ff6132429e6c68aecff8c14229dffc43c0c00d01 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 5 Dec 2019 18:28:41 -0500 Subject: [PATCH 3/8] change to use require/import --- doc/api/esm.md | 30 +++++++++---------- doc/api/modules.md | 2 +- lib/internal/modules/cjs/loader.js | 6 ++-- src/env.h | 1 + src/module_wrap.cc | 4 +-- .../pkgexports-sugar2/package.json | 4 +-- .../node_modules/pkgexports/package.json | 2 +- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index f826067eed3a70..8b763ee46fd19c 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -329,7 +329,7 @@ Node.js and the browser can be written: "main": "./index.js", "exports": { "./feature": { - "module": "./feature-default.js", + "import": "./feature-default.js", "browser": "./feature-browser.js" } } @@ -344,15 +344,15 @@ The conditions supported in Node.js are matched in the following order: 1. `"node"` - matched for any Node.js environment. Can be a CommonJS or ES module file. _This is currently only supported behind the `--experimental-conditional-exports` flag._ -2. `"commonjs"` - matched when the package is loaded via `require()`. +2. `"require"` - matched when the package is loaded via `require()`. _This is currently only supported behind the `--experimental-conditional-exports` flag._ -3. `"module"` - matched when the package is not loaded via `require()`. +3. `"import"` - matched when the package is not loaded via `require()`. Can be any module format, this field does not set the type interpretation. _This is currently only supported behind the `--experimental-conditional-exports` flag._ -Using the `"commonjs"` condition it is possible to define a package that will +Using the `"require"` condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a hazard in that it can result in having two separate instances of the same package in use in an application, which can cause a number of bugs. @@ -395,8 +395,8 @@ from exports subpaths. { "exports": { ".": { - "module": "./main.js", - "commonjs": "./main.cjs" + "import": "./main.js", + "require": "./main.cjs" } } } @@ -408,8 +408,8 @@ can be written: ```js { "exports": { - "module": "./main.js", - "commonjs": "./main.cjs" + "import": "./main.js", + "require": "./main.cjs" } } ``` @@ -423,8 +423,8 @@ thrown: // Throws on resolution! "exports": { "./feature": "./lib/feature.js", - "module": "./main.js", - "commonjs": "./main.cjs" + "import": "./main.js", + "require": "./main.cjs" } } ``` @@ -520,8 +520,8 @@ ES module wrapper is used for `import` and the CommonJS entry point for "type": "module", "main": "./index.cjs", "exports": { - "commonjs": "./index.cjs", - "module": "./wrapper.mjs" + "require": "./index.cjs", + "import": "./wrapper.mjs" } } ``` @@ -606,8 +606,8 @@ CommonJS and ES module entry points directly (requires "type": "module", "main": "./index.cjs", "exports": { - "module": "./index.mjs", - "commonjs": "./index.cjs" + "import": "./index.mjs", + "require": "./index.cjs" } } ``` @@ -1150,7 +1150,7 @@ of these top-level routines unless stated otherwise. _isMain_ is **true** when resolving the Node.js application entry point. _defaultEnv_ is the conditional environment name priority array, -`["node", "module"]`. +`["node", "import"]`.
Resolver algorithm specification diff --git a/doc/api/modules.md b/doc/api/modules.md index 298af58d37af18..5653fe8f4245bd 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -241,7 +241,7 @@ RESOLVE_BARE_SPECIFIER(DIR, X) g. If no such key can be found, throw "not found". h. let RESOLVED_URL = PACKAGE_EXPORTS_TARGET_RESOLVE(pathToFileURL(DIR/name), exports[key], - subpath.slice(key.length), ["node", "commonjs"]), as defined in the ESM + subpath.slice(key.length), ["node", "require"]), as defined in the ESM resolver. i. return fileURLToPath(RESOLVED_URL) 3. return DIR/X diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 6c37332a1ec446..acdd14b959895b 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -596,10 +596,10 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { } } if (experimentalConditionalExports && - ObjectPrototypeHasOwnProperty(target, 'commonjs')) { + ObjectPrototypeHasOwnProperty(target, 'require')) { try { - const result = resolveExportsTarget(pkgPath, target.commonjs, subpath, - basePath, mappingKey); + const result = resolveExportsTarget(pkgPath, target.require, subpath, + basePath, mappingKey); emitExperimentalWarning('Conditional exports'); return result; } catch (e) { diff --git a/src/env.h b/src/env.h index b3f1243f77584b..8cd56b4dd66343 100644 --- a/src/env.h +++ b/src/env.h @@ -255,6 +255,7 @@ constexpr size_t kFsStatsBufferLength = V(hostmaster_string, "hostmaster") \ V(http_1_1_string, "http/1.1") \ V(ignore_string, "ignore") \ + V(import_string, "import") \ V(infoaccess_string, "infoAccess") \ V(inherit_string, "inherit") \ V(input_string, "input") \ diff --git a/src/module_wrap.cc b/src/module_wrap.cc index edb53146360024..568e1ad2fdbba0 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -968,10 +968,10 @@ Maybe ResolveExportsTarget(Environment* env, } } if (env->options()->experimental_conditional_exports && - target_obj->HasOwnProperty(context, env->module_string()).FromJust()) { + target_obj->HasOwnProperty(context, env->import_string()).FromJust()) { matched = true; conditionalTarget = - target_obj->Get(context, env->module_string()).ToLocalChecked(); + target_obj->Get(context, env->import_string()).ToLocalChecked(); Maybe resolved = ResolveExportsTarget(env, pjson_url, conditionalTarget, subpath, pkg_subpath, base, false); if (!resolved.IsNothing()) { diff --git a/test/fixtures/node_modules/pkgexports-sugar2/package.json b/test/fixtures/node_modules/pkgexports-sugar2/package.json index 3c0957be3871a0..bef4889bea8930 100644 --- a/test/fixtures/node_modules/pkgexports-sugar2/package.json +++ b/test/fixtures/node_modules/pkgexports-sugar2/package.json @@ -1,6 +1,6 @@ { "exports": { - "commonjs": "./not-exported.js", - "module": "./main.js" + "require": "./not-exported.js", + "import": "./main.js" } } diff --git a/test/fixtures/node_modules/pkgexports/package.json b/test/fixtures/node_modules/pkgexports/package.json index 5013b134769083..37c28cdc1a950f 100644 --- a/test/fixtures/node_modules/pkgexports/package.json +++ b/test/fixtures/node_modules/pkgexports/package.json @@ -19,6 +19,6 @@ "./nofallback1": [], "./nofallback2": [null, {}, "builtin:x"], "./nodemodules": "./node_modules/internalpkg/x.js", - "./condition": [{ "commonjs": "./sp ce.js" }, "./asdf.js"] + "./condition": [{ "require": "./sp ce.js" }, "./asdf.js"] } } From 7483163186bef066ae26bdb74b853a348a9cebcd Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 9 Dec 2019 12:57:13 -0500 Subject: [PATCH 4/8] update condition descriptions --- doc/api/esm.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 8b763ee46fd19c..3903e80207eab8 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -344,10 +344,12 @@ The conditions supported in Node.js are matched in the following order: 1. `"node"` - matched for any Node.js environment. Can be a CommonJS or ES module file. _This is currently only supported behind the `--experimental-conditional-exports` flag._ -2. `"require"` - matched when the package is loaded via `require()`. +2. `"require"` - matched when the package is loaded with the CommonJS + module resolver such as via `require()`. _This is currently only supported behind the `--experimental-conditional-exports` flag._ -3. `"import"` - matched when the package is not loaded via `require()`. +3. `"import"` - matched when the package is imported with the ES module + resolver such as when using a static or dynamic import expression. Can be any module format, this field does not set the type interpretation. _This is currently only supported behind the `--experimental-conditional-exports` flag._ From 1fe09b1b1142ae6fe088144418fb0f329ffbee25 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 9 Dec 2019 13:11:25 -0500 Subject: [PATCH 5/8] remove resolver references from condition definitions --- doc/api/esm.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 3903e80207eab8..550a326bfab5ef 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -343,16 +343,14 @@ The conditions supported in Node.js are matched in the following order: 1. `"node"` - matched for any Node.js environment. Can be a CommonJS or ES module file. _This is currently only supported behind the - `--experimental-conditional-exports` flag._ -2. `"require"` - matched when the package is loaded with the CommonJS - module resolver such as via `require()`. - _This is currently only supported behind the - `--experimental-conditional-exports` flag._ -3. `"import"` - matched when the package is imported with the ES module - resolver such as when using a static or dynamic import expression. - Can be any module format, this field does not set the type interpretation. + `--experimental-conditional-exports` flag._ +2. `"require"` - matched when the package is loaded via `require()`. _This is currently only supported behind the - `--experimental-conditional-exports` flag._ + `--experimental-conditional-exports` flag._ +3. `"import"` - matched when the package is loaded via `import` or + `import()`. Can be any module format, this field does not set the type + interpretation. _This is currently only supported behind the + `--experimental-conditional-exports` flag._ Using the `"require"` condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a From 59baa76c26bb1f2cc1650c633d59b6989a7107d2 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 9 Dec 2019 19:31:07 -0500 Subject: [PATCH 6/8] maintain "default" in documentation --- doc/api/esm.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/api/esm.md b/doc/api/esm.md index 550a326bfab5ef..a2ea05cac5c5fd 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -351,6 +351,9 @@ The conditions supported in Node.js are matched in the following order: `import()`. Can be any module format, this field does not set the type interpretation. _This is currently only supported behind the `--experimental-conditional-exports` flag._ +4. `"default"` - the generic fallback that will always match if no other + more specific condition is matched first. Can be a CommonJS or ES module + file. Using the `"require"` condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a From c7fe517d6a6ec65ed22bb25f5208325a9d6f532c Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 9 Dec 2019 20:43:49 -0500 Subject: [PATCH 7/8] add warning note --- doc/api/esm.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/api/esm.md b/doc/api/esm.md index a2ea05cac5c5fd..e0b26db0c01e7c 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -354,6 +354,10 @@ The conditions supported in Node.js are matched in the following order: 4. `"default"` - the generic fallback that will always match if no other more specific condition is matched first. Can be a CommonJS or ES module file. + +> Setting any of the above flagged conditions for a published package is not +recommended until they are unflagged to avoid breaking changes to packages in +future. Using the `"require"` condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a From ecd1017ad7d9dd62d55460e4c5b9593cb64facd9 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 10 Dec 2019 00:38:31 -0500 Subject: [PATCH 8/8] linting fixes, update other msg --- doc/api/esm.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index e0b26db0c01e7c..5245cef465e3c7 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -354,10 +354,10 @@ The conditions supported in Node.js are matched in the following order: 4. `"default"` - the generic fallback that will always match if no other more specific condition is matched first. Can be a CommonJS or ES module file. - + > Setting any of the above flagged conditions for a published package is not -recommended until they are unflagged to avoid breaking changes to packages in -future. +> recommended until they are unflagged to avoid breaking changes to packages in +> future. Using the `"require"` condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a @@ -516,9 +516,8 @@ ES module wrapper is used for `import` and the CommonJS entry point for `require`. > Note: While `--experimental-conditional-exports` is flagged, a package -> using this pattern will throw when loaded via `require()` in modern -> Node.js, unless package consumers use the `--experimental-conditional-exports` -> flag. +> using this pattern will throw when loaded unless package consumers use the +> `--experimental-conditional-exports` flag. ```js