diff --git a/package.json b/package.json index 59c10352..b6299896 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "hydrogen-web", "version": "0.2.22", "description": "A javascript matrix client prototype, trying to minize RAM usage by offloading as much as possible to IndexedDB", - "main": "src/lib.ts", "directories": { "doc": "doc" }, @@ -12,7 +11,8 @@ "lint-ci": "eslint src/", "test": "impunity --entry-point src/platform/web/main.js src/platform/web/Platform.js --force-esm-dirs lib/ src/ --root-dir src/", "start": "vite --port 3000", - "build": "vite build" + "build": "vite build", + "build:sdk": "./scripts/sdk/build.sh" }, "repository": { "type": "git", diff --git a/scripts/build-plugins/service-worker.js b/scripts/build-plugins/service-worker.js index 19ae793f..805f6000 100644 --- a/scripts/build-plugins/service-worker.js +++ b/scripts/build-plugins/service-worker.js @@ -12,6 +12,7 @@ function injectServiceWorker(swFile, otherUnhashedFiles, placeholdersPerChunk) { const swName = path.basename(swFile); let root; let version; + let logger; return { name: "hydrogen:injectServiceWorker", @@ -27,6 +28,7 @@ function injectServiceWorker(swFile, otherUnhashedFiles, placeholdersPerChunk) { configResolved: config => { root = config.root; version = JSON.parse(config.define.DEFINE_VERSION); // unquote + logger = config.logger; }, generateBundle: async function(options, bundle) { const unhashedFilenames = [swName].concat(otherUnhashedFiles); @@ -46,7 +48,7 @@ function injectServiceWorker(swFile, otherUnhashedFiles, placeholdersPerChunk) { ...getCacheFileNamePlaceholderValues(swName, unhashedFilenames, assets, placeholdersPerChunk) }; replacePlaceholdersInChunks(assets, placeholdersPerChunk, placeholderValues); - console.log(`\nBuilt ${version} (${globalHash})`); + logger.info(`\nBuilt ${version} (${globalHash})`); } }; } diff --git a/scripts/sdk/build.sh b/scripts/sdk/build.sh new file mode 100755 index 00000000..71c98438 --- /dev/null +++ b/scripts/sdk/build.sh @@ -0,0 +1,3 @@ +yarn run vite build -c vite.sdk-config.js +yarn tsc -p tsconfig-declaration.json +./scripts/sdk/create-manifest.js ./target/package.json diff --git a/scripts/sdk/create-manifest.js b/scripts/sdk/create-manifest.js new file mode 100755 index 00000000..93074c55 --- /dev/null +++ b/scripts/sdk/create-manifest.js @@ -0,0 +1,21 @@ +#!/usr/bin/env node +const fs = require("fs"); +const baseManifest = JSON.parse(fs.readFileSync("package.json", "utf8")); +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 json = JSON.stringify(manifest, undefined, 2); +const outFile = process.argv[2]; +fs.writeFileSync(outFile, json, {encoding: "utf8"}); diff --git a/sdk-vite.config.js b/sdk-vite.config.js deleted file mode 100644 index b95470f5..00000000 --- a/sdk-vite.config.js +++ /dev/null @@ -1,43 +0,0 @@ -const path = require("path"); -const mergeOptions = require('merge-options').bind({concatArrays: true}); -const commonOptions = require("./vite.common-config.js"); - -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/"); - - -export default mergeOptions(commonOptions, { - root: "src/", - build: { - outDir: "../target", - lib: { - entry: "lib.ts", - fileName: "hydrogen", - formats: ["cjs", "es"] - }, - rollupOptions: { - 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: { - manualChunks: (id) => { - 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`, - // important to preserve export names of every module - // so we can still override the file and provider alternative impls - minifyInternalExports: false, - preferConst: true, - } - } - }, -}); diff --git a/src/foo-index.html b/src/foo-index.html new file mode 100644 index 00000000..c6bfa098 --- /dev/null +++ b/src/foo-index.html @@ -0,0 +1,16 @@ + + + + + + + + + + + + diff --git a/src/lib.ts b/src/lib.ts index a4a9f4c5..6a5f6581 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -32,5 +32,11 @@ 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"; diff --git a/tsconfig-declaration.json b/tsconfig-declaration.json index 8ba13448..472e1698 100644 --- a/tsconfig-declaration.json +++ b/tsconfig-declaration.json @@ -4,7 +4,7 @@ "noEmit": false, "emitDeclarationOnly": true, "declaration": true, - "outDir": "dist/types", + "outDir": "target/types", "rootDir": "src" }, "exclude": [ diff --git a/vite.common-config.js b/vite.common-config.js index b120ee59..39b2845b 100644 --- a/vite.common-config.js +++ b/vite.common-config.js @@ -5,7 +5,7 @@ const path = require("path"); const version = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8")).version; const commonOptions = { - logLevel: "warn", + logLevel: "info", public: false, server: { hmr: false @@ -21,8 +21,6 @@ const commonOptions = { }, build: { emptyOutDir: true, - minify: true, - sourcemap: true, assetsInlineLimit: 0, polyfillModulePreload: false, }, diff --git a/vite.config.js b/vite.config.js index f3f43be2..b6ec597d 100644 --- a/vite.config.js +++ b/vite.config.js @@ -11,6 +11,8 @@ export default defineConfig(({mode}) => { base: "./", build: { outDir: "../../../target", + minify: true, + sourcemap: true, }, plugins: [ // important this comes before service worker diff --git a/vite.sdk-config.js b/vite.sdk-config.js new file mode 100644 index 00000000..afeabbaa --- /dev/null +++ b/vite.sdk-config.js @@ -0,0 +1,63 @@ +const path = require("path"); +const mergeOptions = require('merge-options').bind({concatArrays: true}); +const commonOptions = require("./vite.common-config.js"); + +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, +}; + +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; + return !resolveId.startsWith(srcDir) || resolveId.startsWith(mocksDir) || resolveId.startsWith(fixturesDir); + }, + preserveEntrySignatures: "strict", + output: [ + Object.assign({}, commonOutput, {format: "es"}), + Object.assign({}, commonOutput, {format: "cjs"}), + ] + } + }, +});