Skip to content

Commit 9481c7d

Browse files
authored
fix: worker match only run in js (#7500)
1 parent 4d55218 commit 9481c7d

File tree

10 files changed

+63
-12
lines changed

10 files changed

+63
-12
lines changed

packages/playground/vue/Main.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</Suspense>
2121
<ReactivityTransform :foo="time" />
2222
<SetupImportTemplate />
23+
<WorkerTest />
2324
</template>
2425

2526
<script setup lang="ts">
@@ -35,7 +36,7 @@ import ScanDep from './ScanDep.vue'
3536
import AsyncComponent from './AsyncComponent.vue'
3637
import ReactivityTransform from './ReactivityTransform.vue'
3738
import SetupImportTemplate from './setup-import-template/SetupImportTemplate.vue'
38-
39+
import WorkerTest from './worker.vue'
3940
import { ref } from 'vue'
4041
4142
const time = ref('loading...')

packages/playground/vue/__tests__/vue.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,9 @@ describe('setup import template', () => {
242242
expect(await page.textContent('.setup-import-template')).toMatch('1')
243243
})
244244
})
245+
246+
describe('vue worker', () => {
247+
test('should work', async () => {
248+
expect(await page.textContent('.vue-worker')).toMatch('worker load!')
249+
})
250+
})

packages/playground/vue/worker.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<h2>worker template error match</h2>
3+
<code>
4+
const worker = new Worker(new URL('./worker.js', import.meta.url))
5+
</code>
6+
<div class="vue-worker">{{ message }}</div>
7+
</template>
8+
9+
<script setup>
10+
import { ref } from 'vue'
11+
12+
const message = ref('')
13+
const worker = new Worker(new URL('./workerTest.js', import.meta.url))
14+
worker.addEventListener('message', (ev) => {
15+
message.value = ev.data
16+
})
17+
</script>

packages/playground/vue/workerTest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
self.postMessage('worker load!')

packages/playground/worker/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
<p>worker template error match:</p>
2+
<code>
3+
const worker = new Worker(new URL('./worker.js', import.meta.url))
4+
</code>
5+
16
<h2 class="format-iife">format iife:</h2>
27
<div>Expected values: <span class="mode-true"></span></div>
38
<button class="ping">Ping</button>

