| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -335,8 +335,8 @@ Node.js and the browser can be written: | |||
| 335 | 335 | "main": "./index.js", | |
| 336 | 336 | "exports": { | |
| 337 | 337 | "./feature": { | |
| 338 | - "browser": "./feature-browser.js", | ||
| 339 | - "default": "./feature-default.js" | ||
| 338 | + "import": "./feature-default.js", | ||
| 339 | + "browser": "./feature-browser.js" | ||
| 340 | 340 | } | |
| 341 | 341 | } | |
| 342 | 342 | } | |
@@ -347,16 +347,24 @@ will be used as the final fallback. | |||
| 347 | 347 | ||
| 348 | 348 | The conditions supported in Node.js are matched in the following order: | |
| 349 | 349 | ||
| 350 | - 1. `"require"` - matched when the package is loaded via `require()`. | ||
| 351 | - _This is currently only supported behind the | ||
| 352 | - `--experimental-conditional-exports` flag._ | ||
| 353 | - 2. `"node"` - matched for any Node.js environment. Can be a CommonJS or ES | ||
| 350 | + 1. `"node"` - matched for any Node.js environment. Can be a CommonJS or ES | ||
| 354 | 351 | module file. _This is currently only supported behind the | |
| 355 | - `--experimental-conditional-exports` flag._ | ||
| 356 | - 3. `"default"` - the generic fallback that will always match if no other | ||
| 352 | + `--experimental-conditional-exports` flag._ | ||
| 353 | + 2. `"require"` - matched when the package is loaded via `require()`. | ||
| 354 | + _This is currently only supported behind the | ||
| 355 | + `--experimental-conditional-exports` flag._ | ||
| 356 | + 3. `"import"` - matched when the package is loaded via `import` or | ||
| 357 | + `import()`. Can be any module format, this field does not set the type | ||
| 358 | + interpretation. _This is currently only supported behind the | ||
| 359 | + `--experimental-conditional-exports` flag._ | ||
| 360 | + 4. `"default"` - the generic fallback that will always match if no other | ||
| 357 | 361 | more specific condition is matched first. Can be a CommonJS or ES module | |
| 358 | 362 | file. | |
| 359 | 363 | ||
| 364 | + > Setting any of the above flagged conditions for a published package is not | ||
| 365 | + > recommended until they are unflagged to avoid breaking changes to packages in | ||
| 366 | + > future. | ||
| 367 | + | ||
| 360 | 368 | Using the `"require"` condition it is possible to define a package that will | |
| 361 | 369 | have a different exported value for CommonJS and ES modules, which can be a | |
| 362 | 370 | hazard in that it can result in having two separate instances of the same | |
@@ -400,8 +408,8 @@ from exports subpaths. | |||
| 400 | 408 | { | |
| 401 | 409 | "exports": { | |
| 402 | 410 | ".": { | |
| 403 | - "require": "./main.cjs", | ||
| 404 | - "default": "./main.js" | ||
| 411 | + "import": "./main.js", | ||
| 412 | + "require": "./main.cjs" | ||
| 405 | 413 | } | |
| 406 | 414 | } | |
| 407 | 415 | } | |
@@ -413,8 +421,8 @@ can be written: | |||
| 413 | 421 | ```js | |
| 414 | 422 | { | |
| 415 | 423 | "exports": { | |
| 416 | - "require": "./main.cjs", | ||
| 417 | - "default": "./main.js" | ||
| 424 | + "import": "./main.js", | ||
| 425 | + "require": "./main.cjs" | ||
| 418 | 426 | } | |
| 419 | 427 | } | |
| 420 | 428 | ``` | |
@@ -428,8 +436,8 @@ thrown: | |||
| 428 | 436 | // Throws on resolution! | |
| 429 | 437 | "exports": { | |
| 430 | 438 | "./feature": "./lib/feature.js", | |
| 431 | - "require": "./main.cjs", | ||
| 432 | - "default": "./main.js" | ||
| 439 | + "import": "./main.js", | ||
| 440 | + "require": "./main.cjs" | ||
| 433 | 441 | } | |
| 434 | 442 | } | |
| 435 | 443 | ``` | |
@@ -514,9 +522,8 @@ ES module wrapper is used for `import` and the CommonJS entry point for | |||
| 514 | 522 | `require`. | |
| 515 | 523 | ||
| 516 | 524 | > Note: While `--experimental-conditional-exports` is flagged, a package | |
| 517 | - > using this pattern will throw when loaded via `require()` in modern | ||
| 518 | - > Node.js, unless package consumers use the `--experimental-conditional-exports` | ||
| 519 | - > flag. | ||
| 525 | + > using this pattern will throw when loaded unless package consumers use the | ||
| 526 | + > `--experimental-conditional-exports` flag. | ||
| 520 | 527 | ||
| 521 | 528 | <!-- eslint-skip --> | |
| 522 | 529 | ```js | |
@@ -526,7 +533,7 @@ ES module wrapper is used for `import` and the CommonJS entry point for | |||
| 526 | 533 | "main": "./index.cjs", | |
| 527 | 534 | "exports": { | |
| 528 | 535 | "require": "./index.cjs", | |
| 529 | - "default": "./wrapper.mjs" | ||
| 536 | + "import": "./wrapper.mjs" | ||
| 530 | 537 | } | |
| 531 | 538 | } | |
| 532 | 539 | ``` | |
@@ -611,8 +618,8 @@ CommonJS and ES module entry points directly (requires | |||
| 611 | 618 | "type": "module", | |
| 612 | 619 | "main": "./index.cjs", | |
| 613 | 620 | "exports": { | |
| 614 | - "require": "./index.cjs", | ||
| 615 | - "default": "./index.mjs" | ||
| 621 | + "import": "./index.mjs", | ||
| 622 | + "require": "./index.cjs" | ||
| 616 | 623 | } | |
| 617 | 624 | } | |
| 618 | 625 | ``` | |
@@ -1152,7 +1159,7 @@ of these top-level routines unless stated otherwise. | |||
| 1152 | 1159 | _isMain_ is **true** when resolving the Node.js application entry point. | |
| 1153 | 1160 | ||
| 1154 | 1161 | _defaultEnv_ is the conditional environment name priority array, | |
| 1155 | - `["node", "default"]`. | ||
| 1162 | + `["node", "import"]`. | ||
| 1156 | 1163 | ||
| 1157 | 1164 | <details> | |
| 1158 | 1165 | <summary>Resolver algorithm specification</summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -241,7 +241,8 @@ RESOLVE_BARE_SPECIFIER(DIR, X) | |||
| 241 | 241 | g. If no such key can be found, throw "not found". | |
| 242 | 242 | h. let RESOLVED_URL = | |
| 243 | 243 | PACKAGE_EXPORTS_TARGET_RESOLVE(pathToFileURL(DIR/name), exports[key], | |
| 244 | - subpath.slice(key.length)), as defined in the ESM resolver. | ||
| 244 | + subpath.slice(key.length), ["node", "require"]), as defined in the ESM | ||
| 245 | + resolver. | ||
| 245 | 246 | i. return fileURLToPath(RESOLVED_URL) | |
| 246 | 247 | 3. return DIR/X | |
| 247 | 248 | ``` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -574,9 +574,9 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { | |||
| 574 | 574 | } | |
| 575 | 575 | } else if (typeof target === 'object' && target !== null) { | |
| 576 | 576 | if (experimentalConditionalExports && | |
| 577 | - ObjectPrototype.hasOwnProperty(target, 'require')) { | ||
| 577 | + ObjectPrototype.hasOwnProperty(target, 'node')) { | ||
| 578 | 578 | try { | |
| 579 | - const result = resolveExportsTarget(pkgPath, target.require, subpath, | ||
| 579 | + const result = resolveExportsTarget(pkgPath, target.node, subpath, | ||
| 580 | 580 | basePath, mappingKey); | |
| 581 | 581 | emitExperimentalWarning('Conditional exports'); | |
| 582 | 582 | return result; | |
@@ -585,9 +585,9 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) { | |||
| 585 | 585 | } | |
| 586 | 586 | } | |
| 587 | 587 | if (experimentalConditionalExports && | |
| 588 | - ObjectPrototype.hasOwnProperty(target, 'node')) { | ||
| 588 | + ObjectPrototype.hasOwnProperty(target, 'require')) { | ||
| 589 | 589 | try { | |
| 590 | - const result = resolveExportsTarget(pkgPath, target.node, subpath, | ||
| 590 | + const result = resolveExportsTarget(pkgPath, target.require, subpath, | ||
| 591 | 591 | basePath, mappingKey); | |
| 592 | 592 | emitExperimentalWarning('Conditional exports'); | |
| 593 | 593 | return result; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,6 +255,7 @@ constexpr size_t kFsStatsBufferLength = | |||
| 255 | 255 | V(hostmaster_string, "hostmaster") \ | |
| 256 | 256 | V(http_1_1_string, "http/1.1") \ | |
| 257 | 257 | V(ignore_string, "ignore") \ | |
| 258 | + V(import_string, "import") \ | ||
| 258 | 259 | V(infoaccess_string, "infoAccess") \ | |
| 259 | 260 | V(inherit_string, "inherit") \ | |
| 260 | 261 | V(input_string, "input") \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -967,6 +967,17 @@ Maybe<URL> ResolveExportsTarget(Environment* env, | |||
| 967 | 967 | return resolved; | |
| 968 | 968 | } | |
| 969 | 969 | } | |
| 970 | + if (env->options()->experimental_conditional_exports && | ||
| 971 | + target_obj->HasOwnProperty(context, env->import_string()).FromJust()) { | ||
| 972 | + matched = true; | ||
| 973 | + conditionalTarget = | ||
| 974 | + target_obj->Get(context, env->import_string()).ToLocalChecked(); | ||
| 975 | + Maybe<URL> resolved = ResolveExportsTarget(env, pjson_url, | ||
| 976 | + conditionalTarget, subpath, pkg_subpath, base, false); | ||
| 977 | + if (!resolved.IsNothing()) { | ||
| 978 | + return resolved; | ||
| 979 | + } | ||
| 980 | + } | ||
| 970 | 981 | if (target_obj->HasOwnProperty(context, env->default_string()).FromJust()) { | |
| 971 | 982 | matched = true; | |
| 972 | 983 | conditionalTarget = | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,7 +32,7 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; | |||
| 32 | 32 | ['pkgexports-sugar', { default: 'main' }], | |
| 33 | 33 | // Conditional object exports sugar | |
| 34 | 34 | ['pkgexports-sugar2', isRequire ? { default: 'not-exported' } : | |
| 35 | - { default: 'main' }] | ||
| 35 | + { default: 'main' }], | ||
| 36 | 36 | ]); | |
| 37 | 37 | ||
| 38 | 38 | for (const [validSpecifier, expected] of validSpecifiers) { | |
@@ -52,7 +52,7 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; | |||
| 52 | 52 | ['pkgexports-number/hidden.js', './hidden.js'], | |
| 53 | 53 | // Sugar cases still encapsulate | |
| 54 | 54 | ['pkgexports-sugar/not-exported.js', './not-exported.js'], | |
| 55 | - ['pkgexports-sugar2/not-exported.js', './not-exported.js'] | ||
| 55 | + ['pkgexports-sugar2/not-exported.js', './not-exported.js'], | ||
| 56 | 56 | ]); | |
| 57 | 57 | ||
| 58 | 58 | const invalidExports = new Map([ | |
@@ -98,6 +98,15 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; | |||
| 98 | 98 | })); | |
| 99 | 99 | } | |
| 100 | 100 | ||
| 101 | + // Conditional export, even with no match, should still be used instead | ||
| 102 | + // of falling back to main | ||
| 103 | + if (isRequire) { | ||
| 104 | + loadFixture('pkgexports-main').catch(mustCall((err) => { | ||
| 105 | + strictEqual(err.code, 'MODULE_NOT_FOUND'); | ||
| 106 | + assertStartsWith(err.message, 'No valid export'); | ||
| 107 | + })); | ||
| 108 | + } | ||
| 109 | + | ||
| 101 | 110 | // Covering out bases - not a file is still not a file after dir mapping. | |
| 102 | 111 | loadFixture('pkgexports/sub/not-a-file.js').catch(mustCall((err) => { | |
| 103 | 112 | strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments