From 8e88e12f5db8e1cea10b814e41f64aabd065d5f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Fri, 1 Sep 2023 18:09:02 +0200 Subject: [PATCH] fix(vite): transform import.meta.url in optimized deps --- packages/vite/src/node/config.ts | 44 +++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index e05fe34cc44c2c..020bc2b3684e4a 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1,7 +1,7 @@ import fs from 'node:fs' import fsp from 'node:fs/promises' import path from 'node:path' -import { pathToFileURL } from 'node:url' +import { fileURLToPath, pathToFileURL } from 'node:url' import { promisify } from 'node:util' import { performance } from 'node:perf_hooks' import { createRequire } from 'node:module' @@ -740,6 +740,48 @@ export async function resolveConfig( esbuildOptions: { preserveSymlinks: resolveOptions.preserveSymlinks, ...optimizeDeps.esbuildOptions, + plugins: [ + { + /** + * Transform `new URL('./foo.png', import.meta.url)` syntaxes to its absolute path + * So that it's still correct when the file content is optimized and its path changed + */ + name: 'esbuid:asset-import-meta-url', + setup({ onLoad }) { + onLoad({ filter: /.*\.js/, namespace: 'file' }, async (args) => { + const code = fs.readFileSync(args.path, 'utf8') + + const assetImportMetaUrlRE = + /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g + let i = 0 + let newCode = '' + for ( + let match = assetImportMetaUrlRE.exec(code); + match != null; + match = assetImportMetaUrlRE.exec(code) + ) { + newCode += code.slice(i, match.index) + + const path = match[1].slice(1, -1) + const resolved = await import.meta.resolve!( + path, + pathToFileURL(args.path), + ) + + newCode += `new URL(${JSON.stringify( + fileURLToPath(resolved), + )}, import.meta.url)` + + i = assetImportMetaUrlRE.lastIndex + } + newCode += code.slice(i) + + return { contents: newCode } + }) + }, + }, + ...(optimizeDeps.esbuildOptions?.plugins ?? []), + ], }, }, worker: resolvedWorkerOptions,