-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.ts
170 lines (158 loc) · 6.26 KB
/
webpack.config.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import Path from "path";
import Glob from "glob";
import Webpack from "webpack";
import HtmlWebpackPlugin from "html-webpack-plugin";
import CopyWebpackPlugin from "copy-webpack-plugin";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import FriendlyErrorsWebpackPlugin from "friendly-errors-webpack-plugin";
import OptimizeCSSAssetsPlugin from "optimize-css-assets-webpack-plugin";
import ErudaWebpackPlutin from "eruda-webpack-plugin";
import CaseSensitivePathsPlugin from "case-sensitive-paths-webpack-plugin";
import ManifestPlugin from "webpack-manifest-plugin";
import HardSourceWebpackPlugin from "hard-source-webpack-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
import { WebpackBuildEnvironmentEnum, Configuration, ConfigurationFactory } from "./properties";
export default () => {
const env = process.env.WEBPACK_ENV as WebpackBuildEnvironmentEnum;
const config = ConfigurationFactory(env);
const devMode = env === WebpackBuildEnvironmentEnum.dev;
const { entries, htmlWebpackPlugins } = FindPages(config, devMode);
const webpackConfig: Webpack.Configuration & { devServer: any } = {
entry: entries,
devtool: config.devtool,
devServer: config.devServer,
mode: devMode ? "development" : "production",
output: {
path: config.output,
filename: config.filename,
publicPath: config.assetsPublicPath
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"],
alias: { "@": Path.join(__dirname, "src") }
},
externals: {
react: "React",
"react-dom": "ReactDOM"
},
module: {
rules: [
{
test: /\.(ts)x?$/,
include: [/src/],
use: {
loader: "awesome-typescript-loader",
options: {
useCache: true,
reportFiles: ["src/**/*.{ts,tsx}"],
cacheDirectory: "./node_modules/.awcache,",
forceIsolatedModules: true
}
}
},
{
test: /\.css$/,
use: devMode ? ["style-loader", "css-loader"] : [MiniCssExtractPlugin.loader, require.resolve("css-loader")]
},
{
test: /\.scss$$/,
include: [/src/],
use: devMode ? ["style-loader", "css-loader", "sass-loader", "postcss-loader"] : [MiniCssExtractPlugin.loader, "css-loader", "sass-loader", "postcss-loader"]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: "url-loader",
options: {
limit: 100,
name: "images/[name].[hash:5].[ext]"
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: "url-loader",
options: {
limit: 100,
name: "media/[name].[hash:5].[ext]"
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: "url-loader",
options: {
limit: 100,
name: "fonts/[name].[hash:5].[ext]"
}
}
]
},
optimization: {
removeEmptyChunks: true,
removeAvailableModules: true,
minimize: env === WebpackBuildEnvironmentEnum.prod
},
plugins: GetPlugins(config, devMode).concat(htmlWebpackPlugins)
};
return webpackConfig;
};
/**
* 寻找页面
* @param config 配置
* @param devMode 是否开发模式
*/
function FindPages(config: Configuration, devMode: boolean) {
const entries: Webpack.Entry = {};
const htmlWebpackPlugins: Webpack.Plugin[] = [];
Glob.sync(Path.join(__dirname, "./src/pages/*/config.json")).forEach((entry) => {
const dirname = Path.dirname(entry);
const name = Path.basename(dirname);
const pageConfig = require(entry);
console.log("✔ \t", pageConfig);
entries[name] = `${dirname}/index.tsx`;
htmlWebpackPlugins.push(
new HtmlWebpackPlugin({
filename: `${name}.html`,
template: pageConfig.userDefaultPage ? "index.html" : `${dirname}/index.html`,
inject: true,
title: pageConfig.title,
assetsPublicPath: config.assetsPublicPath
})
);
});
console.log("\n\n");
return { entries, htmlWebpackPlugins };
}
/**
* 获取Webpack插件
* @param config 配置
* @param devMode 是否开发模式
*/
function GetPlugins(config: Configuration, devMode: boolean): Webpack.Plugin[] {
let environmentPlugins: Webpack.Plugin[] = [];
const basePlugins: Webpack.Plugin[] = [WebpackVariablePlugin(config), new CaseSensitivePathsPlugin(), new CopyWebpackPlugin([{ from: Path.resolve("static/**/*"), to: Path.resolve(config.output) }]), new HardSourceWebpackPlugin()];
if (devMode) {
environmentPlugins = [new Webpack.HotModuleReplacementPlugin(), new FriendlyErrorsWebpackPlugin(), new ErudaWebpackPlutin()];
} else {
environmentPlugins = [
new CleanWebpackPlugin(),
new ManifestPlugin(),
new Webpack.HashedModuleIdsPlugin(),
new MiniCssExtractPlugin({ filename: "css/[name].[contenthash:5].css" }),
new OptimizeCSSAssetsPlugin(),
new Webpack.optimize.ModuleConcatenationPlugin(),
new BundleAnalyzerPlugin()
];
}
return basePlugins.concat(environmentPlugins);
}
/**
* 创建webpack变量插件
* @param {*} config
*/
function WebpackVariablePlugin(config: Configuration) {
const variable: any = {};
for (let variableName in config.variable) {
variable[`process.env.${variableName}`] = JSON.stringify(config.variable[variableName]);
}
return new Webpack.DefinePlugin(variable);
}