Skip to content

Commit 96e0900

Browse files
fix(node-resolve): resolve local files if resolveOption is set (#337)
* fix(node-resolve): Resolve local files if `resolveOption` is set * format(node-resolve): Remove empty line in `fixtures/only.js` * test(node-resolve): add assertion to 'resolveOnly' tests to check resolved modules Co-authored-by: Javier Iglesias García <javieri@empathy.co>
1 parent a44e5b3 commit 96e0900

6 files changed

Lines changed: 31 additions & 4 deletions

File tree

packages/node-resolve/src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,23 @@ export const nodeResolve = (opts = {}) => {
111111

112112
const parts = importee.split(/[/\\]/);
113113
let id = parts.shift();
114+
let isRelativeImport = false;
114115

115116
if (id[0] === '@' && parts.length > 0) {
116117
// scoped packages
117118
id += `/${parts.shift()}`;
118119
} else if (id[0] === '.') {
119120
// an import relative to the parent dir of the importer
120121
id = resolve(basedir, importee);
122+
isRelativeImport = true;
121123
}
122124

123125
const input = normalizeInput(rollupOptions.input);
124-
125-
if (resolveOnly.length && !resolveOnly.some((pattern) => pattern.test(id))) {
126+
if (
127+
!isRelativeImport &&
128+
resolveOnly.length &&
129+
!resolveOnly.some((pattern) => pattern.test(id))
130+
) {
126131
if (input.includes(id)) {
127132
return null;
128133
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'Resolved local var';

packages/node-resolve/test/fixtures/only.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import foo from '@scoped/foo';
22
import bar from '@scoped/bar';
33
import test from 'test';
44

5+
import local from './only-local';
6+
57
console.log(foo);
68
console.log(bar);
79
console.log(test);
10+
console.log(local);

packages/node-resolve/test/only.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { join } = require('path');
1+
const { join, resolve } = require('path');
22

33
const test = require('ava');
44
const { rollup } = require('rollup');
55

6-
const { getImports } = require('../../../util/test');
6+
const { getImports, getResolvedModules } = require('../../../util/test');
77

88
const { nodeResolve } = require('..');
99

@@ -21,10 +21,12 @@ test('specify the only packages to resolve', async (t) => {
2121
]
2222
});
2323
const imports = await getImports(bundle);
24+
const modules = await getResolvedModules(bundle);
2425

2526
t.is(warnings.length, 0);
2627
t.snapshot(warnings);
2728
t.deepEqual(imports, ['@scoped/foo', '@scoped/bar']);
29+
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
2830
});
2931

3032
test('regex', async (t) => {
@@ -39,10 +41,12 @@ test('regex', async (t) => {
3941
]
4042
});
4143
const imports = await getImports(bundle);
44+
const modules = await getResolvedModules(bundle);
4245

4346
t.is(warnings.length, 0);
4447
t.snapshot(warnings);
4548
t.deepEqual(imports, ['test']);
49+
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
4650
});
4751

4852
test('deprecated: specify the only packages to resolve', async (t) => {
@@ -57,10 +61,12 @@ test('deprecated: specify the only packages to resolve', async (t) => {
5761
]
5862
});
5963
const imports = await getImports(bundle);
64+
const modules = await getResolvedModules(bundle);
6065

6166
t.is(warnings.length, 1);
6267
t.snapshot(warnings);
6368
t.deepEqual(imports, ['@scoped/foo', '@scoped/bar']);
69+
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
6470
});
6571

6672
test('deprecated: regex', async (t) => {
@@ -75,8 +81,10 @@ test('deprecated: regex', async (t) => {
7581
]
7682
});
7783
const imports = await getImports(bundle);
84+
const modules = await getResolvedModules(bundle);
7885

7986
t.is(warnings.length, 1);
8087
t.snapshot(warnings);
8188
t.deepEqual(imports, ['test']);
89+
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
8290
});

util/test.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export const getCode: GetCode;
1717

1818
export function getImports(bundle: RollupBuild): Promise<string[]>;
1919

20+
export function getResolvedModules(bundle: RollupBuild): Promise<Record<string, string>>;
21+
2022
export function testBundle(
2123
t: Assertions,
2224
bundle: RollupBuild,

util/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const getImports = async (bundle) => {
2323
return imports;
2424
};
2525

26+
const getResolvedModules = async (bundle) => {
27+
const {
28+
output: [{ modules }]
29+
} = await bundle.generate({ format: 'esm' });
30+
return modules;
31+
};
32+
2633
/**
2734
* @param {import('ava').Assertions} t
2835
* @param {import('rollup').RollupBuild} bundle
@@ -55,5 +62,6 @@ const testBundle = async (t, bundle, args = {}) => {
5562
module.exports = {
5663
getCode,
5764
getImports,
65+
getResolvedModules,
5866
testBundle
5967
};

0 commit comments

Comments
 (0)