Add Webpack dev-server setting
parent
b3dbef891e
commit
2c0dbb4f08
@ -1,35 +1,66 @@
|
|||||||
const path = require('path');
|
const path = require("path");
|
||||||
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
||||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
const HtmlWebpackPlugin = require("html-webpack-plugin");
|
||||||
|
|
||||||
|
// Ref: https://stackoverflow.com/a/60021512/6133998
|
||||||
|
const populateHtmlPlugins = (pagesArray) => {
|
||||||
|
res = [];
|
||||||
|
pagesArray.forEach((page) => {
|
||||||
|
res.push(
|
||||||
|
new HtmlWebpackPlugin({
|
||||||
|
page,
|
||||||
|
filename: `${page.toLowerCase()}.html`,
|
||||||
|
template: `./src/${page.toLowerCase()}.html`,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
const pages = populateHtmlPlugins(["index", "cards"]);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
entry: './src/main.js',
|
entry: {
|
||||||
|
main: ["./src/main.js"],
|
||||||
|
spacelab: ["./src/scss/spacelab-theme.scss"],
|
||||||
|
cyborg: ["./src/scss/cyborg-theme.scss"]
|
||||||
|
},
|
||||||
output: {
|
output: {
|
||||||
path: path.resolve(__dirname, 'dist'),
|
path: path.resolve(__dirname, "dist"),
|
||||||
filename: 'js/[name].js',
|
filename: "js/[name].js",
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
modules: ['node_modules'],
|
modules: ["node_modules"],
|
||||||
},
|
},
|
||||||
module: {
|
module: {
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
test: /\.(sa|sc|c)ss$/,
|
test: /\.(sa|sc|c)ss$/,
|
||||||
use: [
|
use: [
|
||||||
|
// 取代 style-loader。作用: 提取 js 中的 css 成單獨文件
|
||||||
MiniCssExtractPlugin.loader,
|
MiniCssExtractPlugin.loader,
|
||||||
|
// 將 css 文件整合倒 js 文件中
|
||||||
"css-loader",
|
"css-loader",
|
||||||
"sass-loader"
|
// 將 sass/scss 編譯成 css
|
||||||
|
"sass-loader",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
test: /\.html$/,
|
||||||
|
loader: "raw-loader",
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
new MiniCssExtractPlugin({
|
new MiniCssExtractPlugin({
|
||||||
filename: 'css/[name].css',
|
filename: "css/[name].css",
|
||||||
}),
|
|
||||||
new HtmlWebpackPlugin({
|
|
||||||
template: './src/index.html',
|
|
||||||
filename: 'index.html',
|
|
||||||
}),
|
}),
|
||||||
|
...pages,
|
||||||
],
|
],
|
||||||
|
devServer: {
|
||||||
|
// contentBase: path.join(__dirname, "dist"),
|
||||||
|
compress: true,
|
||||||
|
port: 9000,
|
||||||
|
open: true,
|
||||||
|
hot: true,
|
||||||
|
},
|
||||||
};
|
};
|
Loading…
Reference in New Issue