Skip to content
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 12 commits into from
Jan 24, 2025
Merged

Conversation

vursen
Copy link
Contributor

@vursen vursen commented Jan 22, 2025

Description

Updates the service worker plugin to use the Vite official API for building sw.js.

Fixes #20808

Type of change

  • Bugfix

Copy link

github-actions bot commented Jan 22, 2025

Test Results

1 165 files  ± 0  1 165 suites  ±0   1h 27m 29s ⏱️ - 7m 45s
7 625 tests ± 0  7 569 ✅ ± 0  56 💤 ±0  0 ❌ ±0 
7 863 runs   - 21  7 798 ✅  - 20  65 💤  - 1  0 ❌ ±0 

Results for commit 860b5e2. ± Comparison against base commit 711a36c.

♻️ This comment has been updated with latest results.

@vursen vursen force-pushed the fix-service-worker-build branch from 40db9a5 to 0776317 Compare January 22, 2025 15:51
@vursen vursen changed the title fix: fix service worker build fix: use Vite official API for building sw.js Jan 23, 2025
@vursen vursen force-pushed the fix-service-worker-build branch from 1fb1442 to 574ba0c Compare January 23, 2025 10:51
@vursen vursen changed the base branch from Artur--patch-14 to main January 23, 2025 10:51
@vaadin-bot vaadin-bot added +0.0.1 and removed +1.0.0 labels Jan 23, 2025
@vursen vursen force-pushed the fix-service-worker-build branch from 574ba0c to f2882af Compare January 23, 2025 11:00
@vursen vursen marked this pull request as ready for review January 23, 2025 13:09
@vursen vursen changed the title fix: use Vite official API for building sw.js refactor: use Vite official API for building sw.js Jan 23, 2025
@vursen vursen requested review from Artur- and tepi January 23, 2025 13:11
Artur-
Artur- previously approved these changes Jan 23, 2025
Copy link
Member

@Artur- Artur- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to produce the same output as before with only some variable rename changed and umd headers. Using umd is a bit odd but apparently it is what you need to use..

@vursen
Copy link
Contributor Author

vursen commented Jan 23, 2025

We can use 'es', but that prevents whitespace minification. However, this is probably not a big issue since sw.js should be primarily distributed in gzipped format.

@vursen vursen force-pushed the fix-service-worker-build branch from be63677 to 86ade55 Compare January 23, 2025 15:10
@vursen vursen force-pushed the fix-service-worker-build branch from 86ade55 to cd1eb06 Compare January 23, 2025 15:11
@@ -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'],
Copy link
Contributor Author

@vursen vursen Jan 23, 2025

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 because sw.js existed only virtually in dev mode.

Copy link
Member

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

Copy link
Contributor Author

@vursen vursen Jan 24, 2025

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.

Copy link
Contributor Author

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

output: {
exports: 'none',
entryFileNames: 'sw.js',
inlineDynamicImports: true,
Copy link
Contributor Author

@vursen vursen Jan 23, 2025

Choose a reason for hiding this comment

The 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.

sw: settings.clientServiceWorkerSource
},
output: {
exports: 'none',
Copy link
Contributor Author

@vursen vursen Jan 23, 2025

Choose a reason for hiding this comment

The 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.

@vursen
Copy link
Contributor Author

vursen commented Jan 24, 2025

Seems to produce the same output as before with only some variable rename changed and umd headers.

I've happened to find a different approach that avoids umd and preserves minification. The PR uses it now.

@tepi tepi merged commit 9f4eceb into main Jan 24, 2025
26 checks passed
@tepi tepi deleted the fix-service-worker-build branch January 24, 2025 10:55
vaadin-bot pushed a commit that referenced this pull request Jan 24, 2025
* 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]>
@vaadin-bot
Copy link
Collaborator

Hi @vursen and @tepi, when i performed cherry-pick to this commit to 24.5, i have encountered the following issue. Can you take a look and pick it manually?
Error Message:
Error: Command failed: git cherry-pick 9f4eceb
error: could not apply 9f4eceb... refactor: use Vite official API for building sw.js (#20894)
hint: After resolving the conflicts, mark them with
hint: "git add/rm ", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".

vaadin-bot added a commit that referenced this pull request Jan 24, 2025
* 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: Sergey Vinogradov <[email protected]>
Co-authored-by: Teppo Kurki <[email protected]>
tepi added a commit that referenced this pull request Jan 27, 2025
* 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]>
tepi added a commit that referenced this pull request Jan 27, 2025
* 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]>
tepi added a commit that referenced this pull request Jan 27, 2025
#20916)

* 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 <[email protected]>

* Update vite.generated.ts

---------

Co-authored-by: Sergey Vinogradov <[email protected]>
@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with Vaadin 24.7.0.alpha6 and is also targeting the upcoming stable 24.7.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrading to Vite 6.0.7 does not work because of buildSwPlugin
4 participants