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 @@ + + +
+ +