Skip to content

Commit 6284ebe

Browse files
LarsDenBakkershellscape
authored andcommitted
fix(node-resolve): allow deduplicating custom module dirs (#101)
1 parent 463b5ed commit 6284ebe

File tree

8 files changed

+52
-6
lines changed

8 files changed

+52
-6
lines changed

packages/node-resolve/src/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-param-reassign, no-shadow, no-undefined */
2-
import { dirname, extname, join, normalize, resolve, sep } from 'path';
2+
import { dirname, extname, normalize, resolve, sep } from 'path';
33

44
import fs, { realpathSync } from 'fs';
55

@@ -296,11 +296,7 @@ export default function nodeResolve(options = {}) {
296296
// ignore IDs with null character, these belong to other plugins
297297
if (/\0/.test(importee)) return null;
298298

299-
const basedir = importer ? dirname(importer) : rootDir;
300-
301-
if (shouldDedupe(importee)) {
302-
importee = join(rootDir, 'node_modules', importee);
303-
}
299+
const basedir = !importer || shouldDedupe(importee) ? rootDir : dirname(importer);
304300

305301
// https://github.com/defunctzombie/package-browser-field-spec
306302
const browser = browserMapCache.get(importer);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { join } = require('path');
2+
3+
const test = require('ava');
4+
const { rollup } = require('rollup');
5+
6+
const { testBundle } = require('../../../util/test');
7+
8+
const nodeResolve = require('..');
9+
10+
process.chdir(join(__dirname, 'fixtures', 'custom-resolve-options'));
11+
12+
test('can deduplicate custom module directory', async (t) => {
13+
const bundle = await rollup({
14+
input: 'dedupe.js',
15+
plugins: [
16+
nodeResolve({
17+
dedupe: ['package-b'],
18+
customResolveOptions: {
19+
moduleDirectory: 'js_modules'
20+
}
21+
})
22+
]
23+
});
24+
const { module } = await testBundle(t, bundle);
25+
26+
t.snapshot(module.exports);
27+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import PackageA from 'package-a';
2+
import PackageB from 'package-b';
3+
4+
export { PackageA, PackageB };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import PackageB from 'package-b';
2+
3+
export default `package-a:${PackageB}`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'package-b:nested';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'package-b:root';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Snapshot report for `test/dedupe-custom.js`
2+
3+
The actual snapshot is saved in `dedupe-custom.js.snap`.
4+
5+
Generated by [AVA](https://ava.li).
6+
7+
## can deduplicate custom module directory
8+
9+
> Snapshot 1
10+
11+
{
12+
PackageA: 'package-a:package-b:root',
13+
PackageB: 'package-b:root',
14+
}
Binary file not shown.

0 commit comments

Comments
 (0)