Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hagevvashi/5dc3f91fa7c058265b8108311b85adeb to your computer and use it in GitHub Desktop.
Save hagevvashi/5dc3f91fa7c058265b8108311b85adeb to your computer and use it in GitHub Desktop.
html-webpack-pluginのchunksの動的な設定サンプル
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