Created
March 10, 2023 06:02
-
-
Save lvivski/b3245a494be6a862b63dc4e00012eb1b to your computer and use it in GitHub Desktop.
Webpack Stress Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"devDependencies": { | |
"webpack": "5.76.0", | |
"webpack-cli": "5.0.1" | |
}, | |
"scripts": { | |
"gen": "node gen.js", | |
"start": "webpack build --mode=production --progress=profile" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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