This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
webpack.config.js
179 lines (165 loc) · 4.57 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// bundle forked scripts with webpack
// as in production, with asar, node_modules are not accessible in forked scripts
const path = require('path');
const fs = require('fs-extra');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const packageJson = require('./package.json');
const electronMainSrcPath = path.join(__dirname, 'src', 'electron');
const externals = {};
Object.keys(packageJson.dependencies)
.forEach((name) => {
externals[name] = `commonjs ${name}`;
});
const getPreloadScriptsConfig = () => {
const entry = {
'view-preload': path.join(electronMainSrcPath, 'libs', 'view-preload', 'index.js'),
'adblocker-electron-preload': path.join(__dirname, 'node_modules', '@cliqz', 'adblocker-electron-preload', 'dist', 'es6', 'preload.js'),
};
fs.readdirSync(path.join(electronMainSrcPath, 'windows'))
.filter((fileName) => fileName.endsWith('-preload.js') && fileName !== 'shared-preload.js')
.forEach((fileName) => {
entry[fileName.replace('.js', '')] = path.join(electronMainSrcPath, 'windows', fileName);
});
const plugins = [];
return {
mode: process.env.NODE_ENV || 'production',
node: {
global: false,
__filename: false,
__dirname: false,
},
externals,
entry,
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
target: 'electron-renderer',
output: {
path: path.join(__dirname, 'build'),
filename: '[name].js',
},
devtool: false,
plugins,
};
};
const getElectronMainConfig = () => {
const plugins = [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['libs'],
cleanAfterEveryBuildPatterns: [],
}),
new webpack.DefinePlugin({
'process.env.ELECTRON_APP_SENTRY_DSN': JSON.stringify(process.env.ELECTRON_APP_SENTRY_DSN),
'process.env.ELECTRON_APP_GOOGLE_API_KEY': JSON.stringify(process.env.ELECTRON_APP_GOOGLE_API_KEY),
}),
];
const patterns = [];
if (process.platform === 'win32') {
patterns.push({
from: path.join(__dirname, 'node_modules', 'regedit', 'vbs'),
to: path.join(__dirname, 'build', 'vbs'),
});
}
if (patterns.length > 0) {
plugins.push(new CopyPlugin({ patterns }));
}
return {
mode: process.env.NODE_ENV || 'production',
node: {
global: false,
__filename: false,
__dirname: false,
},
externals,
entry: {
electron: path.join(electronMainSrcPath, 'electron.js'),
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
target: 'electron-main',
output: {
path: path.join(__dirname, 'build'),
filename: '[name].js',
},
devtool: 'source-map',
plugins,
};
};
const getRecipeConfig = () => {
const plugins = [];
const entry = {};
fs.readdirSync(path.join(electronMainSrcPath, 'libs', 'view-preload', 'recipes'))
.filter((fileName) => fileName !== 'index.js')
.forEach((fileName) => {
entry[fileName.replace('.js', '')] = path.join(electronMainSrcPath, 'libs', 'view-preload', 'recipes', fileName);
});
return {
mode: process.env.NODE_ENV || 'production',
node: {
global: false,
__filename: false,
__dirname: false,
},
externals,
entry,
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
target: 'web',
output: {
path: path.join(__dirname, 'build', 'recipes'),
filename: '[name].js',
},
devtool: false,
plugins,
};
};
module.exports = [getElectronMainConfig(), getPreloadScriptsConfig(), getRecipeConfig()];