From 5fd201cfb55d2486ac16754861752bae035d76d9 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:24:37 -0800 Subject: [PATCH] feat(node-resolve): set development or production condition --- packages/node-resolve/README.md | 2 +- packages/node-resolve/src/index.js | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/node-resolve/README.md b/packages/node-resolve/README.md index 6866a6853..11f5b5dc0 100755 --- a/packages/node-resolve/README.md +++ b/packages/node-resolve/README.md @@ -53,7 +53,7 @@ This plugin supports the package entrypoints feature from node js, specified in Type: `Array[...String]`
Default: `[]` -Additional conditions of the package.json exports field to match when resolving modules. By default, this plugin looks for the `['default', 'module', 'import']` conditions when resolving imports. +Additional conditions of the package.json exports field to match when resolving modules. By default, this plugin looks for the `['default', 'module', 'import', 'development|production']` conditions when resolving imports. If neither the `development` or `production` conditions are provided it will default to `production` - or `development` if `NODE_ENV` is set to a value other than `production`. When using `@rollup/plugin-commonjs` v16 or higher, this plugin will use the `['default', 'module', 'require']` conditions when resolving require statements. diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index a675247aa..3cf0caa3a 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -52,8 +52,17 @@ export function nodeResolve(opts = {}) { const options = { ...defaults, ...opts }; const { extensions, jail, moduleDirectories, modulePaths, ignoreSideEffectsForRoot } = options; - const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])]; - const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])]; + const exportConditions = options.exportConditions || []; + const devProdCondition = + exportConditions.includes('development') || exportConditions.includes('production') + ? [] + : [ + process.env.NODE_ENV && process.env.NODE_ENV !== 'production' + ? 'development' + : 'production' + ]; + const conditionsEsm = [...baseConditionsEsm, ...exportConditions, ...devProdCondition]; + const conditionsCjs = [...baseConditionsCjs, ...exportConditions, ...devProdCondition]; const packageInfoCache = new Map(); const idToPackageInfo = new Map(); const mainFields = getMainFields(options);