Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

fix(): make empty.js virtual #224

Merged
merged 2 commits into from
Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/empty.js

This file was deleted.

34 changes: 23 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import fs from 'fs';
import {createFilter} from 'rollup-pluginutils';
import {peerDependencies} from '../package.json';

const builtins = builtinList.reduce((set, id) => set.add(id), new Set());
const builtins = new Set(builtinList);

const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );
const ES6_BROWSER_EMPTY = '!node-resolve:empty.js';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is kind of a private module, I would assume it should be prefixed with \0 instead to avoid other plugins from messing with it, e.g. by inserting code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukastaegert fixed!

// It is important that .mjs occur before .js so that Rollup will interpret npm modules
// which deploy both ESM .mjs and CommonJS .js files as ESM.
const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ];
Expand Down Expand Up @@ -91,7 +91,7 @@ export default function nodeResolve ( options = {} ) {
: new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$')
)
: null;
const browserMapCache = {};
const browserMapCache = new Map();

if ( options.skip ) {
throw new Error( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' );
Expand Down Expand Up @@ -179,6 +179,10 @@ export default function nodeResolve ( options = {} ) {
},

resolveId ( importee, importer ) {
if (importee === ES6_BROWSER_EMPTY) {
return importee;
}

if ( /\0/.test( importee ) ) return null; // ignore IDs with null character, these belong to other plugins

const basedir = importer ? dirname( importer ) : process.cwd();
Expand All @@ -188,21 +192,22 @@ export default function nodeResolve ( options = {} ) {
}

// https://github.com/defunctzombie/package-browser-field-spec
if (useBrowserOverrides && browserMapCache[importer]) {
const browser = browserMapCache.get(importer);
if (useBrowserOverrides && browser) {
const resolvedImportee = resolve( basedir, importee );
const browser = browserMapCache[importer];
if (browser[importee] === false || browser[resolvedImportee] === false) {
return ES6_BROWSER_EMPTY;
}
if (browser[importee] || browser[resolvedImportee] || browser[resolvedImportee + '.js'] || browser[resolvedImportee + '.json']) {
importee = browser[importee] || browser[resolvedImportee] || browser[resolvedImportee + '.js'] || browser[resolvedImportee + '.json'];
const browserImportee = browser[importee] || browser[resolvedImportee] || browser[resolvedImportee + '.js'] || browser[resolvedImportee + '.json'];
if (browserImportee) {
importee = browserImportee;
}
}

const parts = importee.split( /[/\\]/ );
let id = parts.shift();

if ( id[0] === '@' && parts.length ) {
if ( id[0] === '@' && parts.length > 0 ) {
// scoped packages
id += `/${parts.shift()}`;
} else if ( id[0] === '.' ) {
Expand Down Expand Up @@ -261,12 +266,12 @@ export default function nodeResolve ( options = {} ) {
if ( resolved && packageBrowserField ) {
if ( packageBrowserField.hasOwnProperty(resolved) ) {
if (!packageBrowserField[resolved]) {
browserMapCache[resolved] = packageBrowserField;
browserMapCache.set(resolved, packageBrowserField);
return ES6_BROWSER_EMPTY;
}
resolved = packageBrowserField[ resolved ];
}
browserMapCache[resolved] = packageBrowserField;
browserMapCache.set(resolved, packageBrowserField);
}

if ( hasPackageEntry ) {
Expand Down Expand Up @@ -298,6 +303,13 @@ export default function nodeResolve ( options = {} ) {
}
})
.catch(() => null);
}
},

load ( importee ) {
if ( importee === ES6_BROWSER_EMPTY ) {
return 'export default {};';
}
return null;
},
};
}
2 changes: 1 addition & 1 deletion test/samples/browser-object-with-false/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Client from 'isomorphic-object-with-false';
import HTTPTracker from 'isomorphic-object-with-false/lib/client/http-tracker';
import ES6_BROWSER_EMPTY from '../../../src/empty';
import ES6_BROWSER_EMPTY from '!node-resolve:empty.js';
import HTTPTrackerWithSubPath from 'isomorphic-object-with-false/lib/subpath/foo';

// do some assert
Expand Down