Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node-resolve)!: set development or production condition #1823

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@ -170,8 +179,7 @@

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');
});
Loading