Skip to content

Commit

Permalink
Handle builins appropriately for resolve 1.11.0. Fixes rollup#394.
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Jun 26, 2019
1 parent 3bf824b commit 5c80124
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 16 deletions.
57 changes: 46 additions & 11 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -47,7 +47,7 @@
"rollup": "^1.12.0",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.0.0",
"rollup-plugin-node-resolve": "^5.1.0",
"shx": "^0.3.2",
"source-map": "^0.6.1",
"source-map-support": "^0.5.12",
Expand Down
13 changes: 11 additions & 2 deletions src/index.js
@@ -1,5 +1,5 @@
import { extname, resolve } from 'path';
import { sync as nodeResolveSync } from 'resolve';
import { sync as nodeResolveSync, isCore } from 'resolve';
import { createFilter } from 'rollup-pluginutils';
import { peerDependencies } from '../package.json';
import { EXTERNAL_SUFFIX, getIdFromExternalProxyId, getIdFromProxyId, HELPERS, HELPERS_ID, PROXY_SUFFIX } from './helpers';
Expand All @@ -16,10 +16,19 @@ export default function commonjs(options = {}) {
const customNamedExports = {};
if (options.namedExports) {
Object.keys(options.namedExports).forEach(id => {
let resolveId = id;
let resolvedId;

if (isCore(id)) {
// resolve will not find npm modules with the same name as
// core modules without a trailing slash. Since core modules
// must be external, we can assume any core modules defined
// here are npm modules by that name.
resolveId += '/';
}

try {
resolvedId = nodeResolveSync(id, { basedir: process.cwd() });
resolvedId = nodeResolveSync(resolveId, { basedir: process.cwd() });
} catch (err) {
resolvedId = resolve(id);
}
Expand Down
3 changes: 3 additions & 0 deletions test/node_modules/events/index.js

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

4 changes: 4 additions & 0 deletions test/samples/custom-named-exports-browser-shims/main.js
@@ -0,0 +1,4 @@
import { message, foo } from 'events';

assert.equal( message, 'this is not builtin' );
assert.equal( foo, 'this is a hidden export' );
4 changes: 2 additions & 2 deletions test/samples/custom-named-exports-warn-builtins/main.js
@@ -1,3 +1,3 @@
import events from 'events';
import { message } from 'events';

assert.equal( events, 'x' );
assert.equal( message, 'this is not builtin' );
18 changes: 18 additions & 0 deletions test/test.js
Expand Up @@ -342,6 +342,24 @@ describe('rollup-plugin-commonjs', () => {
});
});

it('handles named exports for built-in shims', async () => {
const bundle = await rollup({
input: 'samples/custom-named-exports-browser-shims/main.js',
plugins: [
resolve({
preferBuiltins: false
}),
commonjs({
namedExports: {
events: ['foo']
}
})
]
});

await executeBundle(bundle);
});

it('ignores false positives with namedExports (#36)', async () => {
const bundle = await rollup({
input: 'samples/custom-named-exports-false-positive/main.js',
Expand Down

0 comments on commit 5c80124

Please sign in to comment.