Skip to content

Commit

Permalink
feat(node-resolve): set development or production condition
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Dec 2, 2024
1 parent 92daef0 commit 5fd201c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/node-resolve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ This plugin supports the package entrypoints feature from node js, specified in
Type: `Array[...String]`<br>
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.

Expand Down
13 changes: 11 additions & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5fd201c

Please sign in to comment.