-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
webpack.config.js
114 lines (107 loc) · 3.91 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
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const {CheckerPlugin} = require('awesome-typescript-loader')
const ExtensionReloader = require('webpack-extension-reloader')
const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin')
const locateContentScripts = require('./utils/locateContentScripts')
const sourceRootPath = path.join(__dirname, 'src')
const contentScriptsPath = path.join(sourceRootPath, 'ts', 'contentScripts')
const distRootPath = path.join(__dirname, 'dist')
const nodeEnv = process.env.NODE_ENV ? process.env.NODE_ENV : 'development'
const webBrowser = process.env.WEB_BROWSER ? process.env.WEB_BROWSER : 'chrome'
const contentScripts = locateContentScripts(contentScriptsPath)
const noopPlugin = () => {
this.apply = () => {}
}
const extensionReloader =
nodeEnv === 'watch'
? new ExtensionReloader({
port: 9128,
reloadPage: true,
entries: {
background: 'background',
extensionPage: ['popup', 'options'],
contentScript: Object.keys(contentScripts),
},
})
: noopPlugin
const addUnsafeEvalToManifestPlugin = new ReplaceInFileWebpackPlugin([
{
dir: 'dist',
files: ['manifest.json'],
rules: [
{
search: /"content_security_policy": ".*"/,
replace: "\"content_security_policy\": \"script-src 'self' 'unsafe-eval'; object-src 'self'\"",
},
],
},
])
const developmentModeManifestPlugin = nodeEnv === 'production' ? noopPlugin : addUnsafeEvalToManifestPlugin
module.exports = {
watch: nodeEnv === 'watch',
entry: {
background: path.join(sourceRootPath, 'ts', 'background', 'index.ts'),
options: path.join(sourceRootPath, 'ts', 'options', 'index.tsx'),
popup: path.join(sourceRootPath, 'ts', 'popup', 'index.tsx'),
...contentScripts,
},
output: {
path: distRootPath,
filename: '[name].js',
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json'],
alias: {
// Enable absolute imports
src: path.resolve(__dirname, 'src/ts/'),
},
},
module: {
rules: [{test: /\.(js|ts|tsx)?$/, loader: 'awesome-typescript-loader', exclude: /node_modules/}],
},
plugins: [
new CheckerPlugin(),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'options.html'),
inject: 'body',
filename: 'options.html',
title: 'Web Extension Starter - Options Page',
chunks: ['options'],
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'popup.html'),
inject: 'body',
filename: 'popup.html',
title: 'Web Extension Starter - Popup Page',
chunks: ['popup'],
}),
new CopyWebpackPlugin(
[
{
from: path.join(sourceRootPath, 'assets'),
to: path.join(distRootPath, 'assets'),
test: /\.(jpg|jpeg|png|gif|svg)?$/,
},
{
from: path.join(sourceRootPath, 'manifest.json'),
to: path.join(distRootPath, 'manifest.json'),
toType: 'file',
},
],
{
copyUnmodified: true, // resolve conflict with `CleanWebpackPlugin`
}
),
developmentModeManifestPlugin,
new webpack.DefinePlugin({
NODE_ENV: JSON.stringify(nodeEnv),
WEB_BROWSER: JSON.stringify(webBrowser),
}),
extensionReloader,
new CleanWebpackPlugin(),
],
}