run two vite builds for the sdk build, assets & js separately

This commit is contained in:
Bruno Windels 2021-12-20 19:17:31 +01:00
parent 6add3f1da3
commit c921091957
10 changed files with 94 additions and 107 deletions

View File

@ -0,0 +1,11 @@
{
"name": "hydrogen-sdk",
"version": "0.0.1",
"main": "./hydrogen.cjs.js",
"exports": {
"import": "./hydrogen.es.js",
"require": "./hydrogen.cjs.js"
},
"files": [],
"types": "types/lib.d.ts"
}

View File

@ -1,3 +1,4 @@
yarn run vite build -c vite.sdk-config.js
yarn run vite build -c vite.sdk-assets-config.js
yarn run vite build -c vite.sdk-lib-config.js
yarn tsc -p tsconfig-declaration.json
./scripts/sdk/create-manifest.js ./target/package.json

View File

@ -1,21 +1,15 @@
#!/usr/bin/env node
const fs = require("fs");
const baseManifest = JSON.parse(fs.readFileSync("package.json", "utf8"));
const appManifest = require("../../package.json")
const baseSDKManifest = require("./base-manifest.json")
const mergeOptions = require('merge-options');
const manifestExtension = {
name: "hydrogen-sdk",
main: "./hydrogen.cjs.js",
exports: {
import: "./hydrogen.es.js",
require: "./hydrogen.cjs.js"
},
files: [],
types: "types/lib.d.ts",
devDependencies: undefined,
scripts: undefined,
};
const manifest = mergeOptions(baseManifest, manifestExtension);
const manifest = mergeOptions(appManifest, baseSDKManifest, manifestExtension);
const json = JSON.stringify(manifest, undefined, 2);
const outFile = process.argv[2];
fs.writeFileSync(outFile, json, {encoding: "utf8"});

View File

@ -1,16 +1,13 @@
<!DOCTYPE html>
<!-- this file contains all references to include in the SDK asset build (using vite.sdk-assets-config.js) -->
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="./platform/web/ui/css/main.css">
<link rel="stylesheet" type="text/css" href="./platform/web/ui/css/themes/element/theme.css">
</head>
<body>
<script type="module">
export * from "./lib.ts";
import downloadSandboxPath from "./platform/web/assets/download-sandbox.html?url";
import workerPath from "./platform/web/worker/main.js?url";
import "./platform/web/sdk/paths/vite.ts";
</script>
</body>
</html>

View File

@ -31,12 +31,3 @@ export {RoomViewModel} from "./domain/session/room/RoomViewModel.js";
export {RoomView} from "./platform/web/ui/session/room/RoomView.js";
export {TimelineViewModel} from "./domain/session/room/timeline/TimelineViewModel.js";
export {TimelineView} from "./platform/web/ui/session/room/TimelineView";
// @ts-ignore
export * from "./platform/web/ui/css/main.css";
// @ts-ignore
export * from "./platform/web/ui/css/themes/element/theme.css";
// @ts-ignore
import _downloadSandboxPath from "./platform/web/assets/download-sandbox.html?url";
// @ts-ignore
import _workerPath from "./platform/web/worker/main.js?url";

View File

