-
Notifications
You must be signed in to change notification settings - Fork 83
/
webpack.config.js
183 lines (177 loc) · 7.15 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
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
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 ExtensionReloader = require('webpack-ext-reloader');
const locateContentScripts = require('./utils/locateContentScripts');
const Dotenv = require('dotenv-webpack');
const TerserPlugin = require("terser-webpack-plugin");
const sourceRootPath = path.join(__dirname, 'src');
const archivePath = path.join(__dirname, '..', 'rule-server', 'dist', 'static');
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 extensionReloader = nodeEnv === "watch" || nodeEnv === "watch_local" ? new ExtensionReloader({
// port: 9128,
// reloadPage: true,
// entries: {
// background: 'background',
// extensionPage: ['popup', 'options', 'devtools', 'devtoolsPanel', 'devtoolsSubpanel'],
// contentScript: Object.keys(contentScripts),
// }
// }) : () => { this.apply = () => { } };
const dotenv_path = nodeEnv === "production" ? ".env_production" : nodeEnv === "watch_local" ? "./.env_local" : "./.env_development";
console.log(`[INFO] Using environment ${dotenv_path}`);
module.exports = {
watch: nodeEnv === 'watch' || nodeEnv === "watch_local",
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'),
devtools: path.join(sourceRootPath, 'ts', 'devtools', 'index.tsx'),
devtoolsMain: path.join(sourceRootPath, 'ts', 'devtools', 'indexMain.tsx'),
devtoolsElements: path.join(sourceRootPath, 'ts', 'devtools', 'indexElements.tsx'),
viewKCM: path.join(sourceRootPath, 'ts', 'contentScripts', 'viewKCM.ts'),
viewInspect: path.join(sourceRootPath, 'ts', 'contentScripts', 'viewInspect.ts'),
usingAC: path.join(sourceRootPath, 'ts', 'docs', 'usingAC.tsx'),
...contentScripts,
quickGuideAC: path.join(sourceRootPath, 'ts', 'docs', 'quickGuide.tsx'),
...contentScripts,
},
output: {
path: distRootPath,
filename: '[name].js',
},
optimization: nodeEnv.includes("watch") ? {
minimize: false
} : {
splitChunks: {
maxSize: 3500000,
chunks(chunk) {
return chunk.name !== "background" && chunk.name !== "viewKCM" && chunk.name !== "viewInspect"; // Don't chunk the background script. Chrome doesn't register the service_worker listeners if we do.
}
},
minimizer: [new TerserPlugin({
exclude: /view(Inspect|KCM)/,
})]
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json'],
},
module: {
rules: [
{ test: /\.(js|ts|tsx)?$/, loader: "ts-loader", exclude: /node_modules/ },
{
test: /\.scss$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
},
{
test: /\.svg$/,
use: ['@svgr/webpack', 'url-loader'],
}
]
},
plugins: [
new Dotenv({
path: dotenv_path
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'options.html'),
inject: 'body',
filename: 'options.html',
title: 'Settings - IBM Accessibility Checker',
chunks: ['options']
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'popup.html'),
inject: 'body',
filename: 'popup.html',
title: 'Accessibility Checker Extension - Popup Page',
chunks: ['popup']
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'devtools.html'),
inject: 'body',
filename: 'devtools.html',
title: 'Accessibility Checker Extension',
chunks: ['devtools']
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'devtoolsMain.html'),
inject: 'body',
filename: 'devtoolsMain.html',
title: 'Accessibility Checker Extension',
chunks: ['devtoolsMain']
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'devtoolsElements.html'),
inject: 'body',
filename: 'devtoolsElements.html',
title: 'Accessibility Checker Extension',
chunks: ['devtoolsElements']
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'reports.html'),
inject: 'body',
filename: 'reports.html',
title: 'Accessibility Checker Extension',
chunks: ['reports']
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'usingAC.html'),
inject: 'body',
filename: 'usingAC.html',
title: 'User guide - IBM Accessibility Checker',
chunks: ['usingAC']
}),
new HtmlWebpackPlugin({
template: path.join(sourceRootPath, 'html', 'quickGuideAC.html'),
inject: 'body',
filename: 'quickGuideAC.html',
title: 'Quick guide - IBM Accessibility Checker',
chunks: ['quickGuideAC']
}),
new CopyWebpackPlugin({
patterns: [
{
from: path.join(sourceRootPath, 'assets'),
to: path.join(distRootPath, 'assets')
},
{
from: path.join(sourceRootPath, 'manifest.json'),
to: path.join(distRootPath, 'manifest.json'),
toType: 'file',
},
{
from: path.join(archivePath, "archives.json"),
to: path.join(distRootPath, "archives.json"),
toType: 'file'
},
{
from: path.join(archivePath, "archives"),
to: path.join(distRootPath, "archives"),
globOptions: {
ignore: [
path.join("**", "ace-*.js"),
path.join("**", "doc"),
]
}
}
]}),
new webpack.DefinePlugin({
'NODE_ENV': JSON.stringify(nodeEnv),
'WEB_BROWSER': JSON.stringify(webBrowser),
}),
// extensionReloader,
],
}
if (nodeEnv === 'production') {
module.exports.plugins.push(new CleanWebpackPlugin({ verbose: true, dry: false }));
}