diff --git a/appveyor.yml b/appveyor.yml index 0e41b72..5227ded 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,6 +6,7 @@ clone_depth: 10 init: - git config --global core.autocrlf false + - git config --global core.symlinks true environment: matrix: diff --git a/src/index.js b/src/index.js index 8edd87a..8da0742 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import { realpathSync, existsSync } from 'fs'; import { extname, resolve } from 'path'; import { sync as nodeResolveSync, isCore } from 'resolve'; import { createFilter } from 'rollup-pluginutils'; @@ -39,8 +40,15 @@ export default function commonjs(options = {}) { } catch (err) { resolvedId = resolve(id); } - customNamedExports[resolvedId] = options.namedExports[id]; + + if (existsSync(resolvedId)) { + const realpath = realpathSync(resolvedId); + if (realpath !== resolvedId) { + customNamedExports[realpath] = options.namedExports[id]; + } + } + }); } diff --git a/test/samples/symlinked-node-modules/index.js b/test/samples/symlinked-node-modules/index.js new file mode 100644 index 0000000..d7b8a82 --- /dev/null +++ b/test/samples/symlinked-node-modules/index.js @@ -0,0 +1 @@ +import { foo } from 'events'; \ No newline at end of file diff --git a/test/samples/symlinked-node-modules/node_modules/events b/test/samples/symlinked-node-modules/node_modules/events new file mode 120000 index 0000000..cf24a5a --- /dev/null +++ b/test/samples/symlinked-node-modules/node_modules/events @@ -0,0 +1 @@ +../../../node_modules/events \ No newline at end of file diff --git a/test/test.js b/test/test.js index 0261572..8927c6f 100644 --- a/test/test.js +++ b/test/test.js @@ -342,6 +342,42 @@ describe('rollup-plugin-commonjs', () => { }); }); + it('handles symlinked node_modules with preserveSymlinks: false', () => { + const cwd = process.cwd(); + + // ensure we resolve starting from a directory with + // symlinks in node_modules. + + process.chdir('samples/symlinked-node-modules'); + + return rollup({ + input: './index.js', + onwarn(warning) { + // should not get a warning about unknown export 'foo' + throw new Error(`Unexpected warning: ${warning.message}`); + }, + plugins: [ + resolve({ + preserveSymlinks: false, + preferBuiltins: false + }), + commonjs({ + namedExports: { + events: ['foo'] + } + }) + ] + }) + .then(v => { + process.chdir(cwd); + return v; + }) + .catch(err => { + process.chdir(cwd); + throw err; + }); + }); + it('handles named exports for built-in shims', async () => { const bundle = await rollup({ input: 'samples/custom-named-exports-browser-shims/main.js',