Skip to content

Commit

Permalink
Make the SDK friendly to locally link and develop on
Browse files Browse the repository at this point in the history
Fix #686
Fix #682

Instead of deleting the whole `target/` directory, leave it alone so the symlink
driving the `npm link`/`yarn link` stays in tact.

Leave Vite builds in their build directories (`/lib-build`/`/asset-build`)
so you can `vite build --watch` to build on local changes and still have a
consisent place to reference in the `package.json` `exports`. Previously,
everything relied on `build.sh` which does a bunch of moving and renaming
and made it hard to rebuild on changes.

Add back support for CommonJS (adding the `package.json` `exports`).

The last piece is making sure the `?url` imports (`import workerPath from 'hydrogen-view-sdk/main.js?url';`)
work still. It looks like this may have just been solved via
vitejs/vite#6725 -> vitejs/vite#7073
(literally 2 days ago) and we just need to wait for the next Vite release 🎉
  • Loading branch information
MadLittleMods committed Feb 26, 2022
1 parent 460780d commit cd007b4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
13 changes: 12 additions & 1 deletion scripts/sdk/base-manifest.json
Expand Up @@ -2,6 +2,17 @@
"name": "hydrogen-view-sdk",
"description": "Embeddable matrix client library, including view components",
"version": "0.0.5",
"main": "./hydrogen.es.js",
"main": "./hydrogen.cjs.js",
"exports": {
".": {
"import": "./lib-build/hydrogen.es.js",
"require": "./lib-build/hydrogen.cjs.js"
},
"./paths/vite": "./paths/vite.js",
"./style.css": "./asset-build/assets/index.css",
"./main.js": "./asset-build/assets/download-sandbox.html",
"./download-sandbox.html": "./asset-build/assets/download-sandbox.html",
"./assets/*": "./asset-build/assets/*"
},
"type": "module"
}
12 changes: 3 additions & 9 deletions scripts/sdk/build.sh
@@ -1,5 +1,5 @@
#!/bin/bash
rm -rf target
rm -rf target/*
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
Expand All @@ -10,13 +10,7 @@ mkdir target/paths
cp doc/SDK.md target/README.md
pushd target
pushd asset-build/assets
mv main.*.js ../../main.js
mv index.*.css ../../style.css
mv download-sandbox.*.html ../../download-sandbox.html
rm *.js *.wasm
mv ./* ../../
rm !(main).js *.wasm
popd
rm -rf asset-build
mv lib-build/* .
rm -rf lib-build
rm index.html
popd
16 changes: 1 addition & 15 deletions scripts/sdk/create-manifest.js
Expand Up @@ -3,21 +3,7 @@ const fs = require("fs");
const appManifest = require("../../package.json");
const baseSDKManifest = require("./base-manifest.json");
/*
need to leave exports out of base-manifest.json because of #vite-bug,
with the downside that we can't support environments that support
both esm and commonjs modules, so we pick just esm.
```
"exports": {
".": {
"import": "./hydrogen.es.js",
"require": "./hydrogen.cjs.js"
},
"./paths/vite": "./paths/vite.js",
"./style.css": "./style.css"
},
```
Also need to leave typescript type definitions out until the
Need to leave typescript type definitions out until the
typescript conversion is complete and all imports in the d.ts files
exists.
```
Expand Down
19 changes: 19 additions & 0 deletions vite.sdk-assets-config.js
Expand Up @@ -2,10 +2,29 @@ const path = require("path");
const mergeOptions = require('merge-options');
const commonOptions = require("./vite.common-config.js");

const pathsToExport = [
"index.js",
"index.css",
"download-sandbox.html"
];

export default mergeOptions(commonOptions, {
root: "src/",
base: "./",
build: {
outDir: "../target/asset-build/",
rollupOptions: {
output: {
assetFileNames: (chunkInfo) => {
// Get rid of the hash so we can consistently reference these
// files in our `package.json` `exports`
if(pathsToExport.includes(path.basename(chunkInfo.name))) {
return "assets/[name].[ext]";
}

return "assets/[name]-[hash][extname]";
}
}
}
},
});

0 comments on commit cd007b4

Please sign in to comment.