Shared Assets in Webpack
Now let's try shared assets. Let's make it so that images smaller than 8kb
are converted to base64, and images larger than this size are copied to the file:
export default {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve('dist'),
},
module: {
rules: [
{
test: /\.png$/,
type: 'asset',
}
]
},
plugins: [
new HtmlWebpackPlugin(),
],
};
You can change the image size limit. For example, let's set a limit of 4kb
:
export default {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve('dist'),
},
module: {
rules: [
{
test: /\.png$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 4 * 1024 // 4kb
}
}
}
]
},
plugins: [
new HtmlWebpackPlugin(),
],
};
Connect several images of different sizes to the entry point. Check what type of transformation is applied to each image.