-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
86 lines (78 loc) · 2.1 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
const path = require('path')
const fs = require('fs')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const WebpackImagesResizer = require('webpack-images-resizer')
const package = require('./package.json')
const mainFile = 'background.js'
const files = fs.readdirSync(path.join(__dirname, 'icons'))
const sizes = [16, 32, 48, 128]
let resizers = []
sizes.forEach(size => {
let icons = []
files.forEach(file => {
const parsed = path.parse(file)
icons.push({
src: path.resolve(__dirname, `icons/${parsed.name}${parsed.ext}`),
dest: `icons/${parsed.name}/${size}${parsed.ext}`,
})
})
resizers.push(new WebpackImagesResizer(icons, { width: size, height: size }))
})
module.exports = {
mode: 'production',
output: {
path: path.resolve(__dirname, 'dist'),
filename: mainFile,
},
plugins: [
new CleanWebpackPlugin(), // delete dist folder
new webpack.DefinePlugin({
__VERSION__: JSON.stringify(package.version),
__DESCRIPTION__: JSON.stringify(package.description),
}),
new CopyWebpackPlugin({
patterns: [
// copy version and description from package.json to manifest.json
{
context: 'src',
from: 'manifest.json',
transform(content, path) {
// copy-webpack-plugin passes a buffer
let manifest = JSON.parse(content.toString())
manifest.version = package.version
manifest.description = package.description
manifest.background.service_worker = mainFile
// pretty print to JSON with tabs
return JSON.stringify(manifest, null, '\t')
}
},
// copy popup
{
context: 'src',
from: 'popup.*',
transform(content, path) {
return content.toString()
.replaceAll(
/..\/assets\/(.*)\.(.*)/ig,
'assets/$1.$2')
.replaceAll(
/..\/icons\/(.*)\.(.*)/ig,
'icons/$1/128.$2')
}
},
// copy assets
{
from: 'assets',
to: 'assets',
}
]
}),
// copy icons
...resizers,
],
optimization: {
minimize: process.env.NODE_ENV == 'production'
},
}