Skip to content

Commit ff4c928

Browse files
authored
fix(commonjs): crawl dynamicRequireRoot outside cwd (#1859)
* test(commonjs): add failing test for dynamicRequireRoot outside cwd * fix(commonjs): dynamicRequireRoot outside cwd
1 parent bb54901 commit ff4c928

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

packages/commonjs/src/dynamic-modules.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync, readFileSync, statSync } from 'fs';
2-
import { join, resolve, dirname } from 'path';
2+
import { join, resolve, dirname, relative } from 'path';
33

44
import getCommonDir from 'commondir';
55

@@ -46,7 +46,7 @@ export function getDynamicRequireModules(patterns, dynamicRequireRoot) {
4646
.withBasePath()
4747
.withDirs()
4848
.glob(isNegated ? pattern.substr(1) : pattern)
49-
.crawl()
49+
.crawl(relative('.', dynamicRequireRoot))
5050
.sync()
5151
.sort((a, b) => a.localeCompare(b, 'en'))) {
5252
const resolvedPath = resolve(path);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function takeModule(path) {
2+
// eslint-disable-next-line global-require,import/no-dynamic-require
3+
return require(path);
4+
}
5+
6+
takeModule('../outer.js');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'outer_export_value';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const path = require('path');
2+
3+
const test = require('ava');
4+
const { rollup } = require('rollup');
5+
6+
const { commonjs } = require('./helpers/util.js');
7+
8+
process.chdir(path.join(__dirname, 'fixtures/samples/dynamic-require-root-outside-cwd/cwd'));
9+
10+
test('crawls dynamicRequireRoot outside cwd', async (t) => {
11+
const build = await rollup({
12+
input: 'main.js',
13+
plugins: [
14+
commonjs({
15+
dynamicRequireRoot: '..',
16+
dynamicRequireTargets: ['../outer.js']
17+
})
18+
]
19+
});
20+
const bundle = await build.generate({ format: 'cjs' });
21+
const { code } = bundle.output[0];
22+
t.true(code.includes('outer_export_value'), 'outer_export_value not found in the code');
23+
});

0 commit comments

Comments
 (0)