Skip to content

Commit

Permalink
refactor: stop replace import.meta.url to self.location.href
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Apr 14, 2022
1 parent f6710c3 commit 9e86745
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 54 deletions.
11 changes: 0 additions & 11 deletions packages/playground/worker/__tests__/es/es-worker.spec.ts
Expand Up @@ -85,17 +85,6 @@ if (isBuild) {
})

test('worker emitted and import.meta.url in nested worker (build)', async () => {
// import.meta.url will minify in esbuild in build mode so can't use runtime result.
const files = fs.readdirSync(assetsDir)
const nestedWorkerFile = files.find((f) =>
f.includes('worker-nested-worker')
)
const content = fs.readFileSync(
path.resolve(assetsDir, nestedWorkerFile),
'utf-8'
)
expect(content).toMatch('self.location.href')

expect(await page.textContent('.nested-worker-module')).toMatch(
'"type":"module"'
)
Expand Down
11 changes: 0 additions & 11 deletions packages/playground/worker/__tests__/iife/worker.spec.ts
Expand Up @@ -85,17 +85,6 @@ if (isBuild) {
})

test('worker emitted and import.meta.url in nested worker (build)', async () => {
// import.meta.url will minify in esbuild in build mode so can't use runtime result.
const files = fs.readdirSync(assetsDir)
const nestedWorkerFile = files.find((f) =>
f.includes('worker-nested-worker')
)
const content = fs.readFileSync(
path.resolve(assetsDir, nestedWorkerFile),
'utf-8'
)
expect(content).toMatch('self.location.href')

expect(await page.textContent('.nested-worker-module')).toMatch(
'"type":"module"'
)
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/worker/sub-worker.js
@@ -1,6 +1,6 @@
self.onmessage = (event) => {
if (event.data === 'ping') {
self.postMessage(`pong ${import.meta.url}`)
self.postMessage(`pong ${self.location.href}`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/playground/worker/url-worker.js
@@ -1,4 +1,4 @@
self.postMessage('A string' + import.meta.env.BASE_URL + import.meta.url)
self.postMessage('A string' + import.meta.env.BASE_URL + self.location.url)

// for sourcemap
console.log('url-worker.js')
2 changes: 1 addition & 1 deletion packages/playground/worker/worker-nested-worker.js
Expand Up @@ -8,7 +8,7 @@ self.onmessage = (event) => {
}
}

self.postMessage(import.meta.url)
self.postMessage(self.location.href)

subWorker.onmessage = (ev) => {
self.postMessage({
Expand Down
2 changes: 0 additions & 2 deletions packages/vite/src/node/build.ts
Expand Up @@ -40,7 +40,6 @@ import { assetImportMetaUrlPlugin } from './plugins/assetImportMetaUrl'
import { loadFallbackPlugin } from './plugins/loadFallback'
import { watchPackageDataPlugin } from './packages'
import { ensureWatchPlugin } from './plugins/ensureWatch'
import { redirectWorkerImportMetaUrlPlugin } from './plugins/worker'

export interface BuildOptions {
/**
Expand Down Expand Up @@ -316,7 +315,6 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
dataURIPlugin(),
dynamicImportVars(options.dynamicImportVarsOptions),
assetImportMetaUrlPlugin(config),
redirectWorkerImportMetaUrlPlugin(config),
...(options.rollupOptions.plugins
? (options.rollupOptions.plugins.filter(Boolean) as Plugin[])
: [])
Expand Down
27 changes: 0 additions & 27 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -7,7 +7,6 @@ import { ENV_PUBLIC_PATH } from '../constants'
import path from 'path'
import { onRollupWarning } from '../build'
import type { TransformPluginContext, EmittedFile } from 'rollup'
import MagicString from 'magic-string'

interface WorkerCache {
// save worker bundle emitted files avoid overwrites the same file.
Expand Down Expand Up @@ -287,29 +286,3 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
}
}
}

// just run in build mode
export function redirectWorkerImportMetaUrlPlugin(
config: ResolvedConfig
): Plugin {
const isWorker = config.isWorker

return {
name: 'vite:workerImportMetaUrl',
transform(code, id) {
if (isWorker) {
// if build with iife it will polyfill with `rollup` will be used document in the worker
// else if build with es it will replace with `esbuild` will got a unexpected data.
// so replace import.meta.url break the default handle.
// And it must be done at this (after `(?new Worker)new URL('xxx', import.meta.url)` match),
// Otherwise, the following regex will be incorrectly matched.
const s = new MagicString(code, {})
s.replace(/\bimport.meta.url\b/g, 'self.location.href')
return {
code: s.toString(),
map: s.generateMap({ source: id, hires: true })
}
}
}
}
}

0 comments on commit 9e86745

Please sign in to comment.