-
Notifications
You must be signed in to change notification settings - Fork 32
/
webpack.js
executable file
·171 lines (160 loc) · 4.91 KB
/
webpack.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
#!/usr/bin/env node
/*
Copyright 2022 Marc Nuri San Felix
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* eslint-disable no-console */
const webpack = require('webpack');
const path = require('path');
const fsp = require('fs/promises');
const fs = require('fs');
const {APP_EVENTS, ELECTRONIM_VERSION} = require('./src/constants');
const BUNDLES_DIR = 'bundles';
const ENTRIES = [
// 'chrome-tabs',
'help'
// 'main',
// 'settings',
// 'tab-manager'
];
const LIB_DIR = 'lib';
const LIB_ENTRIES = [
'@fortawesome/fontawesome-free/css/all.css',
'bulma/css/bulma.css'
];
const bundle = webpack({
entry: ENTRIES.reduce((acc, entry) => {
acc[entry] = `/src/${entry}/preload.js`;
return acc;
}, {}),
output: {
filename: '[name].preload.js',
path: path.resolve(__dirname, BUNDLES_DIR),
// Extremely important to avoid runtime require calls that fail in the Electron's sandbox environment
chunkLoading: false
},
mode: 'development',
devtool: false,
target: 'electron-main', // Don't use 'electron-preload', Electron Sandbox takes care of proper security isolation
optimization: {
minimize: false,
runtimeChunk: false
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/
},
{
test: /\.(css)$/,
use: [
{
loader: 'style-loader',
options: {
injectType: 'styleTag',
insert: element => {
// eslint-disable-next-line prefer-const
let observer;
const callback = () => {
if (document && document.head) {
document.head.append(element);
observer.disconnect();
}
};
observer = new MutationObserver(callback);
observer.observe(document, {childList: true, subtree: true});
}
}
},
'css-loader'
]
},
{
test: /\.(svg|eot|ttf|woff|woff2)$/,
type: 'asset/inline'
}
]
},
plugins: [
new webpack.DefinePlugin({
APP_EVENTS: JSON.stringify(APP_EVENTS),
ELECTRONIM_VERSION: JSON.stringify(ELECTRONIM_VERSION)
})
]
});
const libBundle = webpack({
entry: LIB_ENTRIES.reduce((acc, entry) => {
acc[entry] = entry;
return acc;
}, {}),
output: {
filename: '[name].js',
assetModuleFilename: '[name][ext]',
path: path.resolve(__dirname, BUNDLES_DIR, LIB_DIR)
},
target: 'web',
module: {
rules: [
{
test: /\.(css)$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(svg|eot|ttf|woff|woff2)$/,
type: 'asset/resource'
}
]
}
});
const toPromise = async webpackBundle => new Promise((resolve, reject) => {
webpackBundle.run((err, stats) => {
if (err) {
reject(err);
} else {
resolve(stats);
}
});
});
const exec = async () => {
console.log('⌛ Starting webpack bundling process...');
await Promise.all(
ENTRIES.map(entry => fsp.access(path.resolve(__dirname, 'src', entry, 'preload.js'), fs.constants.R_OK)),
LIB_ENTRIES.map(entry => fsp.access(path.resolve(__dirname, 'node_modules', entry), fs.constants.R_OK))
);
console.log('✅ Required files exist');
const bundlesDir = path.resolve(__dirname, BUNDLES_DIR);
await Promise.all([bundlesDir].map(dir => fsp.rm(dir, {recursive: true, force: true})));
console.log('🧹 Cleaned previous build...');
const bundleStats = await toPromise(bundle);
console.log(`⏱️ Webpack bundling completed in ${bundleStats.endTime - bundleStats.startTime}ms`);
const libBundleStats = await toPromise(libBundle);
console.log(`⏱️ Webpack lib bundling completed in ${libBundleStats.endTime - libBundleStats.startTime}ms`);
if (bundleStats.hasErrors()) {
console.error('⚠️ Bundling errors found:');
bundleStats.compilation.errors.forEach(error => console.error(error));
} else if (libBundleStats.hasErrors()) {
console.error('⚠️ Bundling errors found for lib:');
libBundleStats.compilation.errors.forEach(error => console.error(error));
} else {
console.log('🚀 Bundling completed successfully');
console.log(bundleStats.toString({colors: true}));
}
};
exec()
.then(() => {
process.exit(0);
})
.catch(err => {
console.error('Error while creating webpack bundles');
console.error(err);
process.exit(1);
});