Skip to content

Commit 669d7e0

Browse files
authored
feat: allow globs in node_modules when pattern is explicit (#6056)
1 parent 6408a3a commit 669d7e0

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
22
node_modules
3+
!**/glob-import/dir/node_modules
34
dist
45
dist-ssr
56
TODOs.md

packages/playground/glob-import/__tests__/glob-import.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ const allResult = {
4545
}
4646
}
4747

48+
const nodeModulesResult = {
49+
'/dir/node_modules/hoge.js': { msg: 'hoge' }
50+
}
51+
4852
const rawResult = {
4953
'/dir/baz.json': {
5054
msg: 'baz'
@@ -55,6 +59,9 @@ test('should work', async () => {
5559
expect(await page.textContent('.result')).toBe(
5660
JSON.stringify(allResult, null, 2)
5761
)
62+
expect(await page.textContent('.result-node_modules')).toBe(
63+
JSON.stringify(nodeModulesResult, null, 2)
64+
)
5865
})
5966

6067
test('import glob raw', async () => {

packages/playground/glob-import/dir/node_modules/hoge.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/playground/glob-import/index.html

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
<pre class="result"></pre>
2+
<pre class="result-node_modules"></pre>
23
<pre class="globraw"></pre>
34

45
<script type="module" src="./dir/index.js"></script>
56
<script type="module">
7+
function useImports(modules, selector) {
8+
for (const path in modules) {
9+
modules[path]().then((mod) => {
10+
console.log(path, mod)
11+
})
12+
}
13+
14+
const keys = Object.keys(modules)
15+
Promise.all(keys.map((key) => modules[key]())).then((mods) => {
16+
const res = {}
17+
mods.forEach((m, i) => {
18+
res[keys[i]] = m
19+
})
20+
document.querySelector(selector).textContent = JSON.stringify(
21+
res,
22+
null,
23+
2
24+
)
25+
})
26+
}
27+
628
const modules = import.meta.glob(
729
'/dir/**'
830
// for test: annotation contain ")"
931
/*
1032
* for test: annotation contain ")"
1133
* */
1234
)
35+
useImports(modules, '.result')
1336

14-
for (const path in modules) {
15-
modules[path]().then((mod) => {
16-
console.log(path, mod)
17-
})
18-
}
19-
20-
const keys = Object.keys(modules)
21-
Promise.all(keys.map((key) => modules[key]())).then((mods) => {
22-
const res = {}
23-
mods.forEach((m, i) => {
24-
res[keys[i]] = m
25-
})
26-
document.querySelector('.result').textContent = JSON.stringify(res, null, 2)
27-
})
37+
const nodeModules = import.meta.glob('/dir/node_modules/**')
38+
useImports(nodeModules, '.result-node_modules')
2839
</script>
2940

3041
<script type="module">

packages/vite/src/node/importGlob.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export async function transformImportGlob(
6969
}
7070
const files = glob.sync(pattern, {
7171
cwd: base,
72-
ignore: ['**/node_modules/**']
72+
// Ignore node_modules by default unless explicitly indicated in the pattern
73+
ignore: /(^|\/)node_modules\//.test(pattern) ? [] : ['**/node_modules/**']
7374
})
7475
const imports: string[] = []
7576
let importsString = ``

0 commit comments

Comments
 (0)