-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
122 lines (113 loc) · 3.79 KB
/
webpack.config.babel.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
//这边使用 HtmlWebpackPlugin,将 bundle 好的 <script> 插入到 body
import HtmlWebpackPlugin from 'html-webpack-plugin';
import {resolve} from 'path';
import webpack from 'webpack';
import {spawn, execSync} from 'child_process';
const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
template: `${__dirname}/src/index.html`,
filename: 'index.html',
inject: 'body'
});
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const isDev = process.env.NODE_ENV === "development" || process.env.DEBUG;
//path for dev server
const publicPath = isDev ? 'http://localhost:8080/' : './';
const extractSass = new ExtractTextPlugin({
filename: "[name].[hash].css",
disable: isDev
});
module.exports = {
// 档案起始点从 entry 进入,可以是多个档案
entry: [
'react-hot-loader/patch',
// activate HMR for React
'webpack-dev-server/client?http://localhost:8080',
// bundle the client for webpack-dev-server
// and connect to the provided endpoint
'webpack/hot/only-dev-server',
// bundle the client for hot reloading
// only- means to only hot reload for successful updates
'./src/index.js'
],
//output 是放入产生出来的结果的相关参数
//使打包后的错误信息精确到行
devtool: 'inline-source-map',
output: {
path: resolve(__dirname, 'build'),
publicPath: publicPath,
// necessary for HMR to know where to load the hot update chunks
filename: 'bundle.js'
// the output bundle
},
module: {
rules: [
{
test: /\.js?$/,
// use: ['babel-loader',],
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
"presets": [["es2015", {"modules": false}], "react", "stage-2"]
}
}
]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader?pages',],
},
{
test: /\.scss$/,
use: extractSass.extract({
use: [{
loader: "css-loader"
}, {
loader: "sass-loader"
}],
// use style-loader in development
fallback: "style-loader"
})
},
{
test: /\.(png|jpg)$/,
loader: 'url-loader?limit=8192'
}
],
},
//devServer 则是webpack-dev-server设定
devServer: {
hot: true,
//enable HMR on the server
// contentBase: resolve(__dirname, 'dist'),
contentBase: './build',
//match output path
publicPath: publicPath,
//match output `publicPath`
setup() {
if (process.env.DESKTOP) {
spawn(
'npm',
['run', 'start-desktop'],
{shell: true, env: process.env, stdio: 'inherit'}
)
.on('close', code => process.exit(code))
.on('error', spawnError => console.error(spawnError));
}
}
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'product'),
}),
HTMLWebpackPluginConfig,
new webpack.HotModuleReplacementPlugin(),
// enable HMR globally
new webpack.NamedModulesPlugin(),
// prints more readable module names in the browser console on HMR updates
extractSass
// sass
]/*,
target: 'electron-renderer'*/
};