Skip to content

Commit 22b26d2

Browse files
committed
refactor(vite): extract html transform helper
1 parent f98a2ed commit 22b26d2

2 files changed

Lines changed: 79 additions & 63 deletions

File tree

packages/mokup/src/vite/plugin.ts

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Plugin, PreviewServer, ViteDevServer } from 'vite'
22
import type { MokupPluginOptions } from '../shared/types'
3-
43
import type { PluginState } from './plugin/state'
54
import { cwd } from 'node:process'
65
import {
@@ -11,6 +10,7 @@ import {
1110
import { createPlaygroundMiddleware, resolvePlaygroundOptions, resolveSwConfig, resolveSwUnregisterConfig, writePlaygroundBuild } from '../internal/core'
1211
import { resolvePlaygroundDist } from '../playground/assets'
1312
import { createLogger } from '../shared/logger'
13+
import { transformMokupIndexHtml } from './plugin/html-transform'
1414
import { normalizeMokupOptions, normalizeOptions } from './plugin/options'
1515
import { resolveSwImportPath } from './plugin/paths'
1616
import { createRouteRefresher } from './plugin/refresh'
@@ -19,24 +19,6 @@ import { configureDevServer, configurePreviewServer } from './plugin/server-hook
1919
import { buildSwLifecycleInlineScript, buildSwLifecycleScript } from './plugin/sw'
2020
import { loadMokupVirtualModule, resolveMokupVirtualId } from './plugin/virtual-modules'
2121

22-
/**
23-
* Create the mokup Vite plugin.
24-
*
25-
* @param options - Plugin options.
26-
* @returns Vite plugin instance.
27-
*
28-
* @example
29-
* import mokup from 'mokup/vite'
30-
*
31-
* export default {
32-
* plugins: [
33-
* mokup({
34-
* entries: { dir: 'mock', prefix: '/api' },
35-
* playground: true,
36-
* }),
37-
* ],
38-
* }
39-
*/
4022
export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
4123
let root = cwd()
4224
let base = '/'
@@ -60,7 +42,6 @@ export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
6042
type PreviewWatcher = Awaited<ReturnType<typeof configurePreviewServer>>
6143
let previewWatcher: PreviewWatcher | null = null
6244
let currentServer: ViteDevServer | PreviewServer | null = null
63-
6445
const normalizedOptions = normalizeMokupOptions(options)
6546
const runtime = normalizedOptions.runtime ?? 'node'
6647
const enableViteMiddleware = runtime !== 'worker'
@@ -93,15 +74,13 @@ export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
9374
if (swDiagnosticError) {
9475
throw swDiagnosticError
9576
}
96-
9777
const resolveAllDirs = createDirResolver(optionList, () => root)
9878
const hasSwRoutes = () => !!swConfig && state.swRoutes.length > 0
9979
const { resolveSwRequestPath, resolveSwRegisterScope } = createSwPathResolver(() => base)
10080
const { resolveHtmlAssetPath, resolveAssetsFileName } = createHtmlAssetResolver(
10181
() => base,
10282
() => assetsDir,
10383
)
104-
10584
const swVirtualId = 'virtual:mokup-sw'
10685
const resolvedSwVirtualId = `\0${swVirtualId}`
10786
const swLifecycleVirtualId = 'virtual:mokup-sw-lifecycle'
@@ -110,7 +89,6 @@ export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
11089
const resolvedBundleVirtualId = `\0${bundleVirtualId}`
11190
let swLifecycleFileName: string | null = null
11291
let swLifecycleScript: string | null = null
113-
11492
const playgroundMiddleware = createPlaygroundMiddleware({
11593
getRoutes: () => state.routes,
11694
getDisabledRoutes: () => state.disabledRoutes,
@@ -132,7 +110,6 @@ export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
132110
}),
133111
resolvePlaygroundDist,
134112
})
135-
136113
const refreshRouteParams: Parameters<typeof createRouteRefresher>[0] = {
137114
state,
138115
optionList,
@@ -146,7 +123,6 @@ export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
146123
refreshRouteParams.errorOn = normalizedOptions.errorOn
147124
}
148125
const refreshRoutes = createRouteRefresher(refreshRouteParams)
149-
150126
return {
151127
name: 'mokup:vite',
152128
enforce: 'pre',
@@ -183,9 +159,7 @@ export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
183159
resolveSwRequestPath,
184160
resolveSwRegisterScope,
185161
swLifecycleScript,
186-
setSwLifecycleScript: (value) => {
187-
swLifecycleScript = value
188-
},
162+
setSwLifecycleScript: (value) => { swLifecycleScript = value },
189163
})
190164
},
191165
async buildStart() {
@@ -227,48 +201,22 @@ export function createMokupPlugin(options: MokupPluginOptions = {}): Plugin {
227201
})
228202
},
229203
async transformIndexHtml(html) {
230-
if (state.swRoutes.length === 0) {
231-
await refreshRoutes(currentServer ?? undefined)
232-
}
233-
const script = buildSwLifecycleScript({
234-
importPath: command === 'build' ? 'mokup/sw' : resolveSwImportPath(base),
204+
return transformMokupIndexHtml(html, {
205+
state,
206+
refreshRoutes,
207+
currentServer,
208+
command,
209+
base,
235210
swConfig,
236211
unregisterConfig,
237212
hasSwEntries,
238213
hasSwRoutes: hasSwRoutes(),
214+
resolveSwImportPath,
239215
resolveRequestPath: resolveSwRequestPath,
240216
resolveRegisterScope: resolveSwRegisterScope,
217+
swLifecycleFileName,
218+
resolveHtmlAssetPath,
241219
})
242-
if (!script) {
243-
return html
244-
}
245-
if (command === 'build') {
246-
if (!swLifecycleFileName) {
247-
return html
248-
}
249-
const src = resolveHtmlAssetPath(swLifecycleFileName)
250-
return {
251-
html,
252-
tags: [
253-
{
254-
tag: 'script',
255-
attrs: { type: 'module', src },
256-
injectTo: 'head',
257-
},
258-
],
259-
}
260-
}
261-
return {
262-
html,
263-
tags: [
264-
{
265-
tag: 'script',
266-
attrs: { type: 'module' },
267-
children: script,
268-
injectTo: 'head',
269-
},
270-
],
271-
}
272220
},
273221
configResolved(config) {
274222
root = config.root
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { ResolvedSwConfig } from '@mokup/core'
2+
import type { IndexHtmlTransformResult, PreviewServer, ViteDevServer } from 'vite'
3+
import { buildSwLifecycleScript } from './sw'
4+
5+
async function transformMokupIndexHtml(
6+
html: string,
7+
params: {
8+
state: { swRoutes: unknown[] }
9+
refreshRoutes: (server?: ViteDevServer | PreviewServer) => Promise<void>
10+
currentServer: ViteDevServer | PreviewServer | null
11+
command: 'serve' | 'build'
12+
base: string
13+
swConfig: ResolvedSwConfig | null
14+
unregisterConfig: ResolvedSwConfig
15+
hasSwEntries: boolean
16+
hasSwRoutes: boolean
17+
resolveSwImportPath: (base: string) => string
18+
resolveRequestPath: (path: string) => string
19+
resolveRegisterScope: (scope: string) => string
20+
swLifecycleFileName: string | null
21+
resolveHtmlAssetPath: (path: string) => string
22+
},
23+
): Promise<string | IndexHtmlTransformResult> {
24+
if (params.state.swRoutes.length === 0) {
25+
await params.refreshRoutes(params.currentServer ?? undefined)
26+
}
27+
const script = buildSwLifecycleScript({
28+
importPath: params.command === 'build' ? 'mokup/sw' : params.resolveSwImportPath(params.base),
29+
swConfig: params.swConfig,
30+
unregisterConfig: params.unregisterConfig,
31+
hasSwEntries: params.hasSwEntries,
32+
hasSwRoutes: params.hasSwRoutes,
33+
resolveRequestPath: params.resolveRequestPath,
34+
resolveRegisterScope: params.resolveRegisterScope,
35+
})
36+
if (!script) {
37+
return html
38+
}
39+
if (params.command === 'build') {
40+
if (!params.swLifecycleFileName) {
41+
return html
42+
}
43+
const src = params.resolveHtmlAssetPath(params.swLifecycleFileName)
44+
return {
45+
html,
46+
tags: [
47+
{
48+
tag: 'script',
49+
attrs: { type: 'module', src },
50+
injectTo: 'head' as const,
51+
},
52+
],
53+
}
54+
}
55+
return {
56+
html,
57+
tags: [
58+
{
59+
tag: 'script',
60+
attrs: { type: 'module' },
61+
children: script,
62+
injectTo: 'head' as const,
63+
},
64+
],
65+
}
66+
}
67+
68+
export { transformMokupIndexHtml }

0 commit comments

Comments
 (0)