hydrogen-web/vite.config.js

70 lines
2.6 KiB
JavaScript
Raw Normal View History

const injectWebManifest = require("./scripts/build-plugins/manifest");
const {injectServiceWorker, createPlaceholderValues} = require("./scripts/build-plugins/service-worker");
2022-04-11 16:25:28 +05:30
const themeBuilder = require("./scripts/build-plugins/rollup-plugin-build-themes");
const {defineConfig} = require('vite');
const mergeOptions = require('merge-options').bind({concatArrays: true});
2022-04-11 16:25:28 +05:30
const {commonOptions, compiledVariables} = require("./vite.common-config.js");
export default defineConfig(({mode}) => {
const definePlaceholders = createPlaceholderValues(mode);
return mergeOptions(commonOptions, {
root: "src/platform/web",
base: "./",
build: {
outDir: "../../../target",
minify: true,
sourcemap: true,
2022-04-21 12:52:42 +05:30
rollupOptions: {
output: {
2022-05-12 16:02:03 +05:30
assetFileNames: (asset) => {
if (asset.name.includes("config.json")) {
return "[name][extname]";
}
else if (asset.name.match(/theme-.+\.json/)) {
return "assets/[name][extname]";
}
else {
return "assets/[name].[hash][extname]";
}
}
2022-04-21 12:52:42 +05:30
},
},
},
plugins: [
2022-04-13 14:24:50 +05:30
themeBuilder({
themeConfig: {
themes: {
element: "./src/platform/web/ui/css/themes/element",
},
2022-04-13 14:24:50 +05:30
default: "element",
},
compiledVariables,
2022-04-13 14:24:50 +05:30
}),
// important this comes before service worker
// otherwise the manifest and the icons it refers to won't be cached
injectWebManifest("assets/manifest.json"),
injectServiceWorker("./src/platform/web/sw.js", findUnhashedFileNamesFromBundle, {
// placeholders to replace at end of build by chunk name
index: {
DEFINE_GLOBAL_HASH: definePlaceholders.DEFINE_GLOBAL_HASH,
},
sw: definePlaceholders,
}),
],
define: definePlaceholders,
});
});
function findUnhashedFileNamesFromBundle(bundle) {
const names = ["index.html"];
for (const fileName of Object.keys(bundle)) {
if (fileName.includes("config.json")) {
names.push(fileName);
}
if (/theme-.+\.json/.test(fileName)) {
names.push(fileName);
}
}
return names;
}