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..2ce5738f1 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); @@ -171,7 +180,6 @@ export function nodeResolve(opts = {}) { const warn = (...args) => context.warn(...args); const isRequire = custom && custom['node-resolve'] && custom['node-resolve'].isRequire; const exportConditions = isRequire ? conditionsCjs : conditionsEsm; - if (useBrowserOverrides && !exportConditions.includes('browser')) exportConditions.push('browser'); diff --git a/packages/node-resolve/test/fixtures/dev-prod-conditions.js b/packages/node-resolve/test/fixtures/dev-prod-conditions.js new file mode 100644 index 000000000..525d39048 --- /dev/null +++ b/packages/node-resolve/test/fixtures/dev-prod-conditions.js @@ -0,0 +1,3 @@ +import mode from 'dev-prod-conditions'; + +export default mode; diff --git a/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/dev.js b/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/dev.js new file mode 100644 index 000000000..5a230d745 --- /dev/null +++ b/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/dev.js @@ -0,0 +1 @@ +export default 'DEV' diff --git a/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/index.js b/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/index.js new file mode 100644 index 000000000..9373073b4 --- /dev/null +++ b/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/index.js @@ -0,0 +1 @@ +export default 'PROD' diff --git a/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/package.json b/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/package.json new file mode 100644 index 000000000..d681bc409 --- /dev/null +++ b/packages/node-resolve/test/fixtures/node_modules/dev-prod-conditions/package.json @@ -0,0 +1,8 @@ +{ + "name": "dev-prod-conditions", + "type": "module", + "exports": { + "development": "./dev.js", + "default": "./index.js" + } +} diff --git a/packages/node-resolve/test/package-entry-points.js b/packages/node-resolve/test/package-entry-points.js index 371a815a3..42932d90a 100644 --- a/packages/node-resolve/test/package-entry-points.js +++ b/packages/node-resolve/test/package-entry-points.js @@ -413,3 +413,13 @@ test('custom condition takes precedence over browser field with `browser: true`' t.deepEqual(module.exports, 'FROM WEBWORKER CONDITION'); }); + +test('development condition is used when NODE_ENV is not production', async (t) => { + const bundle = await rollup({ + input: 'dev-prod-conditions.js', + plugins: [nodeResolve()] + }); + const { module } = await testBundle(t, bundle); + + t.deepEqual(module.exports, 'DEV'); +});