Make the SDK friendly to locally link and develop on#693
Conversation
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 🎉
| @@ -1,5 +1,5 @@ | |||
| #!/bin/bash | |||
| rm -rf target | |||
| rm -rf target/* | |||
There was a problem hiding this comment.
Only remove the directory contents instead of the whole directory to maintain the npm link/yarn link symlink
There was a problem hiding this comment.
Added as a comment to the script
| pushd asset-build/assets | ||
| mv main.*.js ../../main.js | ||
| mv index.*.css ../../style.css | ||
| mv download-sandbox.*.html ../../download-sandbox.html |
There was a problem hiding this comment.
Instead of needing to move these 3 files and rename them without the hash here, we handle removing the hash in vite.sdk-assets-config.js (rollupOptions.output.assetFileNames) and reference them directly in place in the package.json exports
| "exports": { | ||
| ".": { | ||
| "import": "./lib-build/hydrogen.es.js", | ||
| "require": "./lib-build/hydrogen.cjs.js" |
There was a problem hiding this comment.
Referencing hydrogen.es.js directly in the build, ./lib-build/hydrogen.es.js, to make it compatible with watching for an local changes and rebuild: yarn run vite build -c vite.sdk-lib-config.js --watch
| "./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/*" |
There was a problem hiding this comment.
Fix #682
Make the assets path available to reference and serve separately.
app.use(express.static(path.dirname(require.resolve('hydrogen-view-sdk/assets/main.js'))));| "import": "./lib-build/hydrogen.es.js", | ||
| "require": "./lib-build/hydrogen.cjs.js" | ||
| }, | ||
| "./paths/vite": "./paths/vite.js", |
There was a problem hiding this comment.
This export isn't used but it's part of the ./scripts/sdk/transform-paths.js ./src/platform/web/sdk/paths/vite.js ./target/paths/vite.js stuff in build.sh that's currently commented out.
| // files in our `package.json` `exports` | ||
| if(pathsToExport.includes(path.basename(chunkInfo.name))) { | ||
| return "assets/[name].[ext]"; | ||
| } |
There was a problem hiding this comment.
Instead of renaming these files in build.sh, we can just give them the proper name as part of the Vite build.
| } | ||
|
|
||
| return "assets/[name]-[hash][extname]"; | ||
| } |
There was a problem hiding this comment.
For reference, docs for the rollupOptions.output.assetFileNames option, https://rollupjs.org/guide/en/#outputassetfilenames
…opment-and-commonjs Conflicts: package.json scripts/sdk/base-manifest.json
Update to Vite which includes vitejs/vite#7098
…opment-and-commonjs Conflicts: package.json scripts/sdk/base-manifest.json scripts/sdk/build.sh
| "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/", | ||
| "test:postcss": "impunity --entry-point scripts/postcss/tests/css-compile-variables.test.js scripts/postcss/tests/css-url-to-variables.test.js", | ||
| "test:sdk": "yarn build:sdk && cd ./scripts/sdk/test/ && yarn --no-lockfile && node test-sdk-in-esm-vite-build-env.js && node test-sdk-in-commonjs-env.js", |
There was a problem hiding this comment.
This one is a bit clunky but it makes it easy to test whether hydrogen-view-sdk actually works in an ESM Vite build and when using require in Node.js (CommonJS).
All of the "testing" code was added in 2401b7f if you want to review it on its own. Feel free to prompt to remove or different way to test.
Thought the testing was slightly needed as testing manually in each environment is cumbersome. And we really don't know whether the SDK actually works before pushing it out. All of the usage code is just documented in SDK.md
bwindels
left a comment
There was a problem hiding this comment.
This is fantastic work, thank you so much for all your effort! And sorry for totally missing this PR before.
The changes look great, I just need to run the build on this branch to verify that it all still works, but I'll do that once the blockers are resolved, AIUI:
I've asked @MidhunSureshR to have a look.
| "./paths/vite": "./paths/vite.js", | ||
| "./style.css": "./asset-build/assets/index.css", | ||
| "./style.css": "./asset-build/assets/theme-element-light.css", | ||
| "./theme-element-light.css": "./asset-build/assets/theme-element-light.css", |
There was a problem hiding this comment.
Could we avoid this if we just say in the SDK docs that the path to the css file is hydrogen-view-sdk/assets/theme-element-light.css rather than hydrogen-view-sdk/theme-element-light.css. Will be easier towards the future when adding more assets. Probably best to keep style.css for now for backwards compat though.
There was a problem hiding this comment.
Could we avoid this if we just say in the SDK docs that the path to the css file is
hydrogen-view-sdk/assets/theme-element-light.cssrather thanhydrogen-view-sdk/theme-element-light.css. Will be easier towards the future when adding more assets.
Yes, we can! Should we do that in this PR or in a follow-up?
There was a problem hiding this comment.
The 👍 reaction is ambiguous on what option to do so I'm going to opt to fix this in a fast follow-up PR.
…opment-and-commonjs Conflicts: scripts/sdk/build.sh
Fix #722 Updating Vite to includes fixes from vitejs/vite#7822 -> vitejs/vite#7827
| "text-encoding": "^0.7.0", | ||
| "typescript": "^4.3.5", | ||
| "vite": "^2.6.14", | ||
| "vite": "^2.9.8", |
There was a problem hiding this comment.
Updating Vite to include fix from vitejs/vite#7098 which allows import workerPath from 'hydrogen-view-sdk/main.js?url'; to still work even though we have package.json exports defined (vite@2.9.1).
And to includes fixes for #722 which were added in vitejs/vite#7822 ->vitejs/vite#7827 (vite@2.9.8)
…opment-and-commonjs
…opment-and-commonjs Conflicts: scripts/sdk/base-manifest.json
bwindels
left a comment
There was a problem hiding this comment.
Thanks, changes look great, but when running yarn build:sdk or yarn watch:sdk, I get this error:
$ yarn watch:sdk
yarn run v1.22.17
$ ./scripts/sdk/build.sh && yarn run vite build -c vite.sdk-lib-config.js --watch
$ /home/bwindels/dev/hydrogen-web/node_modules/.bin/vite build -c vite.sdk-assets-config.js
Generated an empty chunk: "vendor"
Generated an empty chunk: "index"
$ /home/bwindels/dev/hydrogen-web/node_modules/.bin/vite build -c vite.sdk-lib-config.js
$ /home/bwindels/dev/hydrogen-web/node_modules/.bin/tsc -p tsconfig-declaration.json
~/dev/hydrogen-web/target ~/dev/hydrogen-web
~/dev/hydrogen-web/target/asset-build/assets ~/dev/hydrogen-web/target ~/dev/hydrogen-web
~/dev/hydrogen-web/target ~/dev/hydrogen-web
rm: cannot remove 'index.html': No such file or directory
| pushd target/asset-build | ||
| rm index.html |
There was a problem hiding this comment.
[...] when running
yarn build:sdkoryarn watch:sdk, I get this error:[...] rm: cannot remove 'index.html': No such file or directory
Fixed this up by removing index.html in the right spot (target/asset-build/index.html).
Sorry didn't pick up on this earlier. I think it's because when running on Windows the git bash window which pops up when you yarn build:sdk to run the shell code closes so fast and masks errors for me. I've tested this on macOS and everything is working now 👍
bwindels
left a comment
There was a problem hiding this comment.
Works now indeed, thanks!
…opment-and-commonjs
|
Thanks for the review and testing @bwindels 🐗 |
> Will be easier towards the future when adding more assets. Probably best to keep style.css for now for backwards compat though. > > *-- #693 (comment)
Trying to get this project again after a few months of changes from `hydrogen-view-sdk` and now finally after element-hq/hydrogen-web#693 merged to make the SDK friendly to locally link and develop on.
Get this project running again after a few months of changes from `hydrogen-view-sdk` and now finally after element-hq/hydrogen-web#693 merged to make the SDK friendly to locally link and develop on.
Make the
hydrogen-view-sdkfriendly to locally link and develop on. Can now simplyyarn watch:sdkto watch and build on changes and use in CommonJS and ESM environments.Also adds
yarn test:sdkto test whether the SDK works in an ESM Vite build and when usingrequirein Node.js (CommonJS).Fix #686
Fix #682
Fix #722
Instead of deleting the whole
target/directory, leave it alone so the symlink driving thenpm link/yarn linkstays in tact.Leave Vite builds in their build directories (
/lib-build//asset-build) so you canvite build --watchto build on local changes and still have a consisent place to reference in thepackage.jsonexports. Previously, everything relied onbuild.shwhich does a bunch of moving and renaming and made it hard to quickly rebuild on changes.Add back support for CommonJS (adding the
package.jsonexports).The last piece is making sure the
?urlimports (ex.import workerPath from 'hydrogen-view-sdk/main.js?url';) still work.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 releaseWas solved by vitejs/vite#7098 and is available in the latest Vite releases 🎉Also updating
vitebecause it includes fixes for #722 which were added in vitejs/vite#7822 ->vitejs/vite#7827Dev notes
package.jsonandexportsdocs: https://nodejs.org/docs/latest-v17.x/api/packages.htmlUse the latest Vite from GitHub source (before release)
How can I try out the latest Vite without waiting for a release?
See https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md#repo-setup to build locally and
npm linkitHow to yarn install a package from a GitHub Monorepo? This doesn't work for Vite as it requires more build stuff.
See