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: call tryFsResolve for relative new URL(foo, import.meta.url) #13142

Merged
merged 3 commits into from May 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -14,6 +14,8 @@ import {
import { CLIENT_ENTRY } from '../constants'
import { fileToUrl } from './asset'
import { preloadHelperId } from './importAnalysisBuild'
import type { InternalResolveOptions } from './resolve'
import { tryFsResolve } from './resolve'

/**
* Convert `new URL('./foo.png', import.meta.url)` to its resolved built URL
Expand All @@ -29,6 +31,16 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const normalizedPublicDir = normalizePath(config.publicDir)
let assetResolver: ResolveFn

const fsResolveOptions: InternalResolveOptions = {
...config.resolve,
root: config.root,
isProduction: config.isProduction,
isBuild: config.command === 'build',
packageCache: config.packageCache,
ssrConfig: config.ssr,
asSrc: true,
}

return {
name: 'vite:asset-import-meta-url',
async transform(code, id, options) {
Expand Down Expand Up @@ -98,6 +110,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
let file: string | undefined
if (url[0] === '.') {
file = slash(path.resolve(path.dirname(id), url))
file = tryFsResolve(file, fsResolveOptions) ?? file
} else {
assetResolver ??= config.createResolver({
extensions: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -498,7 +498,7 @@ function splitFileAndPostfix(path: string) {
return { file, postfix: path.slice(file.length) }
}

function tryFsResolve(
export function tryFsResolve(
fsPath: string,
options: InternalResolveOptions,
tryIndex = true,
Expand Down
13 changes: 13 additions & 0 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Expand Up @@ -17,6 +17,8 @@ import type { ResolveFn } from '..'
import type { WorkerType } from './worker'
import { WORKER_FILE_ID, workerFileToUrl } from './worker'
import { fileToUrl } from './asset'
import type { InternalResolveOptions } from './resolve'
import { tryFsResolve } from './resolve'

const ignoreFlagRE = /\/\*\s*@vite-ignore\s*\*\//

Expand Down Expand Up @@ -99,6 +101,16 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
let workerResolver: ResolveFn

const fsResolveOptions: InternalResolveOptions = {
...config.resolve,
root: config.root,
isProduction: config.isProduction,
isBuild: config.command === 'build',
packageCache: config.packageCache,
ssrConfig: config.ssr,
asSrc: true,
}

return {
name: 'vite:worker-import-meta-url',

Expand Down Expand Up @@ -143,6 +155,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
let file: string | undefined
if (url[0] === '.') {
file = path.resolve(path.dirname(id), url)
file = tryFsResolve(file, fsResolveOptions) ?? file
} else {
workerResolver ??= config.createResolver({
extensions: [],
Expand Down
9 changes: 9 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Expand Up @@ -312,6 +312,15 @@ test('new URL("/...", import.meta.url)', async () => {
)
})

test('new URL(..., import.meta.url) without extension', async () => {
expect(await page.textContent('.import-meta-url-without-extension')).toMatch(
isBuild ? 'data:application/javascript' : 'nested/test.js',
)
expect(
await page.textContent('.import-meta-url-content-without-extension'),
).toContain('export default class')
})

test('new URL(`${dynamic}`, import.meta.url)', async () => {
expect(await page.textContent('.dynamic-import-meta-url-1')).toMatch(
isBuild ? 'data:image/png;base64' : '/foo/nested/icon.png',
Expand Down
15 changes: 15 additions & 0 deletions playground/assets/index.html
Expand Up @@ -190,6 +190,14 @@ <h2>new URL('/...', import.meta.url)</h2>
<img class="import-meta-url-base-path-img" />
<code class="import-meta-url-base-path"></code>

<h2>new URL('...', import.meta.url (without extension))</h2>
<p>
<code class="import-meta-url-content-without-extension"></code>
</p>
<p>
<code class="import-meta-url-without-extension"></code>
</p>

<h2>new URL('...', import.meta.url,) (with comma)</h2>
<img class="import-meta-url-img-comma" />
<code class="import-meta-url-comma"></code>
Expand Down Expand Up @@ -393,6 +401,13 @@ <h3>assets in noscript</h3>
text('.import-meta-url-base-path', metaUrlBasePath)
document.querySelector('.import-meta-url-base-path-img').src = metaUrlBasePath

const metaUrlWithoutExtension = new URL('./nested/test', import.meta.url)
text('.import-meta-url-without-extension', metaUrlWithoutExtension)
;(async () => {
const res = await fetch(metaUrlWithoutExtension)
text('.import-meta-url-content-without-extension', await res.text())
})()

// prettier-ignore
const metaUrlWithComma = new URL('./nested/asset.png', import.meta.url,)
text('.import-meta-url-comma', metaUrlWithComma)
Expand Down
5 changes: 5 additions & 0 deletions playground/worker/__tests__/es/es-worker.spec.ts
Expand Up @@ -113,6 +113,11 @@ test('module worker', async () => {
'A string',
true,
)
await untilUpdated(
() => page.textContent('.worker-import-meta-url-without-extension'),
'A string',
true,
)
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
'A string',
Expand Down
5 changes: 5 additions & 0 deletions playground/worker/__tests__/iife/iife-worker.spec.ts
Expand Up @@ -98,6 +98,11 @@ test('module worker', async () => {
/A\sstring.*\/iife\/.+url-worker\.js/,
true,
)
await untilUpdated(
() => page.textContent('.worker-import-meta-url-without-extension'),
'A string',
true,
)
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
'A string',
Expand Down
6 changes: 6 additions & 0 deletions playground/worker/index.html
Expand Up @@ -56,6 +56,12 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="worker-import-meta-url-resolve"></code>

<p>
new Worker(new URL('./url-worker', import.meta.url), { type: 'module' })
<span class="classname">.worker-import-meta-url-without-extension</span>
</p>
<code class="worker-import-meta-url-without-extension"></code>

<p>
new SharedWorker(new URL('./url-shared-worker.js', import.meta.url), { type:
'module' })
Expand Down
9 changes: 9 additions & 0 deletions playground/worker/worker/main-module.js
Expand Up @@ -90,6 +90,15 @@ wResolve.addEventListener('message', (ev) =>
text('.worker-import-meta-url-resolve', JSON.stringify(ev.data)),
)

// url import worker without extension
const wWithoutExt = new Worker(
new URL('../url-worker', import.meta.url),
/* @vite-ignore */ workerOptions,
)
wWithoutExt.addEventListener('message', (ev) =>
text('.worker-import-meta-url-without-extension', JSON.stringify(ev.data)),
)

const genWorkerName = () => 'module'
const w2 = new SharedWorker(
new URL('../url-shared-worker.js', import.meta.url),
Expand Down