Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@ const allResult = {
}
}

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

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(resultNodeModules, null, 2)
)
})

if (!isBuild) {
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.

35 changes: 23 additions & 12 deletions packages/playground/glob-import/index.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
<pre class="result"></pre>
<pre class="result-node_modules"></pre>

<script type="module" src="./dir/index.js"></script>
<script type="module">
const modules = import.meta.glob('/dir/**')
function useImports(modules, selector) {
for (const path in modules) {
modules[path]().then((mod) => {
console.log(path, mod)
})
}

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 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 modules = import.meta.glob('/dir/**')
useImports(modules, '.result')

const nodeModules = import.meta.glob('/dir/node_modules/**')
useImports(nodeModules, '.result-node_modules')
</script>
3 changes: 2 additions & 1 deletion packages/vite/src/node/importGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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