@ -7,19 +7,10 @@ const version = manifest.version;
const commonOptions = {
logLevel: "info",
public: false,
publicDir: 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,
assetsInlineLimit: 0,

View File

@ -9,6 +9,15 @@ export default defineConfig(({mode}) => {
return mergeOptions(commonOptions, {
root: "src/platform/web",
base: "./",
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",
minify: true,

11
vite.sdk-assets-config.js Normal file
View File

@ -0,0 +1,11 @@
const path = require("path");
const mergeOptions = require('merge-options');
const commonOptions = require("./vite.common-config.js");
export default mergeOptions(commonOptions, {
root: "src/",
base: "./",
build: {
outDir: "../target/asset-build/",
},
});

View File

@ -1,72 +0,0 @@
const path = require("path");
const mergeOptions = require('merge-options').bind({concatArrays: true});
const commonOptions = require("./vite.common-config.js");
const manifest = require("./package.json");
const srcDir = path.join(__dirname, "src/");
const modulesDir = path.join(srcDir, "node_modules/");
const mocksDir = path.join(srcDir, "mocks/");
const fixturesDir = path.join(srcDir, "fixtures/");
const commonOutput = {
// manualChunks: (id) => {
// if (id.endsWith("/lib.ts")) {
// console.log(id, arguments);
// return "es/lib";
// }
// if (id.startsWith(srcDir)) {
// const idPath = id.substring(srcDir.length);
// const pathWithoutExt = idPath.substring(0, idPath.lastIndexOf("."));
// return pathWithoutExt;
// } else {
// return "index";
// }
// },
chunkFileNames: `[format]/[name].js`,
assetFileNames: `assets/[name][extname]`,
// important to preserve export names of every module
// so we can still override the file and provider alternative impls
minifyInternalExports: false,
preferConst: true,
};
const externalDependencies = Object.keys(manifest.dependencies).concat(Object.keys(manifest.devDependencies)).filter(d => d !== "@matrix-org/olm").map(d => path.join(__dirname, "node_modules", d));
console.log("external", externalDependencies);
export default mergeOptions(commonOptions, {
root: "src/",
plugins: [
{
name: "showconfig",
buildStart(rollupOptions) {
console.dir(rollupOptions, {depth: 100});
},
resolveId(source, importer) {
console.log(source, importer);
}
}
],
build: {
minify: false,
sourcemap: false,
outDir: "../target",
rollupOptions: {
input: "./src/lib.ts",
treeshake: false,
external: (id, parentId) => {
const resolveId = (id.startsWith("./") || id.startsWith("../")) ? path.join(path.dirname(parentId), id) : id;
const external = externalDependencies.some(d => resolveId.startsWith(d));
if (external) {
console.log("external", resolveId);
}
return external;
//return !resolveId.startsWith(srcDir);// || resolveId.startsWith(mocksDir) || resolveId.startsWith(fixturesDir);
},
preserveEntrySignatures: "strict",
output: [
Object.assign({}, commonOutput, {format: "es"}),
Object.assign({}, commonOutput, {format: "cjs"}),
]
}
},
});

54
vite.sdk-lib-config.js Normal file
View File

@ -0,0 +1,54 @@
const path = require("path");
const mergeOptions = require('merge-options');
const commonOptions = require("./vite.common-config.js");
const manifest = require("./package.json")
// const srcDir = path.join(__dirname, "src/");
// const modulesDir = path.join(srcDir, "node_modules/");
// const mocksDir = path.join(srcDir, "mocks/");
// const fixturesDir = path.join(srcDir, "fixtures/");
const externalDependencies = Object.keys(manifest.dependencies)
.concat(Object.keys(manifest.devDependencies))
.map(d => path.join(__dirname, "node_modules", d));
export default mergeOptions(commonOptions, {
root: "src/",
build: {
lib: {
entry: path.resolve(__dirname, 'src/lib.ts'),
formats: ["cjs", "es"],
fileName: format => `hydrogen.${format}.js`,
},
minify: false,
sourcemap: false,
outDir: "../target/lib-build",
// don't bundle any dependencies, they should be imported/required
rollupOptions: {
external(id, parentId) {
const isRelativePath = id.startsWith("./") || id.startsWith("../");
const isModuleIdentifier = !isRelativePath && !id.startsWith("/");
const resolveId = isRelativePath ? path.join(path.dirname(parentId), id) : id;
const external = isModuleIdentifier ||
externalDependencies.some(d => resolveId.startsWith(d));
// resolveId.startsWith(fixturesDir) ||
// resolveId.startsWith(mocksDir);
return external;
},
/* don't bundle, so we can override imports per file at build time to replace components */
// output: {
// manualChunks: (id) => {
// if (id.startsWith(srcDir)) {
// const idPath = id.substring(srcDir.length);
// const pathWithoutExt = idPath.substring(0, idPath.lastIndexOf("."));
// return pathWithoutExt;
// } else {
// return "index";
// }
// },
// minifyInternalExports: false,
// chunkFileNames: "[format]/[name].js"
// }
}
},
});