Skip to content

Commit 2a61dd7

Browse files
committed
wip: optimize vue relative asset reference + handle out of root assets
1 parent 88571bb commit 2a61dd7

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/plugin-vue/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"debug": "^4.3.1",
3535
"hash-sum": "^2.0.0",
3636
"rollup": "^2.35.1",
37+
"slash": "^3.0.0",
3738
"source-map": "^0.6.1"
3839
}
3940
}

packages/plugin-vue/src/template.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path'
2+
import slash from 'slash'
13
import {
24
compileTemplate,
35
SFCDescriptor,
@@ -92,19 +94,44 @@ export function getTemplateCompilerOptions(
9294
}
9395
const resolvedScript = getResolvedScript(descriptor, options.ssr)
9496
const hasScoped = descriptor.styles.some((s) => s.scoped)
97+
const { id, filename, cssVars } = descriptor
98+
99+
let transformAssetUrls = options.template?.transformAssetUrls
100+
// inject vite base so that @vue/compiler-sfc can transform relative paths
101+
// directly to absolute paths without incurring an extra import request
102+
if (filename.startsWith(options.root)) {
103+
// TODO account for vite base config
104+
const base =
105+
'/' + slash(path.relative(options.root, path.dirname(filename)))
106+
if (transformAssetUrls && typeof transformAssetUrls === 'object') {
107+
// presence of array fields means this is raw tags config
108+
if (
109+
Object.keys(transformAssetUrls).some((key) =>
110+
Array.isArray((transformAssetUrls as any)[key])
111+
)
112+
) {
113+
transformAssetUrls = { base, tags: transformAssetUrls } as any
114+
} else {
115+
transformAssetUrls = { ...transformAssetUrls, base }
116+
}
117+
} else {
118+
transformAssetUrls = { base }
119+
}
120+
}
121+
95122
return {
96123
...options.template,
97-
id: descriptor.id,
124+
id,
125+
filename,
98126
scoped: hasScoped,
99127
isProd: options.isProduction,
100-
filename: descriptor.filename,
101128
inMap: block.src ? undefined : block.map,
102129
ssr: options.ssr,
103-
ssrCssVars: descriptor.cssVars,
104-
transformAssetUrls: options.template?.transformAssetUrls,
130+
ssrCssVars: cssVars,
131+
transformAssetUrls,
105132
compilerOptions: {
106133
...options.template?.compilerOptions,
107-
scopeId: hasScoped ? `data-v-${descriptor.id}` : undefined,
134+
scopeId: hasScoped ? `data-v-${id}` : undefined,
108135
bindingMetadata: resolvedScript ? resolvedScript.bindings : undefined
109136
}
110137
}

0 commit comments

Comments
 (0)