-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: use Vite official API for building sw.js #20894
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4687227
fix: fix service worker build
vursen 2e72c30
fix PwaTestIT
vursen 94b5a42
exclude sw.js from manifest
vursen c568ab3
generate service worker at closeBundle stage
vursen 0bcc85c
clean up service worker build config
vursen f2882af
generate service worker at different stages depending on mode
vursen bef6e12
do not use lib mode to allow for minification in es format
vursen 9406727
clean up plugin code
vursen dbbda36
add inlineDynamicImports: true option
vursen cd1eb06
revert some changes to minimize diff
vursen 2ab63d1
add exports option for backward compatibility
vursen 860b5e2
Merge branch 'main' into fix-service-worker-build
tepi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,18 +16,18 @@ import settings from '#settingsImport#'; | |
import { | ||
AssetInfo, | ||
ChunkInfo, | ||
build, | ||
defineConfig, | ||
mergeConfig, | ||
OutputOptions, | ||
PluginOption, | ||
ResolvedConfig, | ||
InlineConfig, | ||
UserConfigFn | ||
} from 'vite'; | ||
import { getManifest, type ManifestTransform } from 'workbox-build'; | ||
|
||
import * as rollup from 'rollup'; | ||
import brotli from 'rollup-plugin-brotli'; | ||
import replace from '@rollup/plugin-replace'; | ||
import checker from 'vite-plugin-checker'; | ||
import postcssLit from '#buildFolder#/plugins/rollup-plugin-postcss-lit-custom/rollup-plugin-postcss-lit.js'; | ||
|
||
|
@@ -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, | ||
}); | ||
|
@@ -118,87 +118,53 @@ function injectManifestToSWPlugin(): rollup.Plugin { | |
} | ||
|
||
function buildSWPlugin(opts: { devMode: boolean }): PluginOption { | ||
let config: ResolvedConfig; | ||
let buildConfig: InlineConfig; | ||
const devMode = opts.devMode; | ||
|
||
const swObj: { code?: string, map?: rollup.SourceMap | null } = {}; | ||
|
||
async function build(action: 'generate' | 'write', additionalPlugins: rollup.Plugin[] = []) { | ||
const includedPluginNames = [ | ||
'vite:esbuild', | ||
'rollup-plugin-dynamic-import-variables', | ||
'vite:esbuild-transpile', | ||
'vite:terser' | ||
]; | ||
const plugins: rollup.Plugin[] = config.plugins.filter((p) => { | ||
return includedPluginNames.includes(p.name); | ||
}); | ||
const resolver = config.createResolver(); | ||
const resolvePlugin: rollup.Plugin = { | ||
name: 'resolver', | ||
resolveId(source, importer, _options) { | ||
return resolver(source, importer); | ||
} | ||
}; | ||
plugins.unshift(resolvePlugin); // Put resolve first | ||
plugins.push( | ||
replace({ | ||
values: { | ||
'process.env.NODE_ENV': JSON.stringify(config.mode), | ||
...config.define | ||
}, | ||
preventAssignment: true | ||
}) | ||
); | ||
if (additionalPlugins) { | ||
plugins.push(...additionalPlugins); | ||
} | ||
const bundle = await rollup.rollup({ | ||
input: path.resolve(settings.clientServiceWorkerSource), | ||
plugins | ||
}); | ||
|
||
try { | ||
return await bundle[action]({ | ||
file: path.resolve(buildOutputFolder, 'sw.js'), | ||
format: 'es', | ||
exports: 'none', | ||
sourcemap: config.command === 'serve' || config.build.sourcemap, | ||
inlineDynamicImports: true | ||
}); | ||
} finally { | ||
await bundle.close(); | ||
} | ||
} | ||
|
||
return { | ||
name: 'vaadin:build-sw', | ||
enforce: 'post', | ||
async configResolved(resolvedConfig) { | ||
config = resolvedConfig; | ||
async configResolved(viteConfig) { | ||
buildConfig = { | ||
base: viteConfig.base, | ||
root: viteConfig.root, | ||
mode: viteConfig.mode, | ||
resolve: viteConfig.resolve, | ||
define: { | ||
...viteConfig.define, | ||
'process.env.NODE_ENV': JSON.stringify(viteConfig.mode), | ||
}, | ||
build: { | ||
minify: viteConfig.build.minify, | ||
outDir: viteConfig.build.outDir, | ||
sourcemap: viteConfig.command === 'serve' || viteConfig.build.sourcemap, | ||
emptyOutDir: false, | ||
modulePreload: false, | ||
rollupOptions: { | ||
input: { | ||
sw: settings.clientServiceWorkerSource | ||
}, | ||
output: { | ||
entryFileNames: 'sw.js', | ||
inlineDynamicImports: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: This option is untested, but that can be addressed separately since it's coming from the previous config. |
||
}, | ||
}, | ||
}, | ||
}; | ||
}, | ||
async buildStart() { | ||
if (devMode) { | ||
const { output } = await build('generate'); | ||
swObj.code = output[0].code; | ||
swObj.map = output[0].map; | ||
} | ||
}, | ||
async load(id) { | ||
if (id.endsWith('sw.js')) { | ||
return ''; | ||
} | ||
}, | ||
async transform(_code, id) { | ||
if (id.endsWith('sw.js')) { | ||
return swObj; | ||
await build(buildConfig); | ||
} | ||
}, | ||
async closeBundle() { | ||
if (!devMode) { | ||
await build('write', [injectManifestToSWPlugin(), brotli()]); | ||
await build({ | ||
...buildConfig, | ||
plugins: [injectManifestToSWPlugin(), brotli()] | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: Added this line to prevent
sw.js
from being included in the manifest when the production bundle is built after the development bundle. Previously, this didn't happen becausesw.js
existed only virtually in dev mode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If sw.js is now written to the file system, does it reintroduce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, just confirmed that it does. Thanks for noting this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR with a fix: #20909