Skip to content

Commit

Permalink
chore(commonjs): switch to fdir for fewer dependencies (#1741)
Browse files Browse the repository at this point in the history
BREAKING CHANGES: directories listed in dynamicRequireTargets now require a trailing slash

Co-authored-by: shellscape <andrew@shellscape.org>
  • Loading branch information
benmccann and shellscape authored Sep 23, 2024
1 parent 05af988 commit c2a05eb
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 131 deletions.
5 changes: 3 additions & 2 deletions packages/commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@
"@rollup/pluginutils": "^5.0.1",
"commondir": "^1.0.1",
"estree-walker": "^2.0.2",
"glob": "^10.4.1",
"fdir": "^6.1.1",
"is-reference": "1.2.1",
"magic-string": "^0.30.3"
"magic-string": "^0.30.3",
"picomatch": "^2.3.1"
},
"devDependencies": {
"@rollup/plugin-json": "^5.0.0",
Expand Down
11 changes: 8 additions & 3 deletions packages/commonjs/src/dynamic-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join, resolve, dirname } from 'path';

import getCommonDir from 'commondir';

import { glob } from 'glob';
import { fdir } from 'fdir';

import { getVirtualPathForDynamicRequirePath, normalizePathSlashes } from './utils';

Expand Down Expand Up @@ -41,8 +41,13 @@ export function getDynamicRequireModules(patterns, dynamicRequireRoot) {
isNegated
? dynamicRequireModules.delete(targetPath)
: dynamicRequireModules.set(targetPath, resolvedPath);
for (const path of glob
.sync(isNegated ? pattern.substr(1) : pattern)
// eslint-disable-next-line new-cap
for (const path of new fdir()
.withBasePath()
.withDirs()
.glob(isNegated ? pattern.substr(1) : pattern)
.crawl()
.sync()
.sort((a, b) => a.localeCompare(b, 'en'))) {
const resolvedPath = resolve(path);
const requirePath = normalizePathSlashes(resolvedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = {
},
pluginOptions: {
dynamicRequireTargets: [
'fixtures/function/dynamic-require-instances/direct',
'fixtures/function/dynamic-require-instances/direct/',
'fixtures/function/dynamic-require-instances/direct/index.js',
'fixtures/function/dynamic-require-instances/package',
'fixtures/function/dynamic-require-instances/package/',
'fixtures/function/dynamic-require-instances/package/main.js'
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module.exports = {
},
pluginOptions: {
dynamicRequireTargets: [
'fixtures/function/dynamic-require-package',
'fixtures/function/dynamic-require-package/sub',
'fixtures/function/dynamic-require-package/node_modules/custom-module'
'fixtures/function/dynamic-require-package/',
'fixtures/function/dynamic-require-package/sub/',
'fixtures/function/dynamic-require-package/node_modules/custom-module/'
]
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = {
},
pluginOptions: {
dynamicRequireTargets: [
'fixtures/function/dynamic-require-resolve-index',
'fixtures/function/dynamic-require-resolve-index/sub',
'fixtures/function/dynamic-require-resolve-index/node_modules/custom-module'
'fixtures/function/dynamic-require-resolve-index/',
'fixtures/function/dynamic-require-resolve-index/sub/',
'fixtures/function/dynamic-require-resolve-index/node_modules/custom-module/'
]
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
},
pluginOptions: {
dynamicRequireTargets: [
'fixtures/function/dynamic-require-resolve-reference/node_modules/{custom-module,custom-module2}{,/*.js}'
'fixtures/function/dynamic-require-resolve-reference/node_modules/{custom-module,custom-module2}{/,/*.js}'
]
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = {
},
pluginOptions: {
dynamicRequireTargets: [
'fixtures/function/dynamic-require-slash-access',
'fixtures/function/dynamic-require-slash-access/sub',
'fixtures/function/dynamic-require-slash-access/node_modules/custom-module',
'fixtures/function/dynamic-require-slash-access/',
'fixtures/function/dynamic-require-slash-access/sub/',
'fixtures/function/dynamic-require-slash-access/node_modules/custom-module/',
'fixtures/function/dynamic-require-slash-access/node_modules/custom-module2/*.js'
]
}
Expand Down
Loading

0 comments on commit c2a05eb

Please sign in to comment.