1+ const path = require ( 'path' ) ;
2+ const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' ) ;
3+ // const MiniCssExtractPlugin = require('mini-css-extract-plugin');
4+ const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
5+
6+
7+ module . exports = {
8+ entry : "./src/Main.tsx" ,
9+ mode : process . env . NODE_ENV . trim ( ) == 'production' ? 'production' : 'development' ,
10+ output : {
11+ path : path . resolve ( __dirname , 'dist' ) ,
12+ filename : 'static/js/[name].js' ,
13+ } ,
14+
15+
16+ devServer : {
17+ compress : true ,
18+ historyApiFallback : true ,
19+ port : 8091 ,
20+ open : true ,
21+ devMiddleware : {
22+ writeToDisk : true ,
23+ } ,
24+ } ,
25+
26+
27+ // Enable sourcemaps for debugging webpack's output.
28+ devtool : "source-map" ,
29+
30+ resolve : {
31+ extensions : [ '.ts' , '.tsx' , '.js' , '.jsx' , '.scss' , '.css' ] ,
32+ symlinks : false ,
33+ modules : [ 'node_modules' ] ,
34+ fallback : { "crypto" : false }
35+ } ,
36+
37+ module : {
38+ rules : [
39+ // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
40+ {
41+ test : / \. t s x ? $ / ,
42+ exclude : / n o d e _ m o d u l e s / ,
43+ use : "ts-loader"
44+ } ,
45+ // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
46+ {
47+ enforce : 'pre' ,
48+ test : / \. j s $ / ,
49+ loader : "source-map-loader"
50+ } ,
51+ {
52+ test : / \. c s s $ / ,
53+ use : [ 'style-loader' , 'css-loader' ] //在Webpack中,loader的执行顺序是从右向左执行的,webpack先将所有css模块依赖解析完得到计算结果再创建style标签。因此把style-loader放在css-loader的前面。
54+ } ,
55+ ]
56+ } ,
57+ plugins : [
58+ /**
59+ * All files inside webpack's output.path directory will be removed once, but the
60+ * directory itself will not be. If using webpack 4+'s default configuration,
61+ * everything under <PROJECT_DIR>/dist/ will be removed.
62+ * Use cleanOnceBeforeBuildPatterns to override this behavior.
63+ *
64+ * During rebuilds, all webpack assets that are not used anymore
65+ * will be removed automatically.
66+ *
67+ * See `Options and Defaults` for information
68+ */
69+ new CleanWebpackPlugin ( ) ,
70+ new HtmlWebpackPlugin ( {
71+ template : './index.html' ,
72+ filename : 'index.html' ,
73+ } ) ,
74+ ] ,
75+ // When importing a module whose path matches one of the following, just
76+ // assume a corresponding global variable exists and use that instead.
77+ // This is important because it allows us to avoid bundling all of our
78+ // dependencies, which allows browsers to cache those libraries between builds.
79+ externals : {
80+ }
81+ } ;
0 commit comments