|
| 1 | +import path from 'path' |
| 2 | +import slash from 'slash' |
1 | 3 | import {
|
2 | 4 | compileTemplate,
|
3 | 5 | SFCDescriptor,
|
@@ -92,19 +94,44 @@ export function getTemplateCompilerOptions(
|
92 | 94 | }
|
93 | 95 | const resolvedScript = getResolvedScript(descriptor, options.ssr)
|
94 | 96 | 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 | + |
95 | 122 | return {
|
96 | 123 | ...options.template,
|
97 |
| - id: descriptor.id, |
| 124 | + id, |
| 125 | + filename, |
98 | 126 | scoped: hasScoped,
|
99 | 127 | isProd: options.isProduction,
|
100 |
| - filename: descriptor.filename, |
101 | 128 | inMap: block.src ? undefined : block.map,
|
102 | 129 | ssr: options.ssr,
|
103 |
| - ssrCssVars: descriptor.cssVars, |
104 |
| - transformAssetUrls: options.template?.transformAssetUrls, |
| 130 | + ssrCssVars: cssVars, |
| 131 | + transformAssetUrls, |
105 | 132 | compilerOptions: {
|
106 | 133 | ...options.template?.compilerOptions,
|
107 |
| - scopeId: hasScoped ? `data-v-${descriptor.id}` : undefined, |
| 134 | + scopeId: hasScoped ? `data-v-${id}` : undefined, |
108 | 135 | bindingMetadata: resolvedScript ? resolvedScript.bindings : undefined
|
109 | 136 | }
|
110 | 137 | }
|
|
0 commit comments