Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node-resolve): add rootDir option #98

Merged
merged 7 commits into from
Dec 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/node-resolve/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test/fixtures/node_modules
test/fixtures/**/node_modules
12 changes: 12 additions & 0 deletions packages/node-resolve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ customResolveOptions: {
}
```

### `rootDir`

Type: `String`<br>
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):
Expand Down
5 changes: 3 additions & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

import packageB from '../package-b/index.js';

export default `Package A React: ${React} | ${packageB}`;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export default `package B react: ${React}`;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions packages/node-resolve/test/root-dir.js
Original file line number Diff line number Diff line change
@@ -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);
});
17 changes: 17 additions & 0 deletions packages/node-resolve/test/snapshots/root-dir.js.md
Original file line number Diff line number Diff line change
@@ -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'
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/node-resolve/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down