feat: allow globs in node_modules when pattern is explicit (#6056)

This commit is contained in:
Fran Dios 2022-01-12 13:37:58 +01:00 committed by GitHub
parent 6408a3ab9b
commit 669d7e0f4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 15 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.DS_Store
node_modules
!**/glob-import/dir/node_modules
dist
dist-ssr
TODOs.md

View File

@ -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 () => {

View File

@ -0,0 +1 @@
export const msg = 'hoge'

View File

@ -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">

View File

@ -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 = ``