feat: add rules for asset modules

This commit is contained in:
Christopher Willis-Ford 2024-03-18 11:25:04 -07:00
parent 44c77658ba
commit bde30f62b0
2 changed files with 53 additions and 11 deletions

View file

@ -68,6 +68,21 @@ module.exports = [
- `node` builds to `dist/node/`
- Supports merging in arbitrary configuration with `merge({...})`
### Asset Modules
This configuration makes webpack 5's [Asset Modules](https://webpack.js.org/guides/asset-modules/) available through
resource queries parameters:
```js
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)`

View file

@ -87,18 +87,45 @@ class ScratchWebpackConfigBuilder {
]
},
module: {
rules: [{
test: enableReact ? /\.[cm]?jsx?$/ : /\.[cm]?js$/,
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
...(
enableReact ? ['@babel/preset-react'] : []
)
]
rules: [
{
test: enableReact ? /\.[cm]?jsx?$/ : /\.[cm]?js$/,
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
...(
enableReact ? ['@babel/preset-react'] : []
)
]
}
},
{
// `asset` automatically chooses between exporting a data URI and emitting a separate file.
// Previously achievable by using `url-loader` with asset size limit.
resourceQuery: /^\?asset$/,
type: 'asset'
},
{
// `asset/resource` emits a separate file and exports the URL.
// Previously achievable by using `file-loader`.
resourceQuery: /^\?(resource|file)$/,
type: 'asset/resource'
},
{
// `asset/inline` exports a data URI of the asset.
// Previously achievable by using `url-loader`.
resourceQuery: /^\?(inline|url)$/,
type: 'asset/inline'
},
{
// `asset/source` exports the source code of the asset.
// Previously achievable by using `raw-loader`.
resourceQuery: /^\?(source|raw)$/,
type: 'asset/source'
}
}]
],
},
plugins: []
};