Shared configuration for Scratch's use of webpack
Find a file
renovate[bot] 4bec0b875a
Some checks are pending
CI/CD / ci-cd (push) Waiting to run
chore(deps): update dependency husky to v9.1.7 (#64)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-11-21 18:23:05 +00:00
.github chore(deps): update actions/setup-node action to v4 2024-03-11 14:08:10 +00:00
.husky chore: add husky hook for commitlint 2024-03-08 18:09:52 -08:00
src feat: default to publicPath: '/' and allow overrides 2024-09-24 12:25:45 +03:00
test test: add tests 2024-03-08 17:45:46 -08:00
.editorconfig chore: configure Renovate 2024-03-08 16:59:50 -08:00
.gitattributes chore: npm init and some dotfiles 2024-03-08 07:41:13 -08:00
.gitignore Initial commit 2024-03-08 07:32:45 -08:00
.nvmrc ci: set up GHA 2024-03-08 17:54:44 -08:00
CHANGELOG.md chore(release): 1.6.0 [skip ci] 2024-09-25 08:42:11 +00:00
commitlint.config.js chore: add commitlint 2024-03-08 17:58:02 -08:00
LICENSE Initial commit 2024-03-08 07:32:45 -08:00
package-lock.json chore(deps): update dependency husky to v9.1.7 (#64) 2024-11-21 18:23:05 +00:00
package.json chore(deps): update dependency husky to v9.1.7 (#64) 2024-11-21 18:23:05 +00:00
README.md feat: add rules for asset modules 2024-03-18 11:25:13 -07:00
release.config.js ci: configure semantic-release 2024-03-08 18:15:17 -08:00
renovate.json5 chore: configure Renovate 2024-03-08 16:59:50 -08:00

scratch-webpack-configuration

Shared configuration for Scratch's use of webpack

Usage

Add something like this to your webpack.config.*js file:

import ScratchWebpackConfigBuilder from 'scratch-webpack-configuration';

const builder = new ScratchWebpackConfigBuilder(
    {
        rootPath: __dirname,
        enableReact: true
    })
    .setTarget('browserslist')
    .addModuleRule({
        test: /\.css$/,
        use: [/* CSS loaders */]
    })
    .addPlugin(new CopyWebpackPlugin({
        patterns: [/* CopyWebpackPlugin patterns */]
    });

if (process.env.FOO === 'bar') {
    builder.addPlugin(new MyCustomPlugin());
}

module.exports = builder.get();

Call addModuleRule and addPlugin as few or as many times as needed. If you need multiple configurations, you can use clone() to share a base configuration and then add or override settings:

const baseConfig = new ScratchWebpackConfigBuilder({rootPath: __dirname, libraryName: 'my-library'})
    .addModuleRule({
        test: /\.foo$/,
        use: [/* FOO loaders */]
    });

const config1 = baseConfig.clone()
    .setTarget('browserslist')
    .merge({/* arbitrary configuration */})
    .addPlugin(new MyCustomPlugin('hi'));

const config2 = baseConfig.clone()
    .setTarget('node')
    .addPlugin(new MyCustomPlugin('hello'));

module.exports = [
    config1.get(),
    config2.get()
];

What it does

  • Sets up a default configuration that is suitable for most Scratch projects
    • Use enableReact to enable React support
    • Target node or browserslist (more targets will be added as needed)
  • Adds babel-loader with the @babel/preset-env preset
    • Adds @babel/preset-react if React support is enabled
  • Adds target-specific presets for webpack 5's externals and externalsPresets settings
  • Target-specific output directory under dist/
    • browserslist builds to dist/web/
    • node builds to dist/node/
  • Supports merging in arbitrary configuration with merge({...})

Asset Modules

This configuration makes webpack 5's Asset Modules available through resource queries parameters:

import myImage from './my-image.png?asset'; // Use `asset` (let webpack decide)
import myImage from './my-image.png?resource'; // Use `asset/resource`, similar to `file-loader`
import myImage from './my-image.png?inline'; // Use `asset/inline`, similar to `url-loader`
import myImage from './my-image.png?source'; // Use `asset/source`, similar to `raw-loader`

You can also use file for asset/resource, url for asset/inline, and raw for asset/source, to make it clear which loader you're replacing.

API

new ScratchWebpackConfigBuilder(options)

Creates a new ScratchWebpackConfigBuilder instance.

options

Required:

  • rootPath (string, required): The root path of the project. This is used to establish defaults for other paths.

Optional:

  • distPath (string, default: path.join(rootPath, 'dist')): The path to the output directory. Defaults to dist
  • enableReact (boolean, default: false): Whether to enable React support. Adds .jsx to the list of extensions to process, and adjusts Babel settings.
  • libraryName (string, default: undefined): If set, configures a default entry point and output library name. under the root path.
  • srcPath (string, default: path.join(rootPath, 'src')): The path to the source directory. Defaults to src under the root path.

Package exports

The exports field in package.json

Most project.json files specify a main entry point, and some specify browser as well. Newer versions of Node support the exports field as well. If both are present, exports will take precedence.

For more information about exports, see: https://webpack.js.org/guides/package-exports/, especially the "Target environment" section.

Unfortunately, plenty of tools don't support exports yet, and some that do exhibit some surprising quirks.

Here's what I currently recommend for a project with only one entry point:

{
  "main": "./dist/node/foo.js",
  "browser": "./dist/web/foo.js",
  "exports": {
    "webpack": "./src/index.js",
    "browser": "./dist/web/foo.js",
    "node": "./dist/node/foo.js",
    "default": "./src/index.js"
  },
}
  • main supports older Node as well as jest
  • browser is present for completeness; I haven't found it strictly necessary
  • exports.webpack is the entry point for Webpack
    • webpack will grab the first item under exports matching its conditions, including browser, so I recommend listing exports.webpack first in the exports object
    • this allows (for example) scratch-gui to build scratch-vm from source rather than using the prebuilt version, resulting in more optimal output and preventing version conflicts due to bundled dependencies
  • exports.default makes eslint happy
  • exports.browser and exports.node prevent exports-aware tools from using exports.default for all contexts

Note that using src/index.js for the webpack and default exports means that the NPM package must include src.

browserslist target

While it could be handy to include browserslist configuration in this package, there are tools other than webpack that should use the same browserslist configuration. For that reason, I recommend configuring browserslist in your package.json file or in a top-level .browserslistrc file.

The Scratch system requirements determine the browsers we should target. That information can be found here: https://scratch.mit.edu/faq