Skip to content

Commit

Permalink
feat(node-resolve): set development or production condition
Browse files Browse the repository at this point in the history
The `'development'` or `'production'` condition will be set. 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`.
  • Loading branch information
benmccann committed Dec 2, 2024
1 parent 92daef0 commit e7bbc57
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 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
14 changes: 11 additions & 3 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 Expand Up @@ -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;

Check warning on line 182 in packages/node-resolve/src/index.js

View workflow job for this annotation

GitHub Actions / Node v20

'exportConditions' is already declared in the upper scope on line 55 column 9

Check warning on line 182 in packages/node-resolve/src/index.js

View workflow job for this annotation

GitHub Actions / Node v18

'exportConditions' is already declared in the upper scope on line 55 column 9

Check warning on line 182 in packages/node-resolve/src/index.js

View workflow job for this annotation

GitHub Actions / Node v20

'exportConditions' is already declared in the upper scope on line 55 column 9

Check warning on line 182 in packages/node-resolve/src/index.js

View workflow job for this annotation

GitHub Actions / Node v18

'exportConditions' is already declared in the upper scope on line 55 column 9

if (useBrowserOverrides && !exportConditions.includes('browser'))
exportConditions.push('browser');

Expand Down
3 changes: 3 additions & 0 deletions packages/node-resolve/test/fixtures/dev-prod-conditions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import mode from 'dev-prod-conditions';

export default mode;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions packages/node-resolve/test/package-entry-points.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit e7bbc57

Please sign in to comment.