Skip to content

Commit

Permalink
feat: allow globs in node_modules when pattern is explicit (vitejs#6056)
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox committed Jan 12, 2022
1 parent 6408a3a commit 669d7e0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
.DS_Store
node_modules
!**/glob-import/dir/node_modules
dist
dist-ssr
TODOs.md
Expand Down
7 changes: 7 additions & 0 deletions packages/playground/glob-import/__tests__/glob-import.spec.ts
Expand Up @@ -45,6 +45,10 @@ const allResult = {
}
}

const nodeModulesResult = {
'/dir/node_modules/hoge.js': { msg: 'hoge' }
}

const rawResult = {
'/dir/baz.json': {
msg: 'baz'
Expand All @@ -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 () => {
Expand Down
1 change: 1 addition & 0 deletions packages/playground/glob-import/dir/node_modules/hoge.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 25 additions & 14 deletions packages/playground/glob-import/index.html
@@ -1,30 +1,41 @@
<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 ")"
/*
* 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">
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/importGlob.ts
Expand Up @@ -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 = ``
Expand Down

0 comments on commit 669d7e0

Please sign in to comment.