Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the SDK friendly to locally link and develop on #693

Merged

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Feb 26, 2022

Make the hydrogen-view-sdk friendly to locally link and develop on. Can now simply yarn watch:sdk to watch and build on changes and use in CommonJS and ESM environments.

Also adds yarn test:sdk to test whether the SDK works in an ESM Vite build and when using require in Node.js (CommonJS).

Fix #686
Fix #682
Fix #722

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 quickly rebuild on changes.

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

The last piece is making sure the ?url imports (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 release Was solved by vitejs/vite#7098 and is available in the latest Vite releases 🎉

Also updating vite because it includes fixes for #722 which were added in vitejs/vite#7822 ->vitejs/vite#7827

Dev notes

Use 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 link it


How to yarn install a package from a GitHub Monorepo? This doesn't work for Vite as it requires more build stuff.

See

yarn add https://gitpkg.now.sh/vitejs/vite/packages/vite?main --dev

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/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only remove the directory contents instead of the whole directory to maintain the npm link/yarn link symlink

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as a comment to the script

scripts/sdk/build.sh Outdated Show resolved Hide resolved
@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
Copy link
Contributor Author

@MadLittleMods MadLittleMods Feb 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/*"
Copy link
Contributor Author

@MadLittleMods MadLittleMods Feb 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]";
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]";
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, docs for the rollupOptions.output.assetFileNames option, https://rollupjs.org/guide/en/#outputassetfilenames

package.json Outdated Show resolved Hide resolved
@MadLittleMods MadLittleMods marked this pull request as draft February 26, 2022 11:23
@MadLittleMods MadLittleMods changed the title Make the SDK friendly to locally link and develop on Draft: Make the SDK friendly to locally link and develop on Feb 26, 2022
package.json Outdated Show resolved Hide resolved
@MadLittleMods MadLittleMods changed the title Draft: Make the SDK friendly to locally link and develop on Make the SDK friendly to locally link and develop on Apr 6, 2022
package.json Outdated Show resolved Hide resolved
@MadLittleMods MadLittleMods marked this pull request as ready for review April 6, 2022 00:30
…opment-and-commonjs

Conflicts:
	package.json
	scripts/sdk/base-manifest.json
	scripts/sdk/build.sh
scripts/sdk/build.sh Outdated Show resolved Hide resolved
@@ -11,9 +11,11 @@
"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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

package.json Outdated Show resolved Hide resolved
Copy link
Contributor

@bwindels bwindels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

scripts/sdk/build.sh Outdated Show resolved Hide resolved
"main": "./lib-build/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",
"./style.css": "./asset-build/assets/theme-element-light.css",
"./theme-element-light.css": "./asset-build/assets/theme-element-light.css",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Yes, we can! Should we do that in this PR or in a follow-up?

Copy link
Contributor Author

@MadLittleMods MadLittleMods May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 👍 reaction is ambiguous on what option to do so I'm going to opt to fix this in a fast follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wfm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #746

@@ -47,7 +49,7 @@
"regenerator-runtime": "^0.13.7",
"text-encoding": "^0.7.0",
"typescript": "^4.3.5",
"vite": "^2.6.14",
"vite": "^2.9.8",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Contributor

@bwindels bwindels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@bwindels bwindels added this to the er21mat22kenavo milestone May 17, 2022
Comment on lines +19 to +20
pushd target/asset-build
rm index.html
Copy link
Contributor Author

@MadLittleMods MadLittleMods May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] when running yarn build:sdk or yarn watch:sdk, I get this error:

[...]
rm: cannot remove 'index.html': No such file or directory

-- #693 (review)

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 👍

Copy link
Contributor

@bwindels bwindels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works now indeed, thanks!

@bwindels bwindels merged commit a49d7ea into master May 30, 2022
@MadLittleMods
Copy link
Contributor Author

Thanks for the review and testing @bwindels 🐗

MadLittleMods added a commit that referenced this pull request May 31, 2022
> Will be easier towards the future when adding more assets. Probably best to keep style.css for now for backwards compat though.
>
> *-- #693 (comment)
MadLittleMods added a commit to matrix-org/matrix-viewer that referenced this pull request Jun 6, 2022
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.
MadLittleMods added a commit to matrix-org/matrix-viewer that referenced this pull request Jun 6, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants