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

fix: leave fully dynamic import.meta.url asset (fixes #10306) #10549

Merged
Merged
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
12 changes: 10 additions & 2 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,23 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const ast = this.parse(rawUrl)
const templateLiteral = (ast as any).body[0].expression
if (templateLiteral.expressions.length) {
const pattern = JSON.stringify(buildGlobPattern(templateLiteral))
const pattern = buildGlobPattern(templateLiteral)
if (pattern.startsWith('**')) {
// don't transform for patterns like this
// because users won't intend to do that in most cases
continue
}

// Note: native import.meta.url is not supported in the baseline
// target so we use the global location here. It can be
// window.location or self.location in case it is used in a Web Worker.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Window/self
s.update(
index,
index + exp.length,
`new URL((import.meta.glob(${pattern}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`,
`new URL((import.meta.glob(${JSON.stringify(
pattern,
)}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`,
)
continue
}
Expand Down
19 changes: 16 additions & 3 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ <h2>new URL(`non-existent`, import.meta.url)</h2>
<code class="non-existent-import-meta-url"></code>
</p>

<h2>new URL(`${dynamic}`, import.meta.url)</h2>
<p>
<code class="dynamic-import-meta-url-all"></code>
</p>

<h2>simple script tag import-expression</h2>
<code class="import-expression"></code>
<code class="obj-import-express"></code>
Expand Down Expand Up @@ -427,9 +432,17 @@ <h3>assets in noscript</h3>
testDynamicImportMetaUrlWithComma('icon', 1)
testDynamicImportMetaUrlWithComma('asset', 2)

const name = 'test'
const js = new URL(`./nested/${name}.js`, import.meta.url).href
text('.dynamic-import-meta-url-js', js)
{
const name = 'test'
const js = new URL(`./nested/${name}.js`, import.meta.url).href
text('.dynamic-import-meta-url-js', js)
}

{
const name = './nested/icon'
const metaUrl = new URL(`${name}.png`, import.meta.url)
text(`.dynamic-import-meta-url-all`, metaUrl)
}

function text(el, text) {
document.querySelector(el).textContent = text
Expand Down