Skip to content

Instantly share code, notes, and snippets.

@lvivski
Created March 10, 2023 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvivski/b3245a494be6a862b63dc4e00012eb1b to your computer and use it in GitHub Desktop.
Save lvivski/b3245a494be6a862b63dc4e00012eb1b to your computer and use it in GitHub Desktop.
Webpack Stress Test
const fs = require('fs');
const path = require('path');
const { randomBytes } = require('crypto');
function randomString(size) {
return randomBytes(size).toString('hex');
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const dir = path.resolve(__dirname, `./src/modules/`);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const lazyImports = [];
const lazyChunks = 100;
const totalChunks = 10_000;
for (let i = 0; i <= totalChunks; i++) {
const filePath = path.resolve(dir, `${i}.js`);
fs.writeFileSync(filePath, `export default "${randomString(randomInt(1000, 20_000))}";\n`);
lazyImports.push(`import('modules/${i}').then(console.log);`);
if (i % lazyChunks === 0) {
fs.appendFileSync(filePath, lazyImports.slice(-lazyChunks, -1).join('\n'));
}
}
for (let i = 0; i < 5; i++) {
const indexFilePath = path.resolve(__dirname, `./src/index_${i}.js`);
fs.writeFileSync(indexFilePath, lazyImports.filter((_, i) => i % lazyChunks === 0).join('\n'));
}
{
"devDependencies": {
"webpack": "5.76.0",
"webpack-cli": "5.0.1"
},
"scripts": {
"gen": "node gen.js",
"start": "webpack build --mode=production --progress=profile"
}
}
const path = require('path');
module.exports = {
entry: Object.fromEntries(Array.from({ length: 5 }, (_, i) => [`index_${i}`, `./src/index_${i}.js`])),
output: {
filename: '[name]-[contenthash].js',
chunkFilename: `[name]-[contenthash].js`,
path: path.resolve(__dirname, 'dist'),
},
resolve: {
modules: [path.resolve(__dirname, 'src')]
},
optimization: {
realContentHash: true,
splitChunks: {
chunks: 'all',
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment