This repository was archived by the owner on Nov 15, 2024. It is now read-only.
forked from lmichelin/open-github-links-in-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
103 lines (97 loc) · 3.08 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
const path = require("path")
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const TerserJSPlugin = require("terser-webpack-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin")
const ZipWebpackPlugin = require("zip-webpack-plugin")
const ExtensionReloader = require("webpack-extension-reloader")
const generatePlugins = (env, mode, browser) => {
const plugins = [
new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }),
new CopyWebpackPlugin({
patterns: [
{ from: "icons", to: "icons" },
{
from: "manifest.json",
transform: content => {
const manifest = {
description: process.env.npm_package_description,
version: process.env.npm_package_version,
...JSON.parse(content.toString()),
}
if (mode === "development" || env.test) {
manifest.content_scripts.forEach(script => {
script.all_frames = true // allow cypress tests
})
}
if (browser === "firefox") {
manifest.browser_specific_settings = {
gecko: { id: process.env.npm_package_geckoId },
}
}
return Buffer.from(JSON.stringify(manifest))
},
},
],
}),
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin({
template: "popup.html",
filename: "popup.html",
chunks: ["popup"],
}),
]
if (mode === "development") {
plugins.push(
new ExtensionReloader({
port: browser === "chrome" ? 9090 : 9091,
reloadPage: true,
manifest: "./src/manifest.json",
}),
)
}
if (mode === "production") {
plugins.push(
new ZipWebpackPlugin({
path: path.join(__dirname, "build"),
exclude: [/\.map$/],
filename: `${browser}.zip`,
}),
)
}
return plugins
}
const generateWebpackConfig = (env, mode, browser) => {
return {
stats: "minimal",
devtool: mode === "development" ? "inline-source-map" : "source-map",
watch: mode === "development",
context: path.join(__dirname, "src"),
output: { path: path.join(__dirname, `dist/${browser}`) },
entry: {
inject: "./inject.ts",
background: "./background.ts",
popup: "./popup.ts",
},
module: {
rules: [
{ test: /\.ts$/, loader: "ts-loader" },
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, "css-loader"] },
],
},
resolve: { extensions: [".js", ".ts"] },
optimization: {
minimizer: [
new TerserJSPlugin({ sourceMap: true, terserOptions: { output: { comments: false } } }),
new OptimizeCssAssetsPlugin(),
],
},
plugins: generatePlugins(env, mode, browser),
}
}
module.exports = (env = {}, argv) => [
generateWebpackConfig(env, argv.mode, "chrome"),
generateWebpackConfig(env, argv.mode, "firefox"),
]