-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.generated.ts
171 lines (151 loc) · 5.32 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
/**
* 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 './target/vaadin-dev-server-settings.json';
import { UserConfigFn, defineConfig, HtmlTagDescriptor, mergeConfig } from 'vite';
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 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(__dirname, settings.themeResourceFolder),
themeProjectFolders: themeProjectFolders,
projectStaticAssetsOutputFolder: path.resolve(__dirname, settings.staticOutput),
frontendGeneratedFolder: path.resolve(frontendFolder, settings.generatedFolder)
};
// Block debug and trace logs.
console.trace = () => {};
console.debug = () => {};
function updateTheme(contextPath: string) {
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);
}
}
}
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/';
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: {
themes: themeFolder,
Frontend: frontendFolder
}
},
server: {
fs: {
allow: allowedFrontendFolders,
}
},
build: {
outDir: frontendBundleFolder,
assetsDir: 'VAADIN/build',
rollupOptions: {
input: {
indexhtml: path.resolve(frontendFolder, 'index.html')
}
}
},
plugins: [
!devMode && brotli(),
{
name: 'custom-theme',
config() {
processThemeResources(themeOptions, console);
},
handleHotUpdate(context) {
updateTheme(path.resolve(context.file));
}
},
{
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)));
};