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

Handle builtins appropriately for resolve 1.11.0. Fixes #394. #395

Merged
merged 1 commit into from Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 += '/';
}
Copy link
Member

Choose a reason for hiding this comment

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

👍


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