Update to Hilla 24

This commit is contained in:
grimsi
2024-08-22 10:55:22 +02:00
parent 042c326380
commit cb073c6bcf
52 changed files with 5944 additions and 4078 deletions
+36 -22
View File
@@ -11,6 +11,7 @@ import * as net from 'net';
import { processThemeResources } from './build/plugins/application-theme-plugin/theme-handle.js';
import { rewriteCssUrls } from './build/plugins/theme-loader/theme-loader-utils.js';
import { addFunctionComponentSourceLocationBabel } from './build/plugins/react-function-location-plugin/react-function-location-plugin.js';
import settings from './build/vaadin-dev-server-settings.json';
import {
AssetInfo,
@@ -33,6 +34,9 @@ import postcssLit from './build/plugins/rollup-plugin-postcss-lit-custom/rollup-
import { createRequire } from 'module';
import { visualizer } from 'rollup-plugin-visualizer';
import reactPlugin from '@vitejs/plugin-react';
import vitePluginFileSystemRouter from '@vaadin/hilla-file-router/vite-plugin.js';
// Make `require` compatible with ES modules
const require = createRequire(import.meta.url);
@@ -306,7 +310,7 @@ function statsExtracterPlugin(): PluginOption {
const isFrontendResourceCollected = (id: string) =>
!id.startsWith(themeOptions.frontendGeneratedFolder.replace(/\\/g, '/'))
|| isThemeComponentsResource(id)
|| isThemeComponentsResource(id)
|| isGeneratedWebComponentResource(id);
// collects project's frontend resources in frontend folder, excluding
@@ -343,7 +347,7 @@ function statsExtracterPlugin(): PluginOption {
const fileKey = line.substring(line.indexOf('jar-resources/') + 14);
frontendFiles[fileKey] = hash;
});
// collects and hash rest of the Frontend resources excluding files in /generated/ and /themes/
// collects and hash rest of the Frontend resources excluding files in /generated/ and /themes/
// and files already in frontendFiles.
let frontendFolderAlias = "Frontend";
generatedImports
@@ -358,7 +362,7 @@ function statsExtracterPlugin(): PluginOption {
const fileBuffer = readFileSync(filePath, { encoding: 'utf-8' }).replace(/\r\n/g, '\n');
frontendFiles[line] = createHash('sha256').update(fileBuffer, 'utf8').digest('hex');
}
});
});
// If a index.ts exists hash it to be able to see if it changes.
if (existsSync(path.resolve(frontendFolder, 'index.ts'))) {
const fileBuffer = readFileSync(path.resolve(frontendFolder, 'index.ts'), { encoding: 'utf-8' }).replace(
@@ -605,7 +609,6 @@ function themePlugin(opts): PluginOption {
if (!id.startsWith(settings.themeFolder)) {
return;
}
for (const location of [themeResourceFolder, frontendFolder]) {
const result = await this.resolve(path.resolve(location, id));
if (result) {
@@ -622,8 +625,9 @@ function themePlugin(opts): PluginOption {
) {
return;
}
const [themeName] = bareId.substring(themeFolder.length + 1).split('/');
return rewriteCssUrls(raw, path.dirname(bareId), path.resolve(themeFolder, themeName), console, opts);
const resourceThemeFolder = bareId.startsWith(themeFolder) ? themeFolder : themeOptions.themeResourceFolder;
const [themeName] = bareId.substring(resourceThemeFolder.length + 1).split('/');
return rewriteCssUrls(raw, path.dirname(bareId), path.resolve(resourceThemeFolder, themeName), console, opts);
}
};
}
@@ -644,8 +648,6 @@ function runWatchDog(watchDogPort, watchDogHost) {
client.connect(watchDogPort, watchDogHost || 'localhost');
}
let spaMiddlewareForceRemoved = false;
const allowedFrontendFolders = [frontendFolder, nodeModulesFolder];
function showRecompileReason(): PluginOption {
@@ -716,9 +718,11 @@ export const vaadinConfig: UserConfigFn = (env) => {
}
},
build: {
minify: productionMode,
outDir: buildOutputFolder,
emptyOutDir: devBundle,
assetsDir: 'VAADIN/build',
target: ["esnext", "safari15"],
rollupOptions: {
input: {
indexhtml: projectIndexHtml,
@@ -759,7 +763,7 @@ export const vaadinConfig: UserConfigFn = (env) => {
devMode && showRecompileReason(),
settings.offlineEnabled && buildSWPlugin({ devMode }),
!devMode && statsExtracterPlugin(),
devBundle && preserveUsageStats(),
!productionMode && preserveUsageStats(),
themePlugin({ devMode }),
postcssLit({
include: ['**/*.css', /.*\/.*\.css\?.*/],
@@ -771,20 +775,29 @@ export const vaadinConfig: UserConfigFn = (env) => {
new RegExp('.*/.*\\?html-proxy.*')
]
}),
// The React plugin provides fast refresh and debug source info
reactPlugin({
include: '**/*.tsx',
babel: {
// We need to use babel to provide the source information for it to be correct
// (otherwise Babel will slightly rewrite the source file and esbuild generate source info for the modified file)
presets: [['@babel/preset-react', { runtime: 'automatic', development: !productionMode }]],
// React writes the source location for where components are used, this writes for where they are defined
plugins: [
!productionMode && addFunctionComponentSourceLocationBabel()
].filter(Boolean)
}
}),
{
name: 'vaadin:force-remove-html-middleware',
transformIndexHtml: {
order: 'pre',
handler(_html, { server }) {
if (server && !spaMiddlewareForceRemoved) {
server.middlewares.stack = server.middlewares.stack.filter((mw) => {
const handleName = '' + mw.handle;
return !handleName.includes('viteHtmlFallbackMiddleware');
});
spaMiddlewareForceRemoved = true;
}
}
}
configureServer(server) {
return () => {
server.middlewares.stack = server.middlewares.stack.filter((mw) => {
const handleName = `${mw.handle}`;
return !handleName.includes('viteHtmlFallbackMiddleware');
});
};
},
},
hasExportedWebComponents && {
name: 'vaadin:inject-entrypoints-to-web-component-html',
@@ -819,7 +832,7 @@ export const vaadinConfig: UserConfigFn = (env) => {
if (devMode) {
scripts.push({
tag: 'script',
attrs: { type: 'module', src: `/generated/vite-devmode.ts` },
attrs: { type: 'module', src: `/generated/vite-devmode.ts`, onerror: "document.location.reload()" },
injectTo: 'head'
});
}
@@ -836,6 +849,7 @@ export const vaadinConfig: UserConfigFn = (env) => {
typescript: true
}),
productionMode && visualizer({ brotliSize: true, filename: bundleSizeFile })
, vitePluginFileSystemRouter({isDevMode: devMode})
]
};
};