Skip to content

Commit

Permalink
refactor: use Vite official API for building sw.js (#20894)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
vursen and tepi committed Jan 27, 2025
1 parent 7350de9 commit b7513a4
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions flow-server/src/main/resources/vite.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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() {
Expand Down

0 comments on commit b7513a4

Please sign in to comment.