packages/vite/src/node/build.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import type {
1919
} from 'rollup'
2020
import type Rollup from 'rollup'
2121
import { buildReporterPlugin } from './plugins/reporter'
22-
import { buildHtmlPlugin } from './plugins/html'
2322
import { buildEsbuildPlugin } from './plugins/esbuild'
2423
import { terserPlugin } from './plugins/terser'
2524
import type { Terser } from 'types/terser'
@@ -310,7 +309,6 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
310309
return {
311310
pre: [
312311
watchPackageDataPlugin(config),
313-
buildHtmlPlugin(config),
314312
commonjsPlugin(options.commonjsOptions),
315313
dataURIPlugin(),
316314
dynamicImportVars(options.dynamicImportVarsOptions),

packages/vite/src/node/plugins/assetImportMetaUrl.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import MagicString from 'magic-string'
33
import path from 'path'
44
import { fileToUrl } from './asset'
55
import type { ResolvedConfig } from '../config'
6-
import { multilineCommentsRE, singlelineCommentsRE } from '../utils'
6+
import {
7+
multilineCommentsRE,
8+
singlelineCommentsRE,
9+
stringsRE,
10+
blankReplacer
11+
} from '../utils'
712

813
/**
914
* Convert `new URL('./foo.png', import.meta.url)` to its resolved built URL
@@ -27,12 +32,18 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
2732
const importMetaUrlRE =
2833
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*,?\s*\)/g
2934
const noCommentsCode = code
30-
.replace(multilineCommentsRE, (m) => ' '.repeat(m.length))
31-
.replace(singlelineCommentsRE, (m) => ' '.repeat(m.length))
35+
.replace(multilineCommentsRE, blankReplacer)
36+
.replace(singlelineCommentsRE, blankReplacer)
37+
.replace(stringsRE, (m) => `'${'\0'.repeat(m.length - 2)}'`)
38+
3239
let s: MagicString | null = null
3340
let match: RegExpExecArray | null
3441
while ((match = importMetaUrlRE.exec(noCommentsCode))) {
35-
const { 0: exp, 1: rawUrl, index } = match
42+
const { 0: exp, 1: emptyUrl, index } = match
43+
44+
const urlStart = exp.indexOf(emptyUrl) + index
45+
const urlEnd = urlStart + emptyUrl.length
46+
const rawUrl = code.slice(urlStart, urlEnd)
3647

3748
if (!s) s = new MagicString(code)
3849

packages/vite/src/node/plugins/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { importAnalysisPlugin } from './importAnalysis'
99
import { cssPlugin, cssPostPlugin } from './css'
1010
import { assetPlugin } from './asset'
1111
import { clientInjectionsPlugin } from './clientInjections'
12-
import { htmlInlineProxyPlugin } from './html'
12+
import { buildHtmlPlugin, htmlInlineProxyPlugin } from './html'
1313
import { wasmPlugin } from './wasm'
1414
import { modulePreloadPolyfillPlugin } from './modulePreloadPolyfill'
1515
import { webWorkerPlugin } from './worker'
@@ -61,12 +61,13 @@ export async function resolvePlugins(
6161
),
6262
wasmPlugin(config),
6363
webWorkerPlugin(config),
64-
workerImportMetaUrlPlugin(config),
6564
assetPlugin(config),
6665
...normalPlugins,
6766
definePlugin(config),
6867
cssPostPlugin(config),
6968
config.build.ssr ? ssrRequireHookPlugin(config) : null,
69+
isBuild && buildHtmlPlugin(config),
70+
workerImportMetaUrlPlugin(config),
7071
...buildPlugins.pre,
7172
...postPlugins,
7273
...buildPlugins.post,

packages/vite/src/node/plugins/workerImportMetaUrl.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
cleanUrl,
88
injectQuery,
99
multilineCommentsRE,
10-
singlelineCommentsRE
10+
singlelineCommentsRE,
11+
stringsRE
1112
} from '../utils'
1213
import path from 'path'
1314
import { bundleWorkerEntry } from './worker'
@@ -122,12 +123,21 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
122123
const noCommentsCode = code
123124
.replace(multilineCommentsRE, blankReplacer)
124125
.replace(singlelineCommentsRE, blankReplacer)
126+
127+
const noStringCode = noCommentsCode.replace(
128+
stringsRE,
129+
(m) => `'${' '.repeat(m.length - 2)}'`
130+
)
125131
let match: RegExpExecArray | null
126132
let s: MagicString | null = null
127-
while ((match = importMetaUrlRE.exec(noCommentsCode))) {
128-
const { 0: allExp, 2: exp, 3: rawUrl, index } = match
133+
while ((match = importMetaUrlRE.exec(noStringCode))) {
134+
const { 0: allExp, 2: exp, 3: emptyUrl, index } = match
129135
const urlIndex = allExp.indexOf(exp) + index
130136

137+
const urlStart = allExp.indexOf(emptyUrl) + index
138+
const urlEnd = urlStart + emptyUrl.length
139+
const rawUrl = code.slice(urlStart, urlEnd)
140+
131141
if (options?.ssr) {
132142
this.error(
133143
`\`new URL(url, import.meta.url)\` is not supported in SSR.`,

packages/vite/src/node/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,4 @@ export function parseRequest(id: string): Record<string, string> | null {
732732
}
733733

734734
export const blankReplacer = (match: string) => ' '.repeat(match.length)
735+
export const stringsRE = /"[^"]*"|'[^']*'|`[^`]*`/g

0 commit comments

Comments
 (0)