11import type { Plugin , PreviewServer , ViteDevServer } from 'vite'
22import type { MokupPluginOptions } from '../shared/types'
3-
43import type { PluginState } from './plugin/state'
54import { cwd } from 'node:process'
65import {
@@ -11,6 +10,7 @@ import {
1110import { createPlaygroundMiddleware , resolvePlaygroundOptions , resolveSwConfig , resolveSwUnregisterConfig , writePlaygroundBuild } from '../internal/core'
1211import { resolvePlaygroundDist } from '../playground/assets'
1312import { createLogger } from '../shared/logger'
13+ import { transformMokupIndexHtml } from './plugin/html-transform'
1414import { normalizeMokupOptions , normalizeOptions } from './plugin/options'
1515import { resolveSwImportPath } from './plugin/paths'
1616import { createRouteRefresher } from './plugin/refresh'
@@ -19,24 +19,6 @@ import { configureDevServer, configurePreviewServer } from './plugin/server-hook
1919import { buildSwLifecycleInlineScript , buildSwLifecycleScript } from './plugin/sw'
2020import { 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- */
4022export 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
0 commit comments