Skip to content

Commit

Permalink
fix(node-resolve): resolve local files if resolveOption is set (#337)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
JaviIG and javieri-empathy authored May 1, 2020
1 parent a44e5b3 commit 96e0900
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,23 @@ export const nodeResolve = (opts = {}) => {

const parts = importee.split(/[/\\]/);
let id = parts.shift();
let isRelativeImport = false;

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

const input = normalizeInput(rollupOptions.input);

if (resolveOnly.length && !resolveOnly.some((pattern) => pattern.test(id))) {
if (
!isRelativeImport &&
resolveOnly.length &&
!resolveOnly.some((pattern) => pattern.test(id))
) {
if (input.includes(id)) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions packages/node-resolve/test/fixtures/only-local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Resolved local var';
3 changes: 3 additions & 0 deletions packages/node-resolve/test/fixtures/only.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import foo from '@scoped/foo';
import bar from '@scoped/bar';
import test from 'test';

import local from './only-local';

console.log(foo);
console.log(bar);
console.log(test);
console.log(local);
12 changes: 10 additions & 2 deletions packages/node-resolve/test/only.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { join } = require('path');
const { join, resolve } = require('path');

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

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

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

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

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

test('regex', async (t) => {
Expand All @@ -39,10 +41,12 @@ test('regex', async (t) => {
]
});
const imports = await getImports(bundle);
const modules = await getResolvedModules(bundle);

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

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

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

test('deprecated: regex', async (t) => {
Expand All @@ -75,8 +81,10 @@ test('deprecated: regex', async (t) => {
]
});
const imports = await getImports(bundle);
const modules = await getResolvedModules(bundle);

t.is(warnings.length, 1);
t.snapshot(warnings);
t.deepEqual(imports, ['test']);
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
});
2 changes: 2 additions & 0 deletions util/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const getCode: GetCode;

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

export function getResolvedModules(bundle: RollupBuild): Promise<Record<string, string>>;

export function testBundle(
t: Assertions,
bundle: RollupBuild,
Expand Down
8 changes: 8 additions & 0 deletions util/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const getImports = async (bundle) => {
return imports;
};

const getResolvedModules = async (bundle) => {
const {
output: [{ modules }]
} = await bundle.generate({ format: 'esm' });
return modules;
};

/**
* @param {import('ava').Assertions} t
* @param {import('rollup').RollupBuild} bundle
Expand Down Expand Up @@ -55,5 +62,6 @@ const testBundle = async (t, bundle, args = {}) => {
module.exports = {
getCode,
getImports,
getResolvedModules,
testBundle
};

0 comments on commit 96e0900

Please sign in to comment.