Skip to content

Commit 7b1b5a0

Browse files
committed
wip: asset resolution and tests
1 parent 8fc4c84 commit 7b1b5a0

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

packages/plugin-vue/src/script.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { compileScript, SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc'
22
import { ResolvedOptions } from '.'
3-
import { getTemplateCompilerOptions } from './template'
3+
import { resolveTemplateCompilerOptions } from './template'
44

55
// ssr and non ssr builds would output different script content
66
const clientCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>()
@@ -41,7 +41,7 @@ export function resolveScript(
4141
id: descriptor.id,
4242
isProd: options.isProduction,
4343
inlineTemplate: !options.devServer,
44-
templateOptions: getTemplateCompilerOptions(descriptor, options)
44+
templateOptions: resolveTemplateCompilerOptions(descriptor, options)
4545
})
4646

4747
cacheToUse.set(descriptor, resolved)

packages/plugin-vue/src/template.ts

+33-19
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function compile(
5858
) {
5959
const filename = descriptor.filename
6060
const result = compileTemplate({
61-
...getTemplateCompilerOptions(descriptor, options)!,
61+
...resolveTemplateCompilerOptions(descriptor, options)!,
6262
source: code
6363
})
6464

@@ -84,7 +84,7 @@ export function compile(
8484
return result
8585
}
8686

87-
export function getTemplateCompilerOptions(
87+
export function resolveTemplateCompilerOptions(
8888
descriptor: SFCDescriptor,
8989
options: ResolvedOptions
9090
): Omit<SFCTemplateCompileOptions, 'source'> | undefined {
@@ -97,26 +97,40 @@ export function getTemplateCompilerOptions(
9797
const { id, filename, cssVars } = descriptor
9898

9999
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 }
100+
let assetUrlOptions
101+
if (options.devServer) {
102+
// during dev, inject vite base so that @vue/compiler-sfc can transform
103+
// relative paths directly to absolute paths without incurring an extra import
104+
// request
105+
if (filename.startsWith(options.root)) {
106+
assetUrlOptions = {
107+
base: '/' + slash(path.relative(options.root, path.dirname(filename)))
108+
}
109+
}
110+
} else {
111+
// build: force all asset urls into import requests so that they go through
112+
// the assets plugin for asset registration
113+
assetUrlOptions = {
114+
includeAbsolute: true
115+
}
116+
}
117+
118+
if (transformAssetUrls && typeof transformAssetUrls === 'object') {
119+
// presence of array fields means this is raw tags config
120+
if (
121+
Object.keys(transformAssetUrls).some((key) =>
122+
Array.isArray((transformAssetUrls as any)[key])
123+
)
124+
) {
125+
transformAssetUrls = {
126+
...assetUrlOptions,
127+
tags: transformAssetUrls as any
116128
}
117129
} else {
118-
transformAssetUrls = { base }
130+
transformAssetUrls = { ...transformAssetUrls, ...assetUrlOptions }
119131
}
132+
} else {
133+
transformAssetUrls = assetUrlOptions
120134
}
121135

122136
return {

scripts/jestGlobalSetup.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const DIR = path.join(os.tmpdir(), 'jest_playwright_global_setup')
77

88
module.exports = async () => {
99
const browserServer = await chromium.launchServer({
10+
headless: !process.env.VITE_DEBUG_SERVE,
1011
args: process.env.CI
1112
? ['--no-sandbox', '--disable-setuid-sandbox']
1213
: undefined

scripts/jestPerTestSetup.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ beforeAll(async () => {
2828
const srcDir = resolve(playgroundRoot, testName)
2929
tempDir = resolve(__dirname, '../temp', testName)
3030
await fs.copy(srcDir, tempDir, {
31+
dereference: true,
3132
filter(file) {
3233
return !file.includes('__tests__')
3334
}

0 commit comments

Comments
 (0)