/2020-11-24-advent-calendar-2020-html-webpack-plugin-webpack-config-dynamic-chunks-implementation.ts
Created
November 24, 2020 16:22
-
-
Save hagevvashi/5dc3f91fa7c058265b8108311b85adeb to your computer and use it in GitHub Desktop.
html-webpack-pluginのchunksの動的な設定サンプル
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
import { Configuration } from "webpack"; | |
import glob from "glob"; | |
import HtmlWebpackPlugin from "html-webpack-plugin"; | |
const entries: string[] = glob.sync( | |
"src/path/**/*.t+(s|x)" | |
); | |
const chunks = entries.map((entry) => [ | |
entry.replace(/^src\/path\//, "").replace(/\.tsx?$/, ""), | |
`./${entry}`, | |
]); | |
const config: Configuration = { | |
entry: Object.fromEntries( | |
chunks.map(([chunk, entry]) => { | |
return [ | |
chunk, | |
[ | |
"./node_modules/formdata-polyfill/formdata.min.js", | |
`./${entry}`, | |
], | |
]; | |
}) | |
), | |
plugins: [ | |
...chunks.map( | |
([chunk]) => | |
new HtmlWebpackPlugin({ | |
filename: `./pages/${chunk}.html`, | |
// TODO: ここは適当。後から作る | |
template: "src/index.tsx", | |
chunks: [chunk], | |
}) | |
), | |
], | |
} | |
export default config; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment