-
Notifications
You must be signed in to change notification settings - Fork 164
/
vite.generated.ts
243 lines (218 loc) · 7.58 KB
/
vite.generated.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* NOTICE: this is an auto-generated file
*
* This file has been generated by the `flow:prepare-frontend` maven goal.
* This file will be overwritten on every run. Any custom changes should be made to vite.config.ts
*/
import path from 'path';
import * as net from 'net';
import { processThemeResources } from '@vaadin/application-theme-plugin/theme-handle.js';
import settings from '#settingsImport#';
import { UserConfigFn, defineConfig, HtmlTagDescriptor, mergeConfig, PluginOption } from 'vite';
import { injectManifest } from 'workbox-build';
import brotli from 'rollup-plugin-brotli';
import checker from 'vite-plugin-checker';
const frontendFolder = path.resolve(__dirname, settings.frontendFolder);
const themeFolder = path.resolve(frontendFolder, settings.themeFolder);
const frontendBundleFolder = path.resolve(__dirname, settings.frontendBundleOutput);
const addonFrontendFolder = path.resolve(__dirname, settings.addonFrontendFolder);
const themeResourceFolder = path.resolve(__dirname, settings.themeResourceFolder);
const projectStaticAssetsFolders = [
path.resolve(__dirname, 'src', 'main', 'resources', 'META-INF', 'resources'),
path.resolve(__dirname, 'src', 'main', 'resources', 'static'),
frontendFolder
];
// Folders in the project which can contain application themes
const themeProjectFolders = projectStaticAssetsFolders.map((folder) => path.resolve(folder, settings.themeFolder));
const themeOptions = {
devMode: false,
// The following matches folder 'target/flow-frontend/themes/'
// (not 'frontend/themes') for theme in JAR that is copied there
themeResourceFolder: path.resolve(themeResourceFolder, settings.themeFolder),
themeProjectFolders: themeProjectFolders,
projectStaticAssetsOutputFolder: path.resolve(__dirname, settings.staticOutput),
frontendGeneratedFolder: path.resolve(frontendFolder, settings.generatedFolder)
};
// Block debug and trace logs.
console.trace = () => {};
console.debug = () => {};
function themePlugin(): PluginOption {
return {
name: 'vaadin:theme',
config() {
processThemeResources(themeOptions, console);
},
handleHotUpdate(context) {
const contextPath = path.resolve(context.file);
const themePath = path.resolve(themeFolder);
if (contextPath.startsWith(themePath)) {
const changed = path.relative(themePath, contextPath);
console.debug('Theme file changed', changed);
if (changed.startsWith(settings.themeName)) {
processThemeResources(themeOptions, console);
}
}
},
async resolveId(id) {
if (!id.startsWith(settings.themeFolder)) {
return;
}
for (const location of [themeResourceFolder, frontendFolder]) {
const result = await this.resolve(path.resolve(location, id));
if (result) {
return result;
}
}
},
}
}
function runWatchDog(watchDogPort) {
const client = net.Socket();
client.setEncoding('utf8');
client.on('error', function () {
console.log('Watchdog connection error. Terminating vite process...');
client.destroy();
process.exit(0);
});
client.on('close', function () {
client.destroy();
runWatchDog(watchDogPort);
});
client.connect(watchDogPort, 'localhost');
}
let spaMiddlewareForceRemoved = false;
const allowedFrontendFolders = [
frontendFolder,
addonFrontendFolder,
path.resolve(addonFrontendFolder, '..', 'frontend'), // Contains only generated-flow-imports
path.resolve(frontendFolder, '../node_modules')
];
export const vaadinConfig: UserConfigFn = (env) => {
const devMode = env.mode === 'development';
const basePath = env.mode === 'production' ? '' : '/VAADIN/';
let pwaConfig;
if (devMode && process.env.watchDogPort) {
// Open a connection with the Java dev-mode handler in order to finish
// vite when it exits or crashes.
runWatchDog(process.env.watchDogPort);
}
return {
root: 'frontend',
base: basePath,
resolve: {
alias: {
Frontend: frontendFolder
}
},
define: {
// should be settings.offlinePath after manifests are fixed
OFFLINE_PATH: "'.'"
},
server: {
host: '127.0.0.1',
fs: {
allow: allowedFrontendFolders,
}
},
build: {
outDir: frontendBundleFolder,
assetsDir: 'VAADIN/build',
rollupOptions: {
input: {
indexhtml: path.resolve(frontendFolder, 'index.html')
}
}
},
plugins: [
!devMode && brotli(),
settings.pwaEnabled &&
{
name: 'pwa',
enforce: 'post',
apply: 'build',
async configResolved(config) {
pwaConfig = config;
},
async buildStart() {
// Before inject manifest we need to resolve and transpile the sw.ts file
// This could probably be made another way which needs to be investigated
// eslint-disable-next-line @typescript-eslint/no-var-requires
const rollup = require('rollup') as typeof Rollup
const includedPluginNames = [
'alias',
'vite:resolve',
'vite:esbuild',
'replace',
'vite:define',
'rollup-plugin-dynamic-import-variables',
'vite:esbuild-transpile',
'vite:terser',
]
const plugins = pwaConfig.plugins.filter(p => includedPluginNames.includes(p.name)) as Plugin[]
const bundle = await rollup.rollup({
input: path.resolve(settings.clientServiceWorkerSource),
plugins,
})
try {
await bundle.write({
format: 'es',
exports: 'none',
inlineDynamicImports: true,
file: path.resolve(frontendBundleFolder, 'sw.js'),
})
}
finally {
await bundle.close()
}
// end of resolve and transpilation
await injectManifest({
swSrc: path.resolve(frontendBundleFolder, 'sw.js'),
swDest: path.resolve(frontendBundleFolder, 'sw.js'),
globDirectory: frontendBundleFolder,
injectionPoint: 'self.__WB_MANIFEST',
});
}
},
themePlugin(),
{
name: 'inject-entrypoint-script',
transformIndexHtml: {
enforce: 'pre',
transform(_html, context) {
if (context.server && !spaMiddlewareForceRemoved) {
context.server.middlewares.stack = context.server.middlewares.stack.filter((mw) => {
const handleName = '' + mw.handle;
return !handleName.includes('viteSpaFallbackMiddleware');
});
spaMiddlewareForceRemoved = true;
}
if (context.path !== '/index.html') {
return;
}
const vaadinScript: HtmlTagDescriptor = {
tag: 'script',
attrs: { type: 'module', src: devMode ? '/VAADIN/generated/vaadin.ts' : './generated/vaadin.ts' },
injectTo: 'head'
};
let scripts = [vaadinScript];
if (devMode) {
const viteDevModeScript: HtmlTagDescriptor = {
tag: 'script',
attrs: { type: 'module', src: '/VAADIN/generated/vite-devmode.ts' },
injectTo: 'head'
};
scripts.push(viteDevModeScript);
}
return scripts;
}
}
},
checker({
typescript: true
})
]
};
};
export const overrideVaadinConfig = (customConfig: UserConfigFn) => {
return defineConfig((env) => mergeConfig(vaadinConfig(env), customConfig(env)));
};