fix: bundle blockly test images in block-test plugin #2024
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The basics
This PR will address issues like bundling assets (specifically tested with png files in @blockly/block-test) in webpack UMD bundles, as well as potential future plugin ESM builds.
It makes use of the following:
See https://web.dev/articles/bundling-non-js-resources for details on
new URL(..., import.meta.url)
patternpublicPath configuration in webpack https://webpack.js.org/guides/public-path/#automatic-publicpath
I validated my changes
The details
Resolves
Fixes #503
Fixes #1145
This bundles needed static media files from
https://github.com/google/blockly/tree/develop/tests/media
into@blockly/block-test
package@maribethb and I discussed in detail (see #1969) on what potential approaches that could enable plugins to bundle assets in their builds. The universal way is inline assets using dataUrls, which is not that interesting in itself and may negatively impact performance.
The approach in this PR requires more tooling support depending on how you ingest the package in your app however the upside is that Webpack supports this natively and there is a minimal change to
@blockly/dev-scripts
.Here are the details:
Webpack will include the png files as
<hash>.png
in /dist in@blockly/block-test
package.json module field still points to /src with code
new URL('../media/30px.png', import.meta.url)
bundlers like Webpack will use the module field to build any code that references this plugin via the module field.
So code like
@blockly/dev-tools
orexamples/blockly-react
will also include png files as<hash>.png
in their dist/I also briefly tested removing module field from @blockly/block-test's package.json and it seems that @blockly/dev-tools will still build fine and include all the assets.
npm start
in@blockly/block-test
will also work nowFor plain html files, import.meta will work when you do a script tag with type module, ie:
<script type="module" src="./path/to/@blockly/block-test/dist/index.esm.js">
However we are not building esm format at the moment (see #1877 and #1997) so this is not possible today.
Lastly if you just do a plain script tag in a html file
<script src="./path/to/@blockly/dev-tools/dist/index.js">
(eg dev-tools imports block-test as mentioned above) we rely onpublicPath: "auto"
configuration in webpack to usedocument.currentScript.src
to figure out the correct URL path to the asset in question, such as in advanced playgroundThe relevant change in the UMD output is:
Proposed Changes
See above for details
Reason for Changes
Fixes #503
Fixes #1145 (this should provide a way to load any audio assets but I have not tested)
Test Coverage
npm run start
in@blockly/block-test
Documentation
N/A
Additional Information
Preferably requires more testing from the blockly team to ensure
@blockly/dev-tools
still works as expected since it imports@blockly/block-test
cc @cpcallen