From 2f4cba5924bf224ce5bbdb863e3a44391d29fb80 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Fri, 2 Aug 2019 12:15:51 -0700 Subject: [PATCH 1/5] Support preserveSymlinks: false (fixes #400) --- src/index.js | 6 ++++ test/samples/symlinked-node-modules/index.js | 1 + .../node_modules/events | 1 + test/test.js | 28 +++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 test/samples/symlinked-node-modules/index.js create mode 120000 test/samples/symlinked-node-modules/node_modules/events diff --git a/src/index.js b/src/index.js index 8edd87a..966cd77 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import { realpathSync } from 'fs'; import { extname, resolve } from 'path'; import { sync as nodeResolveSync, isCore } from 'resolve'; import { createFilter } from 'rollup-pluginutils'; @@ -41,6 +42,11 @@ export default function commonjs(options = {}) { } customNamedExports[resolvedId] = options.namedExports[id]; + + 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..7683928 100644 --- a/test/test.js +++ b/test/test.js @@ -342,6 +342,34 @@ describe('rollup-plugin-commonjs', () => { }); }); + it.only('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) { + // The interop should not trigger a "default is not exported" warning + throw new Error(`Unexpected warning: ${warning.message}`); + }, + plugins: [ + resolve({ + preserveSymlinks: false, + preferBuiltins: false + }), + commonjs({ + namedExports: { + events: ['foo'] + } + }) + ] + }).finally(() => process.chdir(cwd)); + }); + it('handles named exports for built-in shims', async () => { const bundle = await rollup({ input: 'samples/custom-named-exports-browser-shims/main.js', From 441fb3253c53e7e8cc6662f3855878db721f7ab9 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Fri, 2 Aug 2019 12:25:56 -0700 Subject: [PATCH 2/5] remove only --- test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 7683928..ee60f4f 100644 --- a/test/test.js +++ b/test/test.js @@ -342,7 +342,7 @@ describe('rollup-plugin-commonjs', () => { }); }); - it.only('handles symlinked node_modules with preserveSymlinks: false', () => { + it('handles symlinked node_modules with preserveSymlinks: false', () => { const cwd = process.cwd(); // ensure we resolve starting from a directory with From 9e21fbb239d58e18bee62bfca132ee78cbb79281 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Fri, 2 Aug 2019 12:34:45 -0700 Subject: [PATCH 3/5] Promise.prototype.finally is too new --- test/test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index ee60f4f..fe631ed 100644 --- a/test/test.js +++ b/test/test.js @@ -367,7 +367,15 @@ describe('rollup-plugin-commonjs', () => { } }) ] - }).finally(() => process.chdir(cwd)); + }) + .then(v => { + process.chdir(cwd); + return v; + }) + .catch(err => { + process.chdir(cwd); + throw err; + }); }); it('handles named exports for built-in shims', async () => { From 38b5a4819221025dde23e1b94b760be3f991b619 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Fri, 2 Aug 2019 12:57:15 -0700 Subject: [PATCH 4/5] Only realpath existing paths --- src/index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 966cd77..8da0742 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import { realpathSync } from 'fs'; +import { realpathSync, existsSync } from 'fs'; import { extname, resolve } from 'path'; import { sync as nodeResolveSync, isCore } from 'resolve'; import { createFilter } from 'rollup-pluginutils'; @@ -40,13 +40,15 @@ export default function commonjs(options = {}) { } catch (err) { resolvedId = resolve(id); } - customNamedExports[resolvedId] = options.namedExports[id]; - const realpath = realpathSync(resolvedId); - if (realpath !== resolvedId) { - customNamedExports[realpath] = options.namedExports[id]; + if (existsSync(resolvedId)) { + const realpath = realpathSync(resolvedId); + if (realpath !== resolvedId) { + customNamedExports[realpath] = options.namedExports[id]; + } } + }); } From 8d62d3e8944be01ee04d8d386a1ac9b20351265f Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Fri, 2 Aug 2019 13:04:26 -0700 Subject: [PATCH 5/5] Update appveyor to ensure symlinks are enabled --- appveyor.yml | 1 + test/test.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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/test/test.js b/test/test.js index fe631ed..8927c6f 100644 --- a/test/test.js +++ b/test/test.js @@ -353,7 +353,7 @@ describe('rollup-plugin-commonjs', () => { return rollup({ input: './index.js', onwarn(warning) { - // The interop should not trigger a "default is not exported" warning + // should not get a warning about unknown export 'foo' throw new Error(`Unexpected warning: ${warning.message}`); }, plugins: [