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 authored and shellscape committed Sep 12, 2022
1 parent 1708f30 commit 0bc9574
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 @@ -57,6 +57,12 @@ export function nodeResolve(opts = {}) {
let { dedupe } = options;
let rollupOptions;

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`.'
);
}

if (typeof dedupe !== 'function') {
dedupe = (importee) =>
options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
Expand Down
22 changes: 9 additions & 13 deletions packages/node-resolve/test/test.js
Expand Up @@ -273,20 +273,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 0bc9574

Please sign in to comment.