-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
125 lines (117 loc) · 3.49 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const path = require("path");
const fs = require("fs");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const Dotenv = require('dotenv-webpack');
/* variables */
/* global process __dirname module */
const isProduction = process.argv.indexOf("-p") >= 0;
const PAGE_DIR = path.join("src", "pages", path.sep);
/**
* more info for multi-page webpack you can follow the link
* @link: https://itnext.io/building-multi-page-application-with-react-f5a338489694 */
function getFilesFromDir(dir, fileTypes) {
const filesToReturn = [];
function walkDir(currentPath) {
const files = fs.readdirSync(currentPath);
for (let i in files) {
const curFile = path.join(currentPath, files[i]);
if (fs.statSync(curFile).isFile() && fileTypes.indexOf(path.extname(curFile)) != -1) {
filesToReturn.push(curFile);
} else if (fs.statSync(curFile).isDirectory()) {
walkDir(curFile);
}
}
}
walkDir(dir);
return filesToReturn;
}
const jsFiles = getFilesFromDir(PAGE_DIR, [".js", ".ts"]);
const entry = jsFiles.reduce((obj, filePath) => {
const entryChunkName = filePath.replace(path.extname(filePath), "").replace(PAGE_DIR, "");
obj[entryChunkName] = `./${filePath}`;
return obj;
}, {});
const htmlFiles = getFilesFromDir(PAGE_DIR, [".html"]);
const htmlPlugins = htmlFiles.map(filePath => {
const fileName = filePath.replace(PAGE_DIR, "");
return new HtmlWebpackPlugin({
chunks: [fileName.replace(path.extname(fileName), ""), "vendor"],
template: filePath,
filename: fileName,
minify: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
},
});
});
// babel config
const babelLoaderConfig = {
loader: "babel-loader",
options: { babelrc: true },
};
module.exports = {
entry: entry,
output: {
path: path.join(__dirname, "build"),
filename: "[name].[hash:4].js"
},
module: {
rules: [
// .js, .ts
{
test: /\.(js|ts)$/,
exclude: /node_modules/,
use: [babelLoaderConfig],
},
{
test: /\.css$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : "style-loader",
{
loader: "css-loader",
},
],
},
{
test: /\.(svg|gif|jpe?g|png|eot|ttf|woff2?)$/,
loader: "url-loader",
exclude: /node_modules/,
options: {
esModule: false,
outputPath: (url, resourcePath, context) => {
if (!isProduction) {
const relativePath = path.relative(context, resourcePath);
return `/${relativePath}`;
}
return `assets/${path.basename(resourcePath)}`;
},
/* TODO work around to fix image in development enviroment */
limit: isProduction ? 3000 : 99999,
publicPath: isProduction ? "../" : '/',// Take the directory into account
name: `assets/[name].[ext]`,
},
},
],
},
plugins: [
...htmlPlugins,
new CleanWebpackPlugin(),
new Dotenv(),
new MiniCssExtractPlugin({
filename: "[name][chunkhash:4].css",
chunkFilename: "[name].[chunkhash:4].css",
}),
],
resolve: {
extensions: [".js", ".ts"],
alias: {
src: path.resolve(__dirname, "src"),
},
},
};