-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
webpack.config.ts
213 lines (199 loc) · 6.35 KB
/
webpack.config.ts
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 * as path from 'node:path'
import * as s from 'superstruct'
import type { Configuration } from 'webpack'
const DashboardPlugin = require('webpack-dashboard/plugin')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const Dotenv = require('dotenv-webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const IN_PRODUCTION_MODE = process.env.NODE_ENV === 'production'
type Target = 'default' | 'firefox' | 'safari'
function createConfig({ envTarget }: { envTarget: Target }) {
const outputPath = {
default: path.resolve(__dirname, 'dist'),
firefox: path.resolve(__dirname, 'dist-firefox'),
safari: path.resolve(__dirname, 'Safari/Gitako/Gitako Extension/Resources'),
}[envTarget]
const plugins = [
new DashboardPlugin(),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
(() => {
const sManifest = s.type({
version: s.optional(s.string()),
description: s.optional(s.string()),
author: s.optional(s.string()),
homepage_url: s.optional(s.string()),
manifest_version: s.number(),
background: s.type({
scripts: s.optional(s.array(s.string())),
service_worker: s.string(),
}),
web_accessible_resources: s.array(
s.type({
resources: s.array(s.string()),
matches: s.array(s.string()),
}),
),
})
return {
from: './src/manifest.json',
to: 'manifest.json',
transform(content: string) {
const {
version,
description,
author,
homepage: homepage_url,
} = require('./package.json')
const manifest = JSON.parse(content)
s.assert(manifest, sManifest)
manifest.version = version
manifest.description = description
manifest.author = author
manifest.homepage_url = homepage_url
switch (envTarget) {
case 'safari': {
// Disable custom domains for Safari
Reflect.deleteProperty(manifest, 'optional_permissions')
Reflect.deleteProperty(manifest, 'background')
break
}
case 'firefox': {
manifest.manifest_version = 3
// Firefox does not support service worker
Reflect.set(manifest.background, 'scripts', [manifest.background.service_worker])
Reflect.deleteProperty(manifest.background, 'service_worker')
break
}
}
if (!IN_PRODUCTION_MODE) {
// enable source mapping while developing
manifest.web_accessible_resources.push({
resources: ['*.map'],
matches: ['*://*/*'],
})
}
return JSON.stringify(manifest)
},
}
})(),
{
from: './src/assets/icons/*',
to: 'icons/[name][ext]',
},
{
from: './vscode-icons/icons/*',
to: 'icons/vscode/[name][ext]',
},
{
from: 'node_modules/webextension-polyfill/dist/browser-polyfill.js',
to: 'browser-polyfill.js',
},
{
from: './src/firefox-shim.js',
to: 'firefox-shim.js',
},
],
}),
new ForkTsCheckerWebpackPlugin(),
new Dotenv(),
new MiniCssExtractPlugin(),
new webpack.DefinePlugin({
'process.env.VERSION': JSON.stringify(process.env.VERSION),
}),
]
const analyze = process.env.ANALYZE !== undefined
if (analyze) {
const webpackBundleAnalyzer = require('webpack-bundle-analyzer')
plugins.push(new webpackBundleAnalyzer.BundleAnalyzerPlugin())
console.log(`BundleAnalyzerPlugin added`)
}
const srcPath = path.resolve(__dirname, 'src')
const webpackConfig: Configuration = {
entry: {
content: './src/content.tsx',
background: './src/background.ts',
},
devtool: IN_PRODUCTION_MODE ? 'source-map' : 'inline-source-map',
mode: IN_PRODUCTION_MODE ? 'production' : 'development',
output: {
path: outputPath,
filename: '[name].js',
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
modules: [srcPath, 'node_modules'],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'babel-loader',
include: [srcPath],
exclude: /node_modules/,
sideEffects: false,
},
{
test: /\.[cm]?js$/,
loader: 'babel-loader',
// Transpile as least files under node_modules
include: /node_modules\/(webext-.*|superstruct)\/.*\.[cm]?js$/,
options: {
cacheDirectory: true,
},
},
{
test: /\.scss$/,
loader: MiniCssExtractPlugin.loader,
include: [srcPath],
},
{
test: /\.scss$/,
loader: 'css-loader',
include: [srcPath],
},
{
test: /\.scss$/,
loader: 'sass-loader',
include: [srcPath],
},
{
test: /\.svg$/,
resourceQuery: /inline/,
loader: 'url-loader',
},
{
test: /\.csv$/,
loader: 'raw-loader',
},
{
test: /\.json$/,
loader: 'json-loader',
include: [srcPath],
},
{
test: /\.png$/,
loader: 'url-loader',
include: [srcPath],
},
],
},
plugins,
}
return webpackConfig
}
const gitakoTarget = process.env.GITAKO_TARGET ?? 'default'
const enabledTargets = (['default', 'firefox', 'safari'] as Target[]).filter(
target => !gitakoTarget || gitakoTarget === target,
)
const configs = enabledTargets.map(envTarget => createConfig({ envTarget }))
// Enable parallelism for faster build
// https://webpack.js.org/configuration/configuration-types/#parallelism
Object.assign(configs, {
parallelism: true,
})
module.exports = configs