-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathwebpack.service.config.mjs
More file actions
213 lines (196 loc) · 5.62 KB
/
webpack.service.config.mjs
File metadata and controls
213 lines (196 loc) · 5.62 KB
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
import PACKAGE from './package.json' with { type: 'json' }
import PublicKey from './public_key.json' with { type: 'json' }
import baseConfig from './webpack.common.config.mjs'
import {
getTarget,
isProduction as isProductionMode,
} from './webpack/utils.mjs'
import { WebextI18nPlugin } from '@media-harvest/webext-i18n-loader'
import browserslist from 'browserslist'
import CopyPlugin from 'copy-webpack-plugin'
import { resolve } from 'path'
import semver from 'semver'
import { merge } from 'webpack-merge'
const { version } = PACKAGE
/**
* @template T {Record<string, unknown>}
* @param {T} manifest
* @param {boolean} isLegacy
* @returns T
*/
const appendDevelopmentManifestAttributes = (manifest, isLegacy) => {
return {
...manifest,
...{
web_accessible_resources: isLegacy
? ['*.map']
: [
{
resources: ['*.map'],
matches: ['<all_urls>'],
},
],
},
}
}
/**
* @param {unknown} manifest
* @param {string} addOnId
* @param {string} updateUrl
* @returns
*/
const appendFirefoxSpecificManifestAttributes = (
manifest,
addOnId,
updateUrl
) => {
const browsers = browserslist()
const firefoxVersions = browsers
.filter(browser => browser.startsWith('firefox'))
.map(browser => browser.replace('firefox ', ''))
.map(browser => parseInt(browser, 10))
const minFirefoxVersion = semver.minVersion(
JSON.stringify(Math.min(...firefoxVersions)),
{
loose: true,
}
)
return {
...manifest,
...{
browser_specific_settings: {
gecko: {
id: addOnId,
strict_min_version: `${minFirefoxVersion.major}.${minFirefoxVersion.minor}`,
...(updateUrl ? { update_url: updateUrl } : {}),
},
},
},
}
}
/**
* @param {unknown} manifest
* @param {string} addOnId
* @param {string} updateUrl
* @returns
*/
const appendChromiumMinimumVersion = manifest => {
const browsers = browserslist()
const chromeVersions = browsers
.filter(browser => browser.startsWith('chrome'))
.map(browser => browser.replace('chrome ', ''))
.map(browser => parseInt(browser, 10))
const minChromeVersion = semver.minVersion(
JSON.stringify(Math.min(...chromeVersions)),
{
loose: true,
}
)
return {
...manifest,
minimum_chrome_version: minChromeVersion.major.toString(),
}
}
/**
* Webpack configuration function
* @param {Record<string, boolean | string>} env - Environment variables passed to webpack
* @param {import('webpack').WebpackOptionsNormalized} argv - Webpack CLI arguments
* @returns {import('webpack').Configuration} Webpack configuration object
*/
export default (env, argv) => {
const isProduction = isProductionMode(argv)
const VERSION = version
const BROWSER = getTarget(env)
const VERSION_NAME = `${VERSION} (${BROWSER})`
const isSelfSign = 'self-sign' in env
const isFirefox = BROWSER === 'firefox'
const isChrome = BROWSER === 'chrome'
const isEdge = BROWSER === 'edge'
const isChromium = isChrome || isEdge
return merge(baseConfig(env, argv), {
name: 'service',
output: {
chunkFormat: false,
},
entry: {
sw: resolve('./src/serviceWorker/sw.ts'),
pages: resolve('./src/pages/index.tsx'),
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
},
],
},
plugins: [
new WebextI18nPlugin({
poDir: resolve(process.cwd(), 'locales'),
}),
new CopyPlugin({
patterns: [
{
from: isChromium ? 'manifest.json' : 'manifest_firefox_v3.json',
context: 'src',
to: 'manifest.json',
transform: content => {
let manifest = JSON.parse(content.toString())
manifest['version'] = VERSION
manifest['version_name'] = VERSION_NAME
if (!isProduction) {
manifest = appendDevelopmentManifestAttributes(manifest)
}
if (isChrome && isProduction) {
manifest['key'] = PublicKey.chrome
}
if (isEdge && !isProduction) {
manifest['key'] = PublicKey.edge
}
if (isChromium) {
manifest = appendChromiumMinimumVersion(manifest)
}
if (isFirefox) {
if (isProduction) {
if (isSelfSign) {
manifest = appendFirefoxSpecificManifestAttributes(
manifest,
'mediaharvest@mediaharvest.app',
'https://release.mediaharvest.app/gecko/update.json'
)
} else {
manifest = appendFirefoxSpecificManifestAttributes(
manifest,
'mediaharvest@addons.mozilla.org'
)
}
} else {
manifest = appendFirefoxSpecificManifestAttributes(
manifest,
'mediaharvest@development'
)
}
}
return Buffer.from(JSON.stringify(manifest))
},
},
{
from: 'assets/icons/*.png',
context: 'src',
to: 'assets/icons/[name][ext]',
},
{
from: 'pages/*.html',
context: 'src',
to: '[name][ext]',
},
{
from: 'static/**/*',
context: 'src',
to: 'static/[name][ext]',
},
],
}),
],
})
}