Skip to content

Commit

Permalink
Reject moduleDirectories containing a slash
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhyndman committed Apr 30, 2022
1 parent 2a371c1 commit 2393801
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/node-resolve/src/index.js
Expand Up @@ -63,6 +63,12 @@ export function nodeResolve(opts = {}) {
options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
}

if (moduleDirectories.some((name) => name.includes('/'))) {
throw new Error(
'`moduleDirectories` option must only contain directory names. If you want to load modules from somewhere not supported by the default module resolution algorithm, see `modulePaths`.'
);
}

const resolveOnly = options.resolveOnly.map((pattern) => {
if (pattern instanceof RegExp) {
return pattern;
Expand Down
22 changes: 9 additions & 13 deletions packages/node-resolve/test/test.js
Expand Up @@ -257,20 +257,16 @@ test('allows custom moduleDirectories with legacy customResolveOptions.moduleDir
t.snapshot(warnings);
});

test('custom moduleDirectories do not support nested dependencies', async (t) => {
const warnings = [];
const bundle = await rollup({
input: 'custom-module-path/main.js',
onwarn: (warning) => warnings.push(warning),
plugins: [
test('moduleDirectories option rejects paths that contain a slash', async (t) => {
t.throws(
() =>
nodeResolve({
moduleDirectories: [join(process.cwd(), 'custom-module-path/node_modules')]
})
]
});

t.is(warnings.length, 1);
t.is(bundle.cache.modules.length, 2);
moduleDirectories: ['some/path']
}),
{
message: /must only contain directory names/
}
);
});

test('allows custom modulePaths', async (t) => {
Expand Down

0 comments on commit 2393801

Please sign in to comment.