forked from electerm/electerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
222 lines (219 loc) · 5.45 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require('dotenv').config()
const webpack = require('webpack')
const os = require('os')
const { identity } = require('lodash')
const HappyPack = require('happypack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const express = require('express')
const path = require('path')
const pack = require('./package.json')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const { env } = process
const git = require('git-rev-sync')
const packThreadCount = env.packThreadCount
? parseInt(env.packThreadCount)
: os.cpus().length
const devPort = env.devPort || 5570
const host = env.host || 'localhost'
const theme = require('@ant-design/dark-theme').default
const happyThreadPool = packThreadCount === 0
? null
: HappyPack.ThreadPool({ size: packThreadCount })
const happyConf = {
loaders: ['babel-loader'],
threadPool: happyThreadPool,
verbose: true
}
const version = pack.version + '-' + git.long()
const isProd = env.NODE_ENV === 'production'
const extractTextPlugin1 = new MiniCssExtractPlugin({
filename: 'css/[name].styles.css'
})
const pug = {
loader: 'pug-html-loader',
options: {
data: {
version,
_global: {
version
}
}
}
}
const stylusSettingPlugin = new webpack.LoaderOptionsPlugin({
test: /\.styl$/,
stylus: {
preferPathResolver: 'webpack'
},
'resolve url': false
})
const from = path.resolve(
__dirname,
'node_modules/@electerm/electerm-resource/tray-icons'
)
const to1 = path.resolve(
__dirname,
'work/app/assets/images'
)
var config = {
mode: 'development',
entry: {
electerm: './src/client/entry/index.jsx',
basic: './src/client/entry/basic.jsx',
index: './src/views/index.pug'
},
output: {
path: path.resolve(__dirname, 'work/app/assets'),
filename: 'js/[name].' + version + '.js',
publicPath: '/',
chunkFilename: 'js/[name].' + version + '.js',
libraryTarget: 'var'
},
externals: {
react: 'React',
'react-dom': 'ReactDOM',
zmodem: 'Zmodem',
lodash: '_'
},
target: 'electron-renderer',
watch: true,
resolve: {
extensions: ['.js', '.jsx', '.ts', '.json'],
alias: {
client: path.resolve(__dirname, 'src/client'),
node_modules: path.resolve(__dirname, 'node_modules'),
'app-common': path.resolve(__dirname, 'src/app/common')
}
},
resolveLoader: {
modules: [
path.resolve(__dirname, 'src/client/loaders'),
path.join(process.cwd(), 'node_modules')
]
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [packThreadCount === 0 ? 'babel-loader?cacheDirectory' : 'happypack/loader?cacheDirectory']
},
{
test: /\.styl$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: '../'
}
},
'css-loader',
'stylus-loader'
]
},
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: '../'
}
},
{
loader: 'css-loader'
},
{
loader: 'less-loader',
options: {
javascriptEnabled: true,
modifyVars: theme
}
}
]
},
{
test: /xterm\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: '../'
}
},
'css-loader'
]
},
{
test: /\.(png|jpg|svg)$/,
use: ['url-loader?limit=1&name=images/[name].[ext]']
},
{
test: /\.pug$/,
use: [
'file-loader?name=index.html',
'concat-loader',
'extract-loader',
'html-loader',
pug
]
}
]
},
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin({})
]
},
devtool: '#eval-source-map',
plugins: [
new webpack.HotModuleReplacementPlugin(),
stylusSettingPlugin,
packThreadCount === 0 ? null : new HappyPack(happyConf),
extractTextPlugin1
].filter(identity),
devServer: {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
contentBase: path.join(__dirname, 'work/app/assets/'),
historyApiFallback: true,
hot: true,
inline: true,
host,
port: devPort,
before: (app) => {
app.use('/node_modules', express.static(
path.resolve(__dirname, './node_modules'), { maxAge: '170d' })
)
}
}
}
if (isProd) {
config.plugins = [
packThreadCount === 0 ? null : new HappyPack(happyConf),
extractTextPlugin1,
stylusSettingPlugin,
new CopyWebpackPlugin([{
from,
to: to1,
force: true
}], {})
]
config.optimization = {
minimize: true
}
config.mode = 'production'
delete config.watch
delete config.devtool
delete config.devServer
}
module.exports = config