From af879777365132655f3e00c878641ac30679ccf5 Mon Sep 17 00:00:00 2001 From: Lars den Bakker Date: Sat, 21 Dec 2019 22:43:59 +0100 Subject: [PATCH 1/5] feat(node-resolve): add rootDir option --- packages/node-resolve/README.md | 11 ++++++++ packages/node-resolve/src/index.js | 5 ++-- .../node_modules/react/index.js | 1 + .../packages/package-a/index.js | 5 ++++ .../package-a/node_modules/react/index.js | 1 + .../packages/package-b/index.js | 3 +++ .../package-b/node_modules/react/index.js | 1 + packages/node-resolve/test/root-dir.js | 25 ++++++++++++++++++ .../test/snapshots/root-dir.js.md | 11 ++++++++ .../test/snapshots/root-dir.js.snap | Bin 0 -> 143 bytes packages/node-resolve/types/index.d.ts | 8 ++++++ 11 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 packages/node-resolve/test/fixtures/monorepo-dedupe/node_modules/react/index.js create mode 100644 packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/index.js create mode 100644 packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/node_modules/react/index.js create mode 100644 packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/index.js create mode 100644 packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/node_modules/react/index.js create mode 100644 packages/node-resolve/test/root-dir.js create mode 100644 packages/node-resolve/test/snapshots/root-dir.js.md create mode 100644 packages/node-resolve/test/snapshots/root-dir.js.snap diff --git a/packages/node-resolve/README.md b/packages/node-resolve/README.md index 99cc3ed74..2df2fb293 100755 --- a/packages/node-resolve/README.md +++ b/packages/node-resolve/README.md @@ -141,6 +141,17 @@ customResolveOptions: { } ``` +### `rootDir` + +Type: `String`
+Default: `process.cwd()` + +Root directory to resolve modules from. Used when resolving entrypoint imports, and when resolving deduplicated modules. Useful when executing rollup in a package of a monorepository. + +``` +rootDir: path.join(process.cwd(), '..', '..') +``` + ## Using with @rollup/plugin-commonjs Since most packages in your node_modules folder are probably legacy CommonJS rather than JavaScript modules, you may need to use [@rollup/plugin-commonjs](https://github.com/rollup/plugins/packages/commonjs): diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index f8928c04b..d47e20dee 100755 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -143,6 +143,7 @@ export default function nodeResolve(options = {}) { const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false; const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true; const customResolveOptions = options.customResolveOptions || {}; + const rootDir = options.rootDir || process.cwd(); const { jail } = options; const only = Array.isArray(options.only) ? options.only.map((o) => @@ -295,10 +296,10 @@ export default function nodeResolve(options = {}) { // ignore IDs with null character, these belong to other plugins if (/\0/.test(importee)) return null; - const basedir = importer ? dirname(importer) : process.cwd(); + const basedir = importer ? dirname(importer) : rootDir; if (shouldDedupe(importee)) { - importee = join(process.cwd(), 'node_modules', importee); + importee = join(rootDir, 'node_modules', importee); } // https://github.com/defunctzombie/package-browser-field-spec diff --git a/packages/node-resolve/test/fixtures/monorepo-dedupe/node_modules/react/index.js b/packages/node-resolve/test/fixtures/monorepo-dedupe/node_modules/react/index.js new file mode 100644 index 000000000..11bfed165 --- /dev/null +++ b/packages/node-resolve/test/fixtures/monorepo-dedupe/node_modules/react/index.js @@ -0,0 +1 @@ +export default 'react imported from root'; diff --git a/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/index.js b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/index.js new file mode 100644 index 000000000..3d4346020 --- /dev/null +++ b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/index.js @@ -0,0 +1,5 @@ +import React from 'react'; + +import packageB from '../package-b/index.js'; + +export default `Package A React: ${React} | ${packageB}`; diff --git a/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/node_modules/react/index.js b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/node_modules/react/index.js new file mode 100644 index 000000000..f2cbfe9d2 --- /dev/null +++ b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-a/node_modules/react/index.js @@ -0,0 +1 @@ +export default 'react imported from package a'; diff --git a/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/index.js b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/index.js new file mode 100644 index 000000000..0115a9bac --- /dev/null +++ b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/index.js @@ -0,0 +1,3 @@ +import React from 'react'; + +export default `package B react: ${React}`; diff --git a/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/node_modules/react/index.js b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/node_modules/react/index.js new file mode 100644 index 000000000..9b109ecf5 --- /dev/null +++ b/packages/node-resolve/test/fixtures/monorepo-dedupe/packages/package-b/node_modules/react/index.js @@ -0,0 +1 @@ +export default 'react imported from package b'; diff --git a/packages/node-resolve/test/root-dir.js b/packages/node-resolve/test/root-dir.js new file mode 100644 index 000000000..aa4990a96 --- /dev/null +++ b/packages/node-resolve/test/root-dir.js @@ -0,0 +1,25 @@ +const { join } = require('path'); + +const test = require('ava'); +const { rollup } = require('rollup'); + +const { testBundle } = require('../../../util/test'); + +const nodeResolve = require('..'); + +process.chdir(join(__dirname, 'fixtures', 'monorepo-dedupe', 'packages', 'package-a')); + +test.only('deduplicates modules from the given root directory', async (t) => { + const bundle = await rollup({ + input: 'index.js', + plugins: [ + nodeResolve({ + dedupe: ['react'], + rootDir: join(__dirname, 'fixtures', 'monorepo-dedupe') + }) + ] + }); + const { module } = await testBundle(t, bundle); + + t.snapshot(module.exports); +}); diff --git a/packages/node-resolve/test/snapshots/root-dir.js.md b/packages/node-resolve/test/snapshots/root-dir.js.md new file mode 100644 index 000000000..079fe8381 --- /dev/null +++ b/packages/node-resolve/test/snapshots/root-dir.js.md @@ -0,0 +1,11 @@ +# Snapshot report for `test/root-dir.js` + +The actual snapshot is saved in `root-dir.js.snap`. + +Generated by [AVA](https://ava.li). + +## deduplicated from the given root directory + +> Snapshot 1 + + 'Package A React: react imported from root | package B react: react imported from root' diff --git a/packages/node-resolve/test/snapshots/root-dir.js.snap b/packages/node-resolve/test/snapshots/root-dir.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..62717dd2fcb54d79e0cf6dcdf9b8b54e118ab73f GIT binary patch literal 143 zcmZ<^b5sbxYX=w@Un Date: Sat, 21 Dec 2019 17:19:49 -0500 Subject: [PATCH 2/5] chore: update .eslintignore for new fixtures --- packages/node-resolve/.eslintignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node-resolve/.eslintignore b/packages/node-resolve/.eslintignore index 97ee4d3c2..360732235 100644 --- a/packages/node-resolve/.eslintignore +++ b/packages/node-resolve/.eslintignore @@ -1 +1 @@ -test/fixtures/node_modules +test/fixtures/**/node_modules From 8c1e2ab7835eefe50b469ce74dcdfcbdaf4996cf Mon Sep 17 00:00:00 2001 From: shellscape Date: Sat, 21 Dec 2019 17:27:12 -0500 Subject: [PATCH 3/5] test: add missing snapshots --- .../node-resolve/test/snapshots/root-dir.js.md | 6 ++++++ .../test/snapshots/root-dir.js.snap | Bin 143 -> 169 bytes 2 files changed, 6 insertions(+) diff --git a/packages/node-resolve/test/snapshots/root-dir.js.md b/packages/node-resolve/test/snapshots/root-dir.js.md index 079fe8381..004f7d9b6 100644 --- a/packages/node-resolve/test/snapshots/root-dir.js.md +++ b/packages/node-resolve/test/snapshots/root-dir.js.md @@ -9,3 +9,9 @@ Generated by [AVA](https://ava.li). > Snapshot 1 'Package A React: react imported from root | package B react: react imported from root' + +## deduplicates modules from the given root directory + +> Snapshot 1 + + 'Package A React: react imported from root | package B react: react imported from root' diff --git a/packages/node-resolve/test/snapshots/root-dir.js.snap b/packages/node-resolve/test/snapshots/root-dir.js.snap index 62717dd2fcb54d79e0cf6dcdf9b8b54e118ab73f..0dfe37a480a51a7cc3d7e115ef5d612834666e0b 100644 GIT binary patch literal 169 zcmV;a09OA&RzVxYX=w@Un Date: Sun, 22 Dec 2019 14:07:43 +0100 Subject: [PATCH 4/5] Update packages/node-resolve/README.md Co-Authored-By: Tiger Oakes --- packages/node-resolve/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/node-resolve/README.md b/packages/node-resolve/README.md index 2df2fb293..9f1ee069d 100755 --- a/packages/node-resolve/README.md +++ b/packages/node-resolve/README.md @@ -149,7 +149,8 @@ Default: `process.cwd()` Root directory to resolve modules from. Used when resolving entrypoint imports, and when resolving deduplicated modules. Useful when executing rollup in a package of a monorepository. ``` -rootDir: path.join(process.cwd(), '..', '..') +// Set the root directory to be the parent folder +rootDir: path.join(process.cwd(), '..') ``` ## Using with @rollup/plugin-commonjs From cc044da25209cc1299815e1267dfe5c286aaff84 Mon Sep 17 00:00:00 2001 From: Lars den Bakker Date: Sun, 22 Dec 2019 14:08:55 +0100 Subject: [PATCH 5/5] test(node-resolve): remove test.only --- packages/node-resolve/test/root-dir.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node-resolve/test/root-dir.js b/packages/node-resolve/test/root-dir.js index aa4990a96..837695c50 100644 --- a/packages/node-resolve/test/root-dir.js +++ b/packages/node-resolve/test/root-dir.js @@ -9,7 +9,7 @@ const nodeResolve = require('..'); process.chdir(join(__dirname, 'fixtures', 'monorepo-dedupe', 'packages', 'package-a')); -test.only('deduplicates modules from the given root directory', async (t) => { +test('deduplicates modules from the given root directory', async (t) => { const bundle = await rollup({ input: 'index.js', plugins: [