Skip to content

Commit

Permalink
chore: remove inlineUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Mar 14, 2023
1 parent 725ed0a commit 6243ddd
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 93 deletions.
7 changes: 0 additions & 7 deletions docs/config/worker-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ Options related to Web Workers.

Output format for worker bundle.

## worker.inlineUrl

- **Type:** `'blob' | 'base64'`
- **Default:** `'blob'`

URL type for inline worker, when setting to `'blob'`, the worker will be loaded as a blob URL with a fallback to data URL. Set to `'base64'` to load the worker as a data URL. When not set, `inlineUrl` will be `'blob'` for Worker and `'base64'` for SharedWorker as blob URL not works with a shared worker.

## worker.plugins

- **Type:** [`(Plugin | Plugin[])[]`](./shared-options#plugins)
Expand Down
7 changes: 0 additions & 7 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,6 @@ export interface UserConfig {
* @default 'iife'
*/
format?: 'es' | 'iife'
/**
* URL type for inline worker
* @default 'blob'
*/
inlineUrl?: 'blob' | 'base64'
/**
* Vite plugins that apply to worker bundle
*/
Expand Down Expand Up @@ -322,7 +317,6 @@ export interface ResolveWorkerOptions extends PluginHookUtils {
format: 'es' | 'iife'
plugins: Plugin[]
rollupOptions: RollupOptions
inlineUrl?: 'blob' | 'base64'
}

export interface InlineConfig extends UserConfig {
Expand Down Expand Up @@ -639,7 +633,6 @@ export async function resolveConfig(
workerConfig = await runConfigHook(workerConfig, workerUserPlugins, configEnv)
const resolvedWorkerOptions: ResolveWorkerOptions = {
format: workerConfig.worker?.format || 'iife',
inlineUrl: workerConfig.worker?.inlineUrl,
plugins: [],
rollupOptions: workerConfig.worker?.rollupOptions || {},
getSortedPlugins: undefined!,
Expand Down
25 changes: 4 additions & 21 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'node:path'
import colors from 'picocolors'
import MagicString from 'magic-string'
import type { EmittedAsset, OutputChunk } from 'rollup'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -279,7 +278,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {

// stringified url or `new URL(...)`
let url: string
const { format, inlineUrl } = config.worker
const { format } = config.worker
const workerConstructor =
query.sharedworker != null ? 'SharedWorker' : 'Worker'
const workerType = isBuild
Expand All @@ -288,6 +287,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
: 'classic'
: 'module'
const workerOptions = workerType === 'classic' ? '' : ',{type: "module"}'

if (isBuild) {
getDepsOptimizer(config, ssr)?.registerWorkersSource(id)
if (query.inline != null) {
Expand All @@ -313,26 +313,9 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
}
`

// If inlineUrl is not specified, use base64 for SharedWorker and blob for Worker
const resolvedInlineUrl = inlineUrl
? inlineUrl
: workerConstructor === 'SharedWorker'
? 'base64'
: 'blob'

if (
workerConstructor === 'SharedWorker' &&
resolvedInlineUrl === 'blob'
) {
config.logger.warn(
colors.yellow(
`\nThe inlined SharedWorker: ${id} does not work with blob URL, considering set 'worker.inlineUrl' to 'base64'. \n`,
),
)
}

return {
code: resolvedInlineUrl === 'blob' ? blobCode : base64Code,
// SharedWorker does not support blob URL
code: workerConstructor === 'Worker' ? blobCode : base64Code,
// Empty sourcemap to suppress Rollup warning
map: { mappings: '' },
}
Expand Down
4 changes: 4 additions & 0 deletions playground/worker/__tests__/es/es-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ test('shared worker', async () => {
await untilUpdated(() => page.textContent('.tick-count'), 'pong', true)
})

test('inline shared worker', async () => {
await untilUpdated(() => page.textContent('.pong-shared-inline'), 'pong')
})

test('worker emitted and import.meta.url in nested worker (serve)', async () => {
await untilUpdated(
() => page.textContent('.nested-worker'),
Expand Down
4 changes: 4 additions & 0 deletions playground/worker/__tests__/iife/iife-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ test('shared worker', async () => {
await untilUpdated(() => page.textContent('.tick-count'), 'pong')
})

test('inline shared worker', async () => {
await untilUpdated(() => page.textContent('.pong-shared-inline'), 'pong')
})

test('worker emitted and import.meta.url in nested worker (serve)', async () => {
await untilUpdated(() => page.textContent('.nested-worker'), '/worker-nested')
await untilUpdated(
Expand Down
27 changes: 0 additions & 27 deletions playground/worker/__tests__/inline-url/inline-url-worker.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion playground/worker/__tests__/inline-url/vite.config.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ test('shared worker', async () => {
await untilUpdated(() => page.textContent('.tick-count'), 'pong', true)
})

test('inline shared worker', async () => {
await untilUpdated(() => page.textContent('.pong-shared-inline'), 'pong')
})

test('worker emitted and import.meta.url in nested worker (serve)', async () => {
await untilUpdated(
() => page.textContent('.nested-worker'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe.runIf(isBuild)('build', () => {

const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
expect(files.length).toBe(31)
expect(files.length).toBe(32)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe.runIf(isBuild)('build', () => {
const assetsDir = path.resolve(testDir, 'dist/iife-sourcemap/assets')
const files = fs.readdirSync(assetsDir)
// should have 2 worker chunk
expect(files.length).toBe(31)
expect(files.length).toBe(32)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
Expand Down
12 changes: 6 additions & 6 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="pong-inline"></code>

<p>
import InlineSharedWorker from '../my-shared-worker?sharedworker&inline'
<span class="classname">.pong-shared-inline</span>
</p>
<code class="pong-shared-inline"></code>

<p>
import TSOutputWorker from '../possible-ts-output-worker?worker'
<span class="classname">.pong-ts-output</span>
Expand All @@ -44,6 +38,12 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="tick-count"></code>

<p>
import InlineSharedWorker from '../my-shared-worker?sharedworker&inline'
<span class="classname">.pong-shared-inline</span>
</p>
<code class="pong-shared-inline"></code>

<p>
new Worker(new URL('./url-worker.js', import.meta.url), { type: 'module' })
<span class="classname">.worker-import-meta-url</span>
Expand Down
13 changes: 13 additions & 0 deletions playground/worker/my-inline-shared-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let inlineSharedWorkerCount = 0

// @ts-expect-error onconnect exists in worker
self.onconnect = (event) => {
inlineSharedWorkerCount++
const port = event.ports[0]
if (inlineSharedWorkerCount >= 2) {
port.postMessage('pong')
}
}

// for sourcemap
console.log('my-inline-shared-worker.js')
2 changes: 1 addition & 1 deletion playground/worker/my-shared-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let sharedWorkerCount = 0
self.onconnect = (event) => {
sharedWorkerCount++
const port = event.ports[0]
if (sharedWorkerCount === 2) {
if (sharedWorkerCount >= 2) {
port.postMessage('pong')
}
}
Expand Down
3 changes: 0 additions & 3 deletions playground/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
"dev:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js dev",
"build:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js build",
"preview:relative-base": "WORKER_MODE=inline vite --config ./vite.config-relative-base.js preview",
"dev:inline-url": "vite --config ./vite.config-inline-url.js dev",
"build:inline-url": "vite --config ./vite.config-inline-url.js build",
"preview:inline-url": "vite --config ./vite.config-inline-url.js preview",
"debug": "node --inspect-brk ../../packages/vite/bin/vite"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions playground/worker/vite.config-es.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import vite from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'

/** @type {import('vite').UserConfig} */
// @ts-expect-error typecast
export default vite.defineConfig({
base: '/es/',
resolve: {
Expand Down
15 changes: 0 additions & 15 deletions playground/worker/vite.config-inline-url.js

This file was deleted.

2 changes: 1 addition & 1 deletion playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import myWorker from '../my-worker.ts?worker'
import InlineWorker from '../my-worker.ts?worker&inline'
import InlineSharedWorker from '../my-shared-worker?sharedworker&inline'
import InlineSharedWorker from '../my-inline-shared-worker?sharedworker&inline'
import mySharedWorker from '../my-shared-worker?sharedworker&name=shared'
import TSOutputWorker from '../possible-ts-output-worker?worker'
import NestedWorker from '../worker-nested-worker?worker'
Expand Down

0 comments on commit 6243ddd

Please sign in to comment.