From b7513a49984ee91ff9e43ca7b25a4d61e5f7bcb8 Mon Sep 17 00:00:00 2001 From: Sergey Vinogradov Date: Fri, 24 Jan 2025 14:55:50 +0400 Subject: [PATCH] refactor: use Vite official API for building sw.js (#20894) * fix: fix service worker build * fix PwaTestIT * exclude sw.js from manifest * generate service worker at closeBundle stage * clean up service worker build config * generate service worker at different stages depending on mode * do not use lib mode to allow for minification in es format * clean up plugin code * add inlineDynamicImports: true option * revert some changes to minimize diff * add exports option for backward compatibility --------- Co-authored-by: Teppo Kurki --- flow-server/src/main/resources/vite.generated.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/flow-server/src/main/resources/vite.generated.ts b/flow-server/src/main/resources/vite.generated.ts index b6f8fa82d29..9da66af2469 100644 --- a/flow-server/src/main/resources/vite.generated.ts +++ b/flow-server/src/main/resources/vite.generated.ts @@ -106,7 +106,7 @@ function injectManifestToSWPlugin(): rollup.Plugin { const { manifestEntries } = await getManifest({ globDirectory: buildOutputFolder, globPatterns: ['**/*'], - globIgnores: ['**/*.br', 'pwa-icons/**'], + globIgnores: ['**/*.br', 'pwa-icons/**', 'sw.js'], manifestTransforms: [rewriteManifestIndexHtmlUrl], maximumFileSizeToCacheInBytes: 100 * 1024 * 1024 // 100mb, }); @@ -119,7 +119,6 @@ function injectManifestToSWPlugin(): rollup.Plugin { function buildSWPlugin(opts: { devMode: boolean }): PluginOption { let buildConfig: InlineConfig; - let buildOutput: rollup.RollupOutput; const devMode = opts.devMode; return { @@ -136,7 +135,6 @@ function buildSWPlugin(opts: { devMode: boolean }): PluginOption { 'process.env.NODE_ENV': JSON.stringify(viteConfig.mode), }, build: { - write: !devMode, minify: viteConfig.build.minify, outDir: viteConfig.build.outDir, sourcemap: viteConfig.command === 'serve' || viteConfig.build.sourcemap, @@ -157,12 +155,7 @@ function buildSWPlugin(opts: { devMode: boolean }): PluginOption { }, async buildStart() { if (devMode) { - buildOutput = await build(buildConfig) as rollup.RollupOutput; - } - }, - async load(id) { - if (id.endsWith('sw.js')) { - return buildOutput.output[0].code; + await build(buildConfig); } }, async closeBundle() {