forked from ShieldBattery/ShieldBattery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
155 lines (145 loc) · 3.99 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
// Can't use ES6 imports in this file because this won't be running through Babel
require('./babel-register')
const makeConfig = require('./common.webpack.config.js').default
const path = require('path')
// Configuration for the web part of the electron process (the client/ scripts
// compiled for electron)
const webWebpackOpts = {
target: 'electron-renderer',
entry: {
bundle: './client/index.jsx',
},
output: {
chunkFilename: '[name].chunk.js',
filename: '[name].js',
path: path.join(__dirname, 'app', 'dist'),
publicPath: process.env.NODE_ENV !== 'production' ? 'http://localhost:5566/dist/' : '/dist/',
libraryTarget: 'commonjs2',
crossOriginLoading: 'anonymous',
},
plugins: [],
}
const webBabelOpts = {
babelrc: false,
configFile: false,
cacheDirectory: process.env.NODE_ENV !== 'production',
presets: [
'@babel/preset-react',
[
'@babel/preset-env',
{
targets: { electron: '13.0' },
modules: false,
useBuiltIns: 'usage',
corejs: 3,
},
],
['@babel/preset-typescript', { allExtensions: true, isTSX: true }],
],
plugins: [
['babel-plugin-styled-components'],
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-private-methods', { loose: true }],
['@babel/plugin-proposal-nullish-coalescing-operator'],
['@babel/plugin-proposal-optional-chaining'],
['babel-plugin-const-enum'],
['react-hot-loader/babel'],
],
}
if (process.env.NODE_ENV !== 'production') {
webWebpackOpts.entry.bundle = [
'webpack-hot-middleware/client?path=http://localhost:5566/__webpack_hmr',
webWebpackOpts.entry.bundle,
].flat()
}
const SB_SERVER = (() => {
if (process.env.SB_SERVER) {
return process.env.SB_SERVER
}
if (process.env.NODE_ENV === 'production') {
return 'https://shieldbattery.net'
}
try {
require('dotenv').config({ path: './server/.env' })
if (process.env.SB_CANONICAL_HOST) {
return process.env.SB_CANONICAL_HOST
}
} catch (err) {
// Intentionally empty, just means the server config isn't there/is broken for some reason
}
// Just use a "default" server address
return 'http://localhost:5555'
})()
console.error('Using a server of ' + SB_SERVER + ' by default')
const electronWeb = makeConfig({
webpack: webWebpackOpts,
babel: webBabelOpts,
mainEntry: 'bundle',
globalDefines: {
IS_ELECTRON: true,
},
envDefines: {
SB_SERVER: SB_SERVER ? JSON.stringify(SB_SERVER) : undefined,
},
})
// Configuration for the main process scripts of Electron (the app/ scripts)
const mainWebpackOpts = {
target: 'electron-main',
entry: {
index: './app/startup.js',
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'app', 'dist'),
libraryTarget: 'commonjs2',
},
plugins: [],
}
const mainBabelOpts = {
babelrc: false,
configFile: false,
cacheDirectory: process.env.NODE_ENV !== 'production',
presets: [
[
'@babel/preset-env',
{
targets: { electron: '13.0' },
modules: false,
useBuiltIns: 'usage',
corejs: 3,
},
],
['@babel/preset-typescript'],
],
plugins: [
'babel-plugin-transform-typescript-metadata',
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-private-methods', { loose: true }],
['babel-plugin-const-enum'],
],
}
const electronMain = makeConfig({
webpack: mainWebpackOpts,
babel: mainBabelOpts,
mainEntry: 'index',
globalDefines: {
IS_ELECTRON: true,
},
extraRules: [
{
test: /\.node$/,
use: [
{
loader: 'native-addon-loader',
options: {
name: './native/[name]-[hash].[ext]',
from: './native/',
},
},
],
},
],
})
module.exports = process.env.NODE_ENV === 'production' ? [electronWeb, electronMain] : electronWeb