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 diff --git a/packages/node-resolve/README.md b/packages/node-resolve/README.md index 99cc3ed74..9f1ee069d 100755 --- a/packages/node-resolve/README.md +++ b/packages/node-resolve/README.md @@ -141,6 +141,18 @@ 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. + +``` +// Set the root directory to be the parent folder +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..837695c50 --- /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('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..004f7d9b6 --- /dev/null +++ b/packages/node-resolve/test/snapshots/root-dir.js.md @@ -0,0 +1,17 @@ +# 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' + +## 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 new file mode 100644 index 000000000..0dfe37a48 Binary files /dev/null and b/packages/node-resolve/test/snapshots/root-dir.js.snap differ diff --git a/packages/node-resolve/types/index.d.ts b/packages/node-resolve/types/index.d.ts index caf8aad09..7b398bc8e 100755 --- a/packages/node-resolve/types/index.d.ts +++ b/packages/node-resolve/types/index.d.ts @@ -92,6 +92,14 @@ export interface Options { * to node-resolve */ customResolveOptions?: AsyncOpts; + + /** + * 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. + * @default process.cwd() + */ + rootDir?: string; } /**