mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
feat: allow globs in node_modules when pattern is explicit (#6056)
This commit is contained in:
parent
6408a3ab9b
commit
669d7e0f4b
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
.DS_Store
|
||||
node_modules
|
||||
!**/glob-import/dir/node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
TODOs.md
|
||||
|
@ -45,6 +45,10 @@ const allResult = {
|
||||
}
|
||||
}
|
||||
|
||||
const nodeModulesResult = {
|
||||
'/dir/node_modules/hoge.js': { msg: 'hoge' }
|
||||
}
|
||||
|
||||
const rawResult = {
|
||||
'/dir/baz.json': {
|
||||
msg: 'baz'
|
||||
@ -55,6 +59,9 @@ test('should work', async () => {
|
||||
expect(await page.textContent('.result')).toBe(
|
||||
JSON.stringify(allResult, null, 2)
|
||||
)
|
||||
expect(await page.textContent('.result-node_modules')).toBe(
|
||||
JSON.stringify(nodeModulesResult, null, 2)
|
||||
)
|
||||
})
|
||||
|
||||
test('import glob raw', async () => {
|
||||
|
1
packages/playground/glob-import/dir/node_modules/hoge.js
generated
vendored
Normal file
1
packages/playground/glob-import/dir/node_modules/hoge.js
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
export const msg = 'hoge'
|
@ -1,8 +1,30 @@
|
||||
<pre class="result"></pre>
|
||||
<pre class="result-node_modules"></pre>
|
||||
<pre class="globraw"></pre>
|
||||
|
||||
<script type="module" src="./dir/index.js"></script>
|
||||
<script type="module">
|
||||
function useImports(modules, selector) {
|
||||
for (const path in modules) {
|
||||
modules[path]().then((mod) => {
|
||||
console.log(path, mod)
|
||||
})
|
||||
}
|
||||
|
||||
const keys = Object.keys(modules)
|
||||
Promise.all(keys.map((key) => modules[key]())).then((mods) => {
|
||||
const res = {}
|
||||
mods.forEach((m, i) => {
|
||||
res[keys[i]] = m
|
||||
})
|
||||
document.querySelector(selector).textContent = JSON.stringify(
|
||||
res,
|
||||
null,
|
||||
2
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
const modules = import.meta.glob(
|
||||
'/dir/**'
|
||||
// for test: annotation contain ")"
|
||||
@ -10,21 +32,10 @@
|
||||
* for test: annotation contain ")"
|
||||
* */
|
||||
)
|
||||
useImports(modules, '.result')
|
||||
|
||||
for (const path in modules) {
|
||||
modules[path]().then((mod) => {
|
||||
console.log(path, mod)
|
||||
})
|
||||
}
|
||||
|
||||
const keys = Object.keys(modules)
|
||||
Promise.all(keys.map((key) => modules[key]())).then((mods) => {
|
||||
const res = {}
|
||||
mods.forEach((m, i) => {
|
||||
res[keys[i]] = m
|
||||
})
|
||||
document.querySelector('.result').textContent = JSON.stringify(res, null, 2)
|
||||
})
|
||||
const nodeModules = import.meta.glob('/dir/node_modules/**')
|
||||
useImports(nodeModules, '.result-node_modules')
|
||||
</script>
|
||||
|
||||
<script type="module">
|
||||
|
@ -69,7 +69,8 @@ export async function transformImportGlob(
|
||||
}
|
||||
const files = glob.sync(pattern, {
|
||||
cwd: base,
|
||||
ignore: ['**/node_modules/**']
|
||||
// Ignore node_modules by default unless explicitly indicated in the pattern
|
||||
ignore: /(^|\/)node_modules\//.test(pattern) ? [] : ['**/node_modules/**']
|
||||
})
|
||||
const imports: string[] = []
|
||||
let importsString = ``
|
||||
|
Loading…
Reference in New Issue
Block a user