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

Follow up to #843, refining exports and browser field interaction #866

Merged
merged 10 commits into from Apr 30, 2021
12 changes: 6 additions & 6 deletions packages/node-resolve/README.md
Expand Up @@ -34,9 +34,9 @@ export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
format: 'cjs',
},
plugins: [nodeResolve()]
plugins: [nodeResolve()],
};
```

Expand Down Expand Up @@ -64,7 +64,7 @@ Setting this option will add extra conditions on top of the default conditions.
Type: `Boolean`<br>
Default: `false`

If `true`, instructs the plugin to use the `"browser"` property in `package.json` files to specify alternative files to load for bundling. This is useful when bundling for a browser environment. Alternatively, a value of `'browser'` can be added to the `mainFields` option. If `false`, any `"browser"` properties in package files will be ignored. This option takes precedence over `mainFields`.
If `true`, instructs the plugin to use the browser module resolutions in `package.json` and adds `'browser'` to `exportConditions` if it is not present so browser conditionals in `exports` are applied. If `false`, any browser properties in package files will be ignored. Alternatively, a value of `'browser'` can be added to the `mainFields` option, however this option takes precedence over `mainFields`.
guybedford marked this conversation as resolved.
Show resolved Hide resolved

> This option does not work when a package is using [package entrypoints](https://nodejs.org/api/packages.html#packages_package_entry_points)

Expand Down Expand Up @@ -179,9 +179,9 @@ export default {
output: {
file: 'bundle.js',
format: 'iife',
name: 'MyModule'
name: 'MyModule',
},
plugins: [nodeResolve(), commonjs()]
plugins: [nodeResolve(), commonjs()],
};
```

Expand Down Expand Up @@ -216,7 +216,7 @@ The node resolve plugin uses `import` by default, you can opt into using the `re
```js
this.resolve(importee, importer, {
skipSelf: true,
custom: { 'node-resolve': { isRequire: true } }
custom: { 'node-resolve': { isRequire: true } },
});
```

Expand Down
4 changes: 4 additions & 0 deletions packages/node-resolve/src/index.js
Expand Up @@ -22,6 +22,7 @@ const deepFreeze = (object) => {
}
}


return object;
};

Expand Down Expand Up @@ -161,6 +162,9 @@ export function nodeResolve(opts = {}) {
opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
const exportConditions = isRequire ? conditionsCjs : conditionsEsm;

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

const resolvedWithoutBuiltins = await resolveImportSpecifiers({
importer,
importSpecifierList,
Expand Down
4 changes: 2 additions & 2 deletions packages/node-resolve/src/package/resolvePackageTarget.js
Expand Up @@ -24,11 +24,11 @@ async function resolvePackageTarget(context, { target, subpath, pattern, interna
target.replace(/\*/g, subpath),
context.pkgURL.href
);
return result ? pathToFileURL(result.location) : null;
return result ? pathToFileURL(result.location).href : null;
}

const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
return result ? pathToFileURL(result.location) : null;
return result ? pathToFileURL(result.location).href : null;
}
throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/node-resolve/test/browser.js
Expand Up @@ -199,3 +199,23 @@ test('pkg.browser with mapping to prevent bundle by specifying a value of false'

t.is(module.exports, 'ok');
});

test('exports.browser can be mapped via pkg.browser', async (t) => {
const bundle = await rollup({
input: 'browser-exports-browser-browser.js',
plugins: [nodeResolve({ browser: true }), commonjs()]
});
const { module } = await testBundle(t, bundle);

t.is(module.exports, 'browser');
});

test('exports.browser does not take precedence over export map result, when browser:false', async (t) => {
guybedford marked this conversation as resolved.
Show resolved Hide resolved
const bundle = await rollup({
input: 'browser-exports-browser.js',
plugins: [nodeResolve({browser: true}), commonjs()]
});
const { module } = await testBundle(t, bundle);

t.is(module.exports, 'require');
});
@@ -0,0 +1,3 @@
import b from 'exports-browser-browser';

module.exports = b;
@@ -0,0 +1,3 @@
const b = require('exports-browser');

module.exports = b;
@@ -0,0 +1,3 @@
const Nanoid = require('nanoid');

module.exports = Nanoid;

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.

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.

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