From 06730086934677f271042329c80b0ebe235a432c Mon Sep 17 00:00:00 2001 From: Seb Jacobs Date: Tue, 15 Apr 2025 12:35:10 +0100 Subject: [PATCH 1/3] refactor: remove unused STATIC_PATH from webpack config STATIC_PATH was introduced back in 2019 [1] as a way to allow consumers of scratch-gui (like ourselves) to customize the publicPath (where assets are served from when the JS is run in the browser). However it looks like this feature was removed/commented out [2] when they migrated to using the shared webpack config (from the npm package `scratch-webpack-configuration`). We would like to re-introduce this feature in order to fix an issue we have noticed with image assets failing to load as the browser is attempting to load thse assets from URLs relative to the webpack entry point (rather than from the root or `/scratch-gui`). Therefore I am removing this commented out code to make the existing config less confusing. [1] https://github.com/scratchfoundation/scratch-gui/commit/e2a2dcf0f [2] https://github.com/scratchfoundation/scratch-gui/commit/f1438f73e Co-Authored-By: Chris Lowis --- webpack.config.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 6813de1187f..0496a8a004a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,8 +7,6 @@ const HtmlWebpackPlugin = require('html-webpack-plugin'); const ScratchWebpackConfigBuilder = require('scratch-webpack-configuration'); -// const STATIC_PATH = process.env.STATIC_PATH || '/static'; - const baseConfig = new ScratchWebpackConfigBuilder( { rootPath: path.resolve(__dirname), From d3083b6c8a17363cecee266941cb05464efdc809 Mon Sep 17 00:00:00 2001 From: Seb Jacobs Date: Tue, 15 Apr 2025 12:37:53 +0100 Subject: [PATCH 2/3] refactor: group asset loading webpack config together The existing webpack config customizes the filenames of any assets we import which have not been inlined using the `assetModuleFilename`. This config option is defined at the global level, however it only applies to assets that are processed by the Asset module definition. Rather than configuring this globally, I have opted for defining it within the Asset module definition (nested under the `generator` option) as that makes it clearer that this option is only relevant for those assets thus making it easier for future developers to understand and make changes in the future, especially for those who are less familiar with the intricacies of webpack config. [1] https://webpack.js.org/guides/asset-modules/ Co-Authored-By: Chris Lowis --- webpack.config.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 0496a8a004a..f3a4ff7a195 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -16,7 +16,6 @@ const baseConfig = new ScratchWebpackConfigBuilder( .setTarget('browserslist') .merge({ output: { - assetModuleFilename: 'static/assets/[name].[hash][ext][query]', library: { name: 'GUI', type: 'umd2' @@ -32,7 +31,10 @@ const baseConfig = new ScratchWebpackConfigBuilder( .addModuleRule({ test: /\.(svg|png|wav|mp3|gif|jpg)$/, resourceQuery: /^$/, // reject any query string - type: 'asset' // let webpack decide on the best type of asset + type: 'asset', // let webpack decide on the best type of asset + generator: { + filename: 'static/assets/[name].[hash][ext][query]', + }, }) .addPlugin(new webpack.DefinePlugin({ 'process.env.DEBUG': Boolean(process.env.DEBUG), From acaed96fba7e48d57b6b2dac56b8273391243986 Mon Sep 17 00:00:00 2001 From: Seb Jacobs Date: Tue, 15 Apr 2025 10:44:59 +0100 Subject: [PATCH 3/3] fix: explicitly set publicPath for static assets https://github.com/RaspberryPiFoundation/experience-cs/issues/23 We recently embedded the scratch-gui within our Experience CS app. We have since noticed a number of issues with assets failing to load within the browser, for example the browser is attempting to load assets from URLs relative to the webpack entry point (rather than from the root or from `/scratch-gui/`). After some investigation it turns out that is due to the fact that the webpack option `publicPath` had not been explicity set [1] (which apparently default to `auto`). From digging into the webpack docs [2], it looks like when `publicPath` is set to `auto` it will attempt to load assets relative to the entrypoint which is not what we want. For our purposes we want assets to be served from `/scratch-gui`, and I can't see the need to make this configurable at this stage, therefore I have hardcoded this value for now as that also makes building the package less prone to error. However if we were to push changes upstream, then I think it would make sense to use an ASSET_PATH environment variable. Rather than configuring this globally, I have opted for defining it within the Asset module definition (nested under the `generator` option) as that makes it clearer that this option is only really relevant for those assets thus making it easier for future developers to understand and make changes in the future, especially for those who are less familiar with the intricacies of webpack config. I also dug into the latest version of scratch-gui and scratch-webpack-configuration and can confirm that this would still be an issue even with the latest releases, but at least they are now explicit about setting `publicPath` to `auto`, rather than the config option missing entirely and falling back to the default value of `auto` [3]. [1] https://github.com/scratchfoundation/scratch-webpack-configuration/blob/v1.5.1/src/index.cjs#L71 https://github.com/RaspberryPiFoundation/scratch-gui/blob/b723be4d1/webpack.config.js#L20 [2] https://webpack.js.org/guides/public-path/ ``` **Automatic publicPath** There are chances that you don't know what the publicPath will be in advance, and webpack can handle it automatically for you by determining the public path from variables like import.meta.url, document.currentScript, script.src or self.location. ``` [3] https://github.com/scratchfoundation/scratch-gui/blob/853caca41/webpack.config.js#L29 Co-Authored-By: Chris Lowis --- webpack.config.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/webpack.config.js b/webpack.config.js index f3a4ff7a195..25f280a7d5c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,6 +7,8 @@ const HtmlWebpackPlugin = require('html-webpack-plugin'); const ScratchWebpackConfigBuilder = require('scratch-webpack-configuration'); +const ASSET_PATH = '/scratch-gui/'; + const baseConfig = new ScratchWebpackConfigBuilder( { rootPath: path.resolve(__dirname), @@ -34,6 +36,7 @@ const baseConfig = new ScratchWebpackConfigBuilder( type: 'asset', // let webpack decide on the best type of asset generator: { filename: 'static/assets/[name].[hash][ext][query]', + publicPath: ASSET_PATH, }, }) .addPlugin(new webpack.DefinePlugin({