-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
215 lines (205 loc) · 6.15 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
const path = require('path')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const WebpackOnBuildPlugin = require('on-build-webpack')
const WriteFilePlugin = require('write-file-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const chalk = require('chalk')
const zipFolder = require('zip-folder')
const fs = require('fs')
const manifestSrcPath = './src/manifest.json'
const manifestDistPath = './dist/manifest.json'
const manifest = require(manifestSrcPath)
const serverPort = 3031
// var PrepackWebpackPlugin = require('prepack-webpack-plugin').default;
// auto increaseVersion of manifest.json
function increaseVersion(package) {
if (increaseVersion.versionUpdated) return
increaseVersion.versionUpdated = true
let version = package.version
const max = 20
const vs = version.split('.').map(i => +i)
let len = vs.length
while (len--) {
if (++vs[len] < max) break
vs[len] = 0
}
package.version = vs.join('.')
}
const config = {
mode: process.env.NODE_ENV,
entry: {
// your entry file file (entry.ts or entry.js)
'background/index': ['./src/background/index.js'],
'popup/index': ['./src/popup/index.js'],
'options/index': ['./src/options/index.js'],
'content/index': ['./src/content/index.js']
},
notHotReload: [],
output: {
path: path.join(__dirname, './dist/'),
filename: '[name].js',
publicPath: '/'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
css: ExtractTextPlugin.extract({
use: 'css-loader',
fallback: 'vue-style-loader'
}),
scss: ExtractTextPlugin.extract({
use: ['css-loader', 'sass-loader'],
fallback: 'vue-style-loader'
}),
sass: ExtractTextPlugin.extract({
use: ['css-loader', 'sass-loader?indentedSyntax=1'],
fallback: 'vue-style-loader'
}),
less: ExtractTextPlugin.extract({
use: ['css-loader', 'less-loader'],
fallback: 'vue-style-loader'
})
}
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
use: 'css-loader',
fallback: 'style-loader'
})
},
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
// loader: 'file-loader'
loader: 'file-loader',
options: {
name: 'static/[name].[ext]?[hash]'
}
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
plugins: [
new CleanWebpackPlugin(['dist', 'ext.zip']),
// copy custom static assets
new CopyWebpackPlugin(
[{
from: path.resolve(__dirname, 'src/static/'),
to: path.resolve(__dirname, 'dist/static/')
},
{
context: path.resolve(__dirname, 'src/'),
from: '**/index.html',
to: path.resolve(__dirname, 'dist')
},
{
from: path.resolve(__dirname, 'src/_locales/'),
to: path.resolve(__dirname, 'dist/_locales/')
},
{
from: path.resolve(__dirname, 'src/manifest.json'),
to: path.resolve(__dirname, 'dist/manifest.json')
}
], {
ignore: ['**/.*', '**/*.map', '**/node_modules/*']
}
),
// extract css into its own file
new ExtractTextPlugin({
filename: '[name].css'
}),
new OptimizeCSSPlugin()
],
resolve: {
extensions: ['.ts', 'tsx', '.js', '.vue', '.json'],
alias: {
vue$: 'vue/dist/vue.esm.js',
// 'vue-i18n$': 'vue-i18n/dist/vue-i18n.min.js',
'@': path.resolve(__dirname, 'src')
}
}
}
if (process.env.NODE_ENV === 'production') {
delete config.notHotReload
// http://vue-loader.vuejs.org/en/workflow/production.html
config.plugins = (config.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
}),
new WebpackOnBuildPlugin(stats => {
increaseVersion(manifest)
try {
fs.writeFileSync(manifestSrcPath, JSON.stringify(manifest, null, 2))
fs.writeFileSync(manifestDistPath, JSON.stringify(manifest, null, 2))
} catch (e) {
console.log(
chalk.red('\n update manifest(dist) file error: ' + e.message)
)
}
console.log(chalk.cyan('\n manifest file updated successfully'))
zipFolder('./dist/', './ext.zip', err => {
if (err) {
console.log(chalk.red('Failed to zip dist folder ' + err))
} else {
console.log(chalk.cyan('An zip of ext is available in ./ext.zip'))
}
})
})
])
module.exports = config
// webpack(config, (err) => { if (err) throw err})
} else {
// config.devtool = "#cheap-module-eval-source-map"
config.devtool = 'sourcemap'
config.plugins = [new webpack.HotModuleReplacementPlugin()].concat(
config.plugins || []
)
config.plugins.push(new WriteFilePlugin())
config.plugins.push(new FriendlyErrorsPlugin())
const notHotReload = config.notHotReload || []
for (let entryName in config.entry) {
if (
config.entry.hasOwnProperty(entryName) &&
notHotReload.indexOf(entryName) === -1
) {
config.entry[entryName] = [
'webpack-dev-server/client?http://localhost:' + serverPort,
'webpack/hot/dev-server'
].concat(config.entry[entryName])
}
}
delete config.notHotReload
const compiler = webpack(config)
const server = new WebpackDevServer(compiler, {
hot: true,
contentBase: path.join(__dirname, 'dist'),
headers: { 'Access-Control-Allow-Origin': '*' }
})
server.listen(serverPort)
}