@@ -16,18 +16,18 @@ import settings from '#settingsImport#';
16
16
import {
17
17
AssetInfo ,
18
18
ChunkInfo ,
19
+ build ,
19
20
defineConfig ,
20
21
mergeConfig ,
21
22
OutputOptions ,
22
23
PluginOption ,
23
- ResolvedConfig ,
24
+ InlineConfig ,
24
25
UserConfigFn
25
26
} from 'vite' ;
26
27
import { getManifest , type ManifestTransform } from 'workbox-build' ;
27
28
28
29
import * as rollup from 'rollup' ;
29
30
import brotli from 'rollup-plugin-brotli' ;
30
- import replace from '@rollup/plugin-replace' ;
31
31
import checker from 'vite-plugin-checker' ;
32
32
import postcssLit from '#buildFolder#/plugins/rollup-plugin-postcss-lit-custom/rollup-plugin-postcss-lit.js' ;
33
33
@@ -106,7 +106,7 @@ function injectManifestToSWPlugin(): rollup.Plugin {
106
106
const { manifestEntries } = await getManifest ( {
107
107
globDirectory : buildOutputFolder ,
108
108
globPatterns : [ '**/*' ] ,
109
- globIgnores : [ '**/*.br' ] ,
109
+ globIgnores : [ '**/*.br' , 'pwa-icons/**' , 'sw.js' ] ,
110
110
manifestTransforms : [ rewriteManifestIndexHtmlUrl ] ,
111
111
maximumFileSizeToCacheInBytes : 100 * 1024 * 1024 // 100mb,
112
112
} ) ;
@@ -118,87 +118,54 @@ function injectManifestToSWPlugin(): rollup.Plugin {
118
118
}
119
119
120
120
function buildSWPlugin ( opts : { devMode : boolean } ) : PluginOption {
121
- let config : ResolvedConfig ;
121
+ let buildConfig : InlineConfig ;
122
122
const devMode = opts . devMode ;
123
123
124
- const swObj : { code ?: string , map ?: rollup . SourceMap | null } = { } ;
125
-
126
- async function build ( action : 'generate' | 'write' , additionalPlugins : rollup . Plugin [ ] = [ ] ) {
127
- const includedPluginNames = [
128
- 'vite:esbuild' ,
129
- 'rollup-plugin-dynamic-import-variables' ,
130
- 'vite:esbuild-transpile' ,
131
- 'vite:terser'
132
- ] ;
133
- const plugins : rollup . Plugin [ ] = config . plugins . filter ( ( p ) => {
134
- return includedPluginNames . includes ( p . name ) ;
135
- } ) ;
136
- const resolver = config . createResolver ( ) ;
137
- const resolvePlugin : rollup . Plugin = {
138
- name : 'resolver' ,
139
- resolveId ( source , importer , _options ) {
140
- return resolver ( source , importer ) ;
141
- }
142
- } ;
143
- plugins . unshift ( resolvePlugin ) ; // Put resolve first
144
- plugins . push (
145
- replace ( {
146
- values : {
147
- 'process.env.NODE_ENV' : JSON . stringify ( config . mode ) ,
148
- ...config . define
149
- } ,
150
- preventAssignment : true
151
- } )
152
- ) ;
153
- if ( additionalPlugins ) {
154
- plugins . push ( ...additionalPlugins ) ;
155
- }
156
- const bundle = await rollup . rollup ( {
157
- input : path . resolve ( settings . clientServiceWorkerSource ) ,
158
- plugins
159
- } ) ;
160
-
161
- try {
162
- return await bundle [ action ] ( {
163
- file : path . resolve ( buildOutputFolder , 'sw.js' ) ,
164
- format : 'es' ,
165
- exports : 'none' ,
166
- sourcemap : config . command === 'serve' || config . build . sourcemap ,
167
- inlineDynamicImports : true
168
- } ) ;
169
- } finally {
170
- await bundle . close ( ) ;
171
- }
172
- }
173
-
174
124
return {
175
125
name : 'vaadin:build-sw' ,
176
126
enforce : 'post' ,
177
- async configResolved ( resolvedConfig ) {
178
- config = resolvedConfig ;
127
+ async configResolved ( viteConfig ) {
128
+ buildConfig = {
129
+ base : viteConfig . base ,
130
+ root : viteConfig . root ,
131
+ mode : viteConfig . mode ,
132
+ resolve : viteConfig . resolve ,
133
+ define : {
134
+ ...viteConfig . define ,
135
+ 'process.env.NODE_ENV' : JSON . stringify ( viteConfig . mode ) ,
136
+ } ,
137
+ build : {
138
+ minify : viteConfig . build . minify ,
139
+ outDir : viteConfig . build . outDir ,
140
+ sourcemap : viteConfig . command === 'serve' || viteConfig . build . sourcemap ,
141
+ emptyOutDir : false ,
142
+ modulePreload : false ,
143
+ rollupOptions : {
144
+ input : {
145
+ sw : settings . clientServiceWorkerSource
146
+ } ,
147
+ output : {
148
+ exports : 'none' ,
149
+ entryFileNames : 'sw.js' ,
150
+ inlineDynamicImports : true ,
151
+ } ,
152
+ } ,
153
+ } ,
154
+ } ;
179
155
} ,
180
156
async buildStart ( ) {
181
157
if ( devMode ) {
182
- const { output } = await build ( 'generate' ) ;
183
- swObj . code = output [ 0 ] . code ;
184
- swObj . map = output [ 0 ] . map ;
185
- }
186
- } ,
187
- async load ( id ) {
188
- if ( id . endsWith ( 'sw.js' ) ) {
189
- return '' ;
190
- }
191
- } ,
192
- async transform ( _code , id ) {
193
- if ( id . endsWith ( 'sw.js' ) ) {
194
- return swObj ;
158
+ await build ( buildConfig ) ;
195
159
}
196
160
} ,
197
161
async closeBundle ( ) {
198
162
if ( ! devMode ) {
199
- await build ( 'write' , [ injectManifestToSWPlugin ( ) , brotli ( ) ] ) ;
163
+ await build ( {
164
+ ...buildConfig ,
165
+ plugins : [ injectManifestToSWPlugin ( ) , brotli ( ) ]
166
+ } ) ;
200
167
}
201
- }
168
+ } ,
202
169
} ;
203
170
}
204
171
0 commit comments