From c11f0774eba98f3bc47fa0bb3189cac1c534fb3d Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 10 Dec 2021 16:03:17 +0100 Subject: [PATCH] move common parts of build config to separate file and merge with it --- package.json | 1 + vite.common-config.js | 50 +++++++++++++++++++++++++++++++++++++++ vite.config.js | 55 ++++--------------------------------------- yarn.lock | 12 ++++++++++ 4 files changed, 68 insertions(+), 50 deletions(-) create mode 100644 vite.common-config.js diff --git a/package.json b/package.json index 46faf089..59c10352 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "fake-indexeddb": "^3.1.2", "impunity": "^1.0.9", "mdn-polyfills": "^5.20.0", + "merge-options": "^3.0.4", "node-html-parser": "^4.0.0", "postcss-css-variables": "^0.18.0", "postcss-flexbugs-fixes": "^5.0.2", diff --git a/vite.common-config.js b/vite.common-config.js new file mode 100644 index 00000000..b120ee59 --- /dev/null +++ b/vite.common-config.js @@ -0,0 +1,50 @@ +const cssvariables = require("postcss-css-variables"); +const flexbugsFixes = require("postcss-flexbugs-fixes"); +const fs = require("fs"); +const path = require("path"); +const version = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8")).version; + +const commonOptions = { + logLevel: "warn", + public: false, + server: { + hmr: false + }, + resolve: { + alias: { + // these should only be imported by the base-x package in any runtime code + // and works in the browser with a Uint8Array shim, + // rather than including a ton of polyfill code + "safe-buffer": "./scripts/package-overrides/safe-buffer/index.js", + "buffer": "./scripts/package-overrides/buffer/index.js", + } + }, + build: { + emptyOutDir: true, + minify: true, + sourcemap: true, + assetsInlineLimit: 0, + polyfillModulePreload: false, + }, + define: { + DEFINE_VERSION: JSON.stringify(version), + DEFINE_GLOBAL_HASH: JSON.stringify(null), + }, + css: { + postcss: { + plugins: [ + cssvariables({ + preserve: (declaration) => { + return declaration.value.indexOf("var(--ios-") == 0; + } + }), + // the grid option creates some source fragment that causes the vite warning reporter to crash because + // it wants to log a warning on a line that does not exist in the source fragment. + // autoprefixer({overrideBrowserslist: ["IE 11"], grid: "no-autoplace"}), + flexbugsFixes() + ] + } + } +}; + +module.exports = commonOptions; diff --git a/vite.config.js b/vite.config.js index 1562269e..f3f43be2 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,39 +1,16 @@ -const cssvariables = require("postcss-css-variables"); -const flexbugsFixes = require("postcss-flexbugs-fixes"); - -const fs = require("fs"); -const path = require("path"); - const injectWebManifest = require("./scripts/build-plugins/manifest"); const {injectServiceWorker, createPlaceholderValues} = require("./scripts/build-plugins/service-worker"); const {defineConfig} = require('vite'); -const version = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8")).version; +const mergeOptions = require('merge-options').bind({concatArrays: true}); +const commonOptions = require("./vite.common-config.js"); export default defineConfig(({mode}) => { const definePlaceholders = createPlaceholderValues(mode); - return { - public: false, + return mergeOptions(commonOptions, { root: "src/platform/web", base: "./", - server: { - hmr: false - }, - resolve: { - alias: { - // these should only be imported by the base-x package in any runtime code - // and works in the browser with a Uint8Array shim, - // rather than including a ton of polyfill code - "safe-buffer": "./scripts/package-overrides/safe-buffer/index.js", - "buffer": "./scripts/package-overrides/buffer/index.js", - } - }, build: { outDir: "../../../target", - emptyOutDir: true, - minify: true, - sourcemap: true, - assetsInlineLimit: 0, - polyfillModulePreload: false, }, plugins: [ // important this comes before service worker @@ -45,28 +22,6 @@ export default defineConfig(({mode}) => { "sw": definePlaceholders }), ], - define: { - DEFINE_VERSION: JSON.stringify(version), - ...definePlaceholders - }, - css: { - postcss: { - plugins: [ - cssvariables({ - preserve: (declaration) => { - return declaration.value.indexOf("var(--ios-") == 0; - } - }), - // the grid option creates some source fragment that causes the vite warning reporter to crash because - // it wants to log a warning on a line that does not exist in the source fragment. - // autoprefixer({overrideBrowserslist: ["IE 11"], grid: "no-autoplace"}), - flexbugsFixes() - ] - } - } - }; + define: definePlaceholders, + }); }); - -function scriptTagPath(htmlFile, index) { - return `${htmlFile}?html-proxy&index=${index}.js`; -} diff --git a/yarn.lock b/yarn.lock index 87d39fac..c564dbe5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -963,6 +963,11 @@ is-number@^7.0.0: resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng== +is-plain-obj@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287" + integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA== + isexe@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" @@ -1036,6 +1041,13 @@ mdn-polyfills@^5.20.0: resolved "https://registry.yarnpkg.com/mdn-polyfills/-/mdn-polyfills-5.20.0.tgz#ca8247edf20a4f60dec6804372229812b348260b" integrity sha512-AbTv1ytcoOUAkxw6u5oo2QPf27kEZgxBAQr49jFb4i2VnTnFGfJbcIQ9UDBOdfNECeXsgkYFwB2BkdeTfOzztw== +merge-options@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/merge-options/-/merge-options-3.0.4.tgz#84709c2aa2a4b24c1981f66c179fe5565cc6dbb7" + integrity sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ== + dependencies: + is-plain-obj "^2.1.0" + merge2@^1.3.0: version "1.4.1" resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"