mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-20 07:30:57 +00:00
perf(vite): use compiled regexp for test (#18646)
This commit is contained in:
parent
968a652be7
commit
d1a79a95a6
@ -1,16 +1,30 @@
|
||||
import { pathToFileURL } from 'node:url'
|
||||
import MagicString from 'magic-string'
|
||||
import { parseQuery, parseURL } from 'ufo'
|
||||
import type { Plugin } from 'vite'
|
||||
|
||||
export interface RuntimePathsOptions {
|
||||
sourcemap?: boolean
|
||||
}
|
||||
|
||||
const VITE_ASSET_RE = /__VITE_ASSET__|__VITE_PUBLIC_ASSET__/
|
||||
const JS_RE = /\.((c|m)?j|t)sx?$/
|
||||
|
||||
export function runtimePathsPlugin (options: RuntimePathsOptions): Plugin {
|
||||
return {
|
||||
name: 'nuxt:runtime-paths-dep',
|
||||
enforce: 'post',
|
||||
transform (code, id) {
|
||||
if (code.includes('__VITE_ASSET__') || code.includes('__VITE_PUBLIC_ASSET__')) {
|
||||
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
||||
if (pathname.endsWith('.vue')) {
|
||||
// vue files
|
||||
if (search && parseQuery(search).type !== 'script') {
|
||||
return
|
||||
}
|
||||
} else if (!JS_RE.test(pathname)) {
|
||||
return
|
||||
}
|
||||
if (VITE_ASSET_RE.test(code)) {
|
||||
const s = new MagicString(code)
|
||||
// Register dependency on paths.mjs, which sets globalThis.__publicAssetsURL
|
||||
s.prepend('import "#build/paths.mjs";')
|
||||
|
Loading…
Reference in New Issue
Block a user