From 59c6cea406fa26400b647a6a031ec4d9270cd30f Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Tue, 23 Aug 2022 19:31:48 +0800 Subject: [PATCH] docs(Configuration): explain more on the condition names (#6330) * explain more on the condition names * Update src/content/configuration/resolve.mdx Co-authored-by: Sam Chen * Update resolve.mdx Co-authored-by: Sam Chen --- src/content/configuration/resolve.mdx | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/content/configuration/resolve.mdx b/src/content/configuration/resolve.mdx index a24d895b2025..72e593b07190 100644 --- a/src/content/configuration/resolve.mdx +++ b/src/content/configuration/resolve.mdx @@ -193,6 +193,52 @@ module.exports = { }; ``` +Webpack will match [export conditions](https://nodejs.org/api/packages.html#conditional-exports) that are listed within the `resolve.conditionNames` array. + +The key order in the `exports` field is significant. During condition matching, earlier entries have higher priority and take precedence over later entries. + +For example, + +**package.json** + +```json +{ + "name": "foo", + "exports": { + ".": { + "import": "./index-import.js", + "require": "./index-require.js", + "node": "./index-node.js" + }, + "./bar": { + "node": "./bar-node.js", + "require": "./bar-require.js" + }, + "./baz": { + "import": "./baz-import.js", + "node": "./baz-node.js" + } + } +} +``` + +**webpack.config.js** + +```js +module.exports = { + //... + resolve: { + conditionNames: ['require', 'node'], + }, +}; +``` + +importing + +- `'foo'` will resolve to `'foo/index-require.js'` +- `'foo/bar'` will resolve to `'foo/bar-node.js'` as the `"node"` key comes before `"require"` key in the conditional exports object. +- `'foo/baz'` will resolve to `'foo/baz-node.js'` + ### resolve.descriptionFiles `[string] = ['package.json']`