basic sdk build config file for es and cjs

This commit is contained in:
Bruno Windels 2021-12-10 16:03:34 +01:00
parent c11f0774eb
commit df6000c706

View file

@ -1,53 +1,27 @@
const cssvariables = require("postcss-css-variables");
const flexbugsFixes = require("postcss-flexbugs-fixes");
const fs = require("fs");
const path = require("path"); const path = require("path");
const mergeOptions = require('merge-options').bind({concatArrays: true});
const injectWebManifest = require("./scripts/build-plugins/manifest"); const commonOptions = require("./vite.common-config.js");
const injectServiceWorker = require("./scripts/build-plugins/service-worker");
// const legacyBuild = require("./scripts/build-plugins/legacy-build");
// we could also just import {version} from "../../package.json" where needed,
// but this won't work in the service worker yet as it is not transformed yet
// TODO: we should emit a chunk early on and then transform the asset again once we know all the other assets to cache
const version = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8")).version;
const {defineConfig} = require("vite");
const srcDir = path.join(__dirname, "src/"); const srcDir = path.join(__dirname, "src/");
const modulesDir = path.join(srcDir, "node_modules/"); const modulesDir = path.join(srcDir, "node_modules/");
const mocksDir = path.join(srcDir, "mocks/"); const mocksDir = path.join(srcDir, "mocks/");
const fixturesDir = path.join(srcDir, "fixtures/"); const fixturesDir = path.join(srcDir, "fixtures/");
export default {
public: false, export default mergeOptions(commonOptions, {
root: "src/", root: "src/",
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: { build: {
outDir: "../target", outDir: "../target",
emptyOutDir: true,
minify: false,
sourcemap: false,
assetsInlineLimit: 0,
polyfillModulePreload: false,
lib: { lib: {
entry: "lib.ts", entry: "lib.ts",
name: "hydrogen", fileName: "hydrogen",
formats: ["cjs", "es"] formats: ["cjs", "es"]
}, },
rollupOptions: { rollupOptions: {
external: id => id.startsWith(modulesDir) || id.startsWith(mocksDir) || id.startsWith(fixturesDir), external: (id, parentId) => {
const resolveId = (id.startsWith("./") || id.startsWith("../")) ? path.join(path.dirname(parentId), id) : id;
return !resolveId.startsWith(srcDir) || resolveId.startsWith(mocksDir) || resolveId.startsWith(fixturesDir);
},
output: { output: {
manualChunks: (id) => { manualChunks: (id) => {
if (id.startsWith(srcDir)) { if (id.startsWith(srcDir)) {
@ -55,35 +29,15 @@ export default {
const pathWithoutExt = idPath.substring(0, idPath.lastIndexOf(".")); const pathWithoutExt = idPath.substring(0, idPath.lastIndexOf("."));
return pathWithoutExt; return pathWithoutExt;
} else { } else {
console.log("putting", id.substring(srcDir.length), "in index");
return "index"; return "index";
} }
}, },
chunkFileNames: `[format]/[name].js`, chunkFileNames: `[format]/[name].js`,
// preserveModules: true, // important to preserve export names of every module
// so we can still override the file and provider alternative impls
minifyInternalExports: false,
preferConst: true,
} }
} }
}, },
define: { });
"DEFINE_VERSION": JSON.stringify(version)
},
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()
]
}
}
};
function scriptTagPath(htmlFile, index) {
return `${htmlFile}?html-proxy&index=${index}.js`;
}