Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(importMetaGlob): Analysis of the imports path in package.json #12480

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ function prepareOutDir(
}
}

function getPkgJson(root: string): PackageData['data'] {
export function getPkgJson(root: string): PackageData['data'] {
return JSON.parse(lookupFile(root, ['package.json']) || `{}`)
}

Expand Down
15 changes: 13 additions & 2 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
slash,
transformStableResult,
} from '../utils'
import { getPkgJson } from '../build'
import { isCSSRequest, isModuleCSSRequest } from './css'

const { isMatch, scan } = micromatch
Expand Down Expand Up @@ -292,10 +293,20 @@ export async function parseImportGlob(

const end = ast.range![1]

const pkgImportsAlias = getPkgJson(root)['imports'] ?? {}

const parseGlobs = globs.map((glob) => {
for (const key of Object.keys(pkgImportsAlias)) {
if (glob.startsWith(key)) {
return glob.replace(key, pkgImportsAlias[key])
}
}
return glob
})
const globsResolved = await Promise.all(
globs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId)),
parseGlobs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId)),
)
const isRelative = globs.every((i) => '.!'.includes(i[0]))
const isRelative = parseGlobs.every((i) => '.!'.includes(i[0]))

return {
match,
Expand Down
15 changes: 15 additions & 0 deletions playground/glob-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ <h2>Escape relative glob</h2>
<pre class="escape-relative"></pre>
<h2>Escape alias glob</h2>
<pre class="escape-alias"></pre>
<h2>Package imports</h2>
<pre class="package-imports"></pre>

<script type="module" src="./dir/index.js"></script>
<script type="module">
Expand Down Expand Up @@ -141,6 +143,19 @@ <h2>Escape alias glob</h2>
document.querySelector('.escape-alias').textContent = alias.sort().join('\n')
</script>

<script type="module">
const index = import.meta.glob('#dir/')
console.log(index)
const m = import.meta.glob('#dir/*')
console.log(m)
const ms = import.meta.glob('#dir/**')
console.log(ms)
document.querySelector('.package-imports').textContent = Object.entries(ms)
.map(([glob]) => glob)
.sort()
.join('\n')
</script>

<script type="module">
console.log('Ran scripts')
</script>
3 changes: 3 additions & 0 deletions playground/glob-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"imports": {
"#dir/": "./dir/"
}
}