Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Split Vite configuration into a generated part and a customizabl…
- Loading branch information
Showing
5 changed files
with
150 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,9 @@ | ||
import { defineConfig } from 'vite'; | ||
import path from 'path'; | ||
import { UserConfigFn } from 'vite'; | ||
import { overrideVaadinConfig } from './vite.generated'; | ||
|
||
import { processThemeResources } from '@vaadin/application-theme-plugin/theme-handle.js'; | ||
import settings from './target/vaadin-dev-server-settings.json'; | ||
const customConfig: UserConfigFn = (env) => ({ | ||
// Here you can add custom Vite parameters | ||
// https://vitejs.dev/config/ | ||
}); | ||
|
||
const frontendFolder = path.resolve(__dirname, settings.frontendFolder); | ||
const themeFolder = path.resolve(frontendFolder, settings.themeFolder); | ||
const buildFolder = path.resolve(__dirname, settings.frontendBundleOutput); | ||
|
||
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); | ||
} | ||
} | ||
} | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig(({ command, mode }) => ({ | ||
root: 'frontend', | ||
base: mode === 'production' ? '' : '/VAADIN/', | ||
resolve: { | ||
alias: { | ||
themes: themeFolder, | ||
Frontend: frontendFolder | ||
}, | ||
}, | ||
build: { | ||
outDir: buildFolder, | ||
assetsDir: 'VAADIN/build', | ||
rollupOptions: { | ||
input: { | ||
main: path.resolve(frontendFolder, 'index.html'), | ||
generated: path.resolve(frontendFolder, 'generated/vaadin.ts') | ||
}, | ||
output: { | ||
// Produce only one chunk that gets imported into index.html | ||
manualChunks: () => 'everything.js' | ||
}, | ||
}, | ||
}, | ||
plugins: [ | ||
{ | ||
name: 'custom-theme', | ||
config() { | ||
processThemeResources(themeOptions, console); | ||
}, | ||
handleHotUpdate(context) { | ||
updateTheme(path.resolve(context.file)); | ||
} | ||
} | ||
] | ||
})); | ||
export default overrideVaadinConfig(customConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* 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 { processThemeResources } from '@vaadin/application-theme-plugin/theme-handle.js'; | ||
import settings from '#settingsImport#'; | ||
import { UserConfigFn } from 'vite'; | ||
import { defineConfig } from 'vite'; | ||
|
||
const frontendFolder = path.resolve(__dirname, settings.frontendFolder); | ||
const themeFolder = path.resolve(frontendFolder, settings.themeFolder); | ||
const buildFolder = path.resolve(__dirname, settings.frontendBundleOutput); | ||
|
||
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); | ||
} | ||
} | ||
} | ||
|
||
export const vaadinConfig:UserConfigFn = (env) => ({ | ||
root: 'frontend', | ||
base: env.mode === 'production' ? '' : '/VAADIN/', | ||
resolve: { | ||
alias: { | ||
themes: themeFolder, | ||
Frontend: frontendFolder | ||
}, | ||
}, | ||
build: { | ||
outDir: buildFolder, | ||
assetsDir: 'VAADIN/build', | ||
rollupOptions: { | ||
input: { | ||
main: path.resolve(frontendFolder, 'index.html'), | ||
generated: path.resolve(frontendFolder, 'generated/vaadin.ts') | ||
}, | ||
output: { | ||
// Produce only one chunk that gets imported into index.html | ||
manualChunks: () => 'everything.js' | ||
}, | ||
}, | ||
}, | ||
plugins: [ | ||
{ | ||
name: 'custom-theme', | ||
config() { | ||
processThemeResources(themeOptions, console); | ||
}, | ||
handleHotUpdate(context) { | ||
updateTheme(path.resolve(context.file)); | ||
} | ||
} | ||
] | ||
}); | ||
|
||
|
||
export const overrideVaadinConfig = (customConfig:UserConfigFn) => { | ||
return defineConfig((env) => ({ | ||
...vaadinConfig(env), | ||
...customConfig(env) | ||
})); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters