-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-dev-config.js
108 lines (102 loc) · 2.89 KB
/
webpack-dev-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
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import precss from 'precss';
import autoprefixer from 'autoprefixer';
import px2rem from 'postcss-pxtorem';
const path = require('path');
const find = require('find');
const files = find.fileSync('./src/js/');
const entrys = {};
const entrysArr = [];
const confEntry = [];
const re = /[\w\W]*src([\w\W]+)\.js$/;
for (let i = 0; i < files.length; i++) {
if (/\.entry\.js$/.test(files[i])) {
const filei = files[i].replace(re, '$1').replace('/js/page/', '');
entrys[filei] = [
'./webpack-public-path',
'react-hot-loader/patch',
'webpack-hot-middleware/client?reload=true',
`${__dirname}/${files[i]}`
];
entrysArr.push(filei);
const confe = `src/${files[i]}`;
confEntry.push(path.resolve(__dirname, confe));
}
}
const config = {
debug: true,
devtool: 'cheap-module-eval-source-map',
noInfo: true,
entry: entrys,
target: 'web',
output: {
path: `${__dirname}/src`,
publicPath: '',
filename: '[name].js',
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
__DEV__: true,
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new ExtractTextPlugin('[name].css')
],
resolve: {
modulesDirectories: ['node_modules', path.join(__dirname, '../node_modules')],
extensions: ['', '.web.js', '.js', '.jsx', '.json', '.less'],
alias: {
app: path.resolve(__dirname, 'src/js'),
style: path.resolve(__dirname, 'src/styles'),
},
},
module: {
loaders: [
{
test: /\.jsx?$/,
loaders: ['babel'],
exclude: /node_modules/,
},
{
test: /\.(less|css)$/,
exclude: /\.mod\.(less|css)/,
loader: ExtractTextPlugin.extract('style', 'css!postcss?parser=postcss-less')
},
{
test: /\.mod\.(less|css)$/,
loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss?parser=postcss-less')
},
{
test: /\.(otf|eot|svg|ttf|woff|woff2).*$/,
loader: 'url?limit=10000',
},
{
test: /\.(gif|jpe?g|png|ico)$/,
loader: 'url?limit=10000',
},
],
},
postcss: () => [precss, autoprefixer],
};
// 根据入口js文件生成对应的html文件
for (let j = 0; j < entrysArr.length; j++) {
const pathname = path.basename(entrysArr[j]).split('.')[0];
const conf = {
filename: `${pathname}.html`,
template: './src/template.html',
inject: 'body',
favicon: './favicon.ico',
title: pathname,
hash: true,
minify: {
removeComments: true,
collapseWhitespace: true,
},
chunks: [entrysArr[j]],
};
config.plugins.push(new HtmlWebpackPlugin(conf));
}
export default config;