-
Notifications
You must be signed in to change notification settings - Fork 15
/
webpack.config.js
120 lines (100 loc) · 2.81 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
var webpack = require('webpack')
var path = require('path')
var fs = require('fs')
process.noDeprecation = true
// 遍历demo下面所有目录层级,使用所有js作为entry
function getDemoEntry(dirPath, entries) {
var reg = /.js$/
var pageDir = fs.readdirSync(dirPath) || []
for (var j = 0; j < pageDir.length; j++) {
var filePath = path.resolve(dirPath, pageDir[j])
var fileStat = fs.statSync(filePath)
if (fileStat.isFile()) {
if (reg.test(pageDir[j])) {
var name = pageDir[j].replace('.js', '')
if (entries[name]) {
console.log('\x1b[31m')
console.log('entry name 冲突: ' + name)
console.log('\t', entries[name][0])
console.log('\t', filePath)
console.log('\x1b[0m')
}
entries[name] = [filePath]
}
} else if (fileStat.isDirectory()) {
getDemoEntry(filePath, entries)
}
}
return entries
}
var ENTRY = process.env.ENTRY
var entry = {}
if (ENTRY) {
entry[ENTRY] = ['./demo/' + ENTRY + '.js']
} else {
// 查找demo目录下所有的js文件作为entry
getDemoEntry(path.resolve(__dirname, 'demo/'), entry)
}
var plugins, devtool, output, mode
if (process.env.NODE_ENV === 'production') {
console.log('publishing')
entry = {
v3: [path.resolve('./src/v3.ts')],
v2: [path.resolve('./src/v2.js')],
}
plugins = [
new webpack.NoEmitOnErrorsPlugin(), // 出错时不发布
]
devtool = 'source-map'
output = {
path: path.resolve('./'),
publicPath: '/', // 为使内网可访问, 不指明host
filename: '[name].js',
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: 'this',
}
mode = 'production'
} else {
console.log('deving')
plugins = [
new webpack.NoEmitOnErrorsPlugin(), // 出错时不发布
]
devtool = 'inline-source-map'
output = {
filename: '[name].demo.js',
publicPath: '/static/',
}
mode = 'development'
}
var config = {
entry: entry,
output: output,
devtool: devtool,
plugins: plugins,
mode: mode,
resolve: {
alias: {
src: path.join(__dirname, 'src'),
},
extensions: ['.js', '.scss', '.css'],
},
module: {
rules: [
{
test: /\.ts$/,
include: /src|demo/,
use: 'ts-loader',
},
{
test: /\.js$/,
include: /src|demo/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
],
},
}
module.exports = config