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

Commit

Permalink
fix(): make empty.js virtual (#224)
Browse files Browse the repository at this point in the history
* fix(): make empty.js virtual

* Using \0 for virtual module
  • Loading branch information
manucorporat authored and lukastaegert committed Jun 16, 2019
1 parent 75bebd4 commit 1719511
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
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 = '\0node-resolve:empty.js';
// 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 '\0node-resolve:empty.js';
import HTTPTrackerWithSubPath from 'isomorphic-object-with-false/lib/subpath/foo';

// do some assert
Expand Down

0 comments on commit 1719511

Please sign in to comment.