-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
141 lines (134 loc) · 3.8 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* eslint-disable */
const path = require('path');
const webpack = require('webpack');
const utils = require('./scripts/utils');
const createTheme = require('./scripts/theme');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
module.exports = function start(env) {
createTheme();
const nodeEnv = env.env || 'development';
const distOutPutPath = path.resolve(__dirname, `dist/${nodeEnv}`);
const action = env.action || 'start';
const routerType = env.routertype || 'hash'; // hash || history
const isBuild = action === 'build';
let plugins = [new HtmlWebpackPlugin({
filename: `index.html`,
template: `./index.html`,
inject: 'body',
chunks: ['index'],
hash: true,
})];
if (isBuild) {
utils.rmdirSync(distOutPutPath);
} else {
const openurl = env.openurl || '';
if (openurl.length > 0) {
plugins.push(new OpenBrowserPlugin({ url: openurl }));
}
plugins.push(new webpack.HotModuleReplacementPlugin());
}
return {
context: path.resolve(__dirname, 'demo'),
mode: ['development', 'production', 'none'].indexOf(nodeEnv) < 0 ? 'development' : nodeEnv,
entry: {
index: ['babel-polyfill', './index.js'],
},
output: {
filename: '[name].[hash:8].js',
chunkFilename: !isBuild ? '[name].bundle.js' : '[name].[chunkhash:8].min.js',
path: distOutPutPath,
publicPath: routerType === 'history' ? '/' : './',
},
performance: {
hints: !isBuild ? false : 'warning'
},
watchOptions: {
poll: true,
ignored: /node_modules/,
},
devtool: isBuild ? 'cheap-module-source-map' : '#source-map',
devServer: {
hot: true,
publicPath: '/',
historyApiFallback: true,
},
resolve: {
// DefinePlugin的一种开发变量注入的替代方案 编译时不同环境加在不同代码文件的方案
extensions: [`.${nodeEnv}.js`, `.${routerType}.js`, '.js'],
alias: {
"star-web": path.resolve(__dirname, './'),
},
},
module: {
rules: [{
test: /\.jsx?$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
'transform-class-properties',
'syntax-dynamic-import',
["import", { "libraryName": "star-web", "libraryDirectory": "src/components"}]
],
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: loader => [
require('postcss-import')({ root: loader.resourcePath }),
require('autoprefixer')({
browsers: [
'> 0.01%',
],
}),
],
},
}],
},
{
test: /\.(png|jpg|jpeg|gif|woff)$/,
loader: 'url-loader?limit=6144&name=imgs/[path][name].[ext]',
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=fonts/[name].[ext]',
},
{
test: /\.less$/,
use: [{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
options: {
plugins: loader => [
require('postcss-import')({ root: loader.resourcePath }),
require('autoprefixer')({
browsers: [
'> 0.01%',
],
}),
],
},
},
{
loader: 'less-loader',
options: {
javascriptEnabled: true,
},
}],
}],
},
plugins,
};
};