Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
Closed
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export default {
// for the browser. If that's you, use this option, otherwise
// pkg.browser will be ignored
browser: true, // Default: false
// browser can also be specified as an array of strings and/or regexps
// so that only modules that match at least one entry use the browser
// field. ex. browser: [ 'some_module', /^@some_scope\/.*$/ ],

// not all files you want to resolve are .js files
extensions: [ '.mjs', '.js', '.jsx', '.json' ], // Default: [ '.mjs', '.js', '.json', '.node' ]
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

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

22 changes: 14 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ function cachedIsFile (file, cb) {
isFileCache[file].then(contents => cb(null, contents), cb);
}

const moduleFilter = (filter) => Array.isArray(filter)
? filter.map(o => o instanceof RegExp
? o
: new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$')
)
: null;

export default function nodeResolve ( options = {} ) {
const useModule = options.module !== false;
const useMain = options.main !== false;
Expand All @@ -46,12 +53,7 @@ export default function nodeResolve ( options = {} ) {
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
const customResolveOptions = options.customResolveOptions || {};
const jail = options.jail;
const only = Array.isArray(options.only)
? options.only.map(o => o instanceof RegExp
? o
: new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$')
)
: null;
const only = moduleFilter(options.only);
const browserMapCache = {};

const onwarn = options.onwarn || CONSOLE_WARN;
Expand Down Expand Up @@ -118,7 +120,11 @@ export default function nodeResolve ( options = {} ) {
basedir: dirname( importer ),
packageFilter ( pkg, pkgPath ) {
const pkgRoot = dirname( pkgPath );
if (options.browser && typeof pkg[ 'browser' ] === 'object') {
const browserFilter = moduleFilter(options.browser);
const useBrowser = options.browser && (
!browserFilter || browserFilter.some(pattern => pattern.test(id))
);
if (useBrowser && typeof pkg[ 'browser' ] === 'object') {
packageBrowserField = Object.keys(pkg[ 'browser' ]).reduce((browser, key) => {
const resolved = pkg[ 'browser' ][ key ] === false ? false : resolve( pkgRoot, pkg[ 'browser' ][ key ] );
browser[ key ] = resolved;
Expand All @@ -136,7 +142,7 @@ export default function nodeResolve ( options = {} ) {
}, {});
}

if (options.browser && typeof pkg[ 'browser' ] === 'string') {
if (useBrowser && typeof pkg[ 'browser' ] === 'string') {
pkg[ 'main' ] = pkg[ 'browser' ];
} else if ( useModule && pkg[ 'module' ] ) {
pkg[ 'main' ] = pkg[ 'module' ];
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ describe( 'rollup-plugin-node-resolve', function () {
});
});

it( 'disregards browser field for modules not in browser array', function () {
return rollup.rollup({
input: 'samples/browser/main.js',
plugins: [
nodeResolve({
main: true,
browser: ['not-a-module']
})
]
}).then( executeBundle ).then( module => {
assert.equal( module.exports, 'node' );
});
});

it( 'allows use of the browser field for module in browser array', function () {
return rollup.rollup({
input: 'samples/browser/main.js',
plugins: [
nodeResolve({
main: true,
browser: ['isomorphic']
})
]
}).then( executeBundle ).then( module => {
assert.equal( module.exports, 'browser' );
});
});

it( 'disregards object browser field by default', function () {
return rollup.rollup({
input: 'samples/browser-object/main.js',
Expand Down