-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.config.js
112 lines (104 loc) · 3.32 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
const merge = require('webpack-merge');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpack = require('clean-webpack-plugin');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const path = require('path');
const fse = require('fs-extra');
const baseConfig = {
devtool: 'inline-source-map',
entry: {
vendor: ['d3'],
common: './app/common.ts'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/[name].[chunkhash:5].js'
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
module: {
rules: [
{
test: /\.ts$/,
use: {
loader: 'ts-loader'
}
},
{
test: /\.less$/,
use: ExtractTextWebpackPlugin.extract({
fallback: {
loader: 'style-loader',
options: {
singleton: true
}
},
use: [
{
loader: 'css-loader',
options: {
minimize: true,
localIdentName: '[path][name]_[local]_[hash:base64:5]'
}
},
{
loader: 'less-loader'
}
]
})
}
]
},
plugins: [
new ExtractTextWebpackPlugin({
filename: 'css/[name].[hash].css'
}),
new CleanWebpack(path.resolve(__dirname, 'dist')),
new webpack.optimize.CommonsChunkPlugin({ // 提取三方生成的代码, 包括模块代码
names: ['vendor'],
minChunks: Infinity
}),
new webpack.optimize.UglifyJsPlugin()
]
};
const generatePage = function ({
title = '',
entry = '',
template = './app/index.html',
name = '',
chunks = []
} = {}) {
return {
entry,
plugins: [
new HtmlWebpackPlugin({
chunks,
template,
title,
filename: name + '.html'
})
]
}
};
const appPaths = fse.readdirSync(path.resolve(__dirname, 'app'));
let appItemPath = '';
let myPages = [];
let appItemHtmlTemplate = '';
appPaths.map(function (item) {
appItemPath = path.resolve(__dirname, 'app', item, 'index.ts');
appItemHtmlTemplate = path.resolve(__dirname, 'app', item, 'index.html');
if(fse.pathExistsSync(appItemPath)) {
myPages.push(generatePage({
title: item,
entry: {
[item]: `./app/${item}/index.ts`
},
name: item,
chunks: [item, 'vendor', 'common'],
template: fse.pathExistsSync(appItemHtmlTemplate) ? path.resolve(__dirname, 'app', item, 'index.html') : './app/index.html',
}))
}
});
module.exports = merge([baseConfig].concat(myPages));