forked from vunb/electron-angular-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
166 lines (161 loc) · 5.48 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
const dev = process.env.NODE_ENV === "dev";
const path = require('path');
const Webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const exec = require('child_process').exec;
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin')
// Html-webpack-plugin configuration
const indexConfig = {
template: './src/index.hbs',
excludeChunks: ['electron'],
baseHref: dev ? '/' : './',
chunksSortMode: (chunk1, chunk2) => {
// Set the order of files injected (dependencies before app)
// https://github.com/jantimon/html-webpack-plugin/issues/481
let orders = ['corejs', 'zonejs', 'app'];
return orders.indexOf(chunk1.names[0]) - orders.indexOf(chunk2.names[0]);
}
};
// Clean-webpack-plugin configuration
const pathsToClean = [
'./dist/css',
'./dist/assets',
'./dist/templates'
];
let webpackConfig = {
mode: 'none',
// How source maps are generated : style of source mapping
devtool: dev ? 'eval-cheap-module-source-map' : false,
// Development server configuration
devServer: {
historyApiFallback: true,
// Execute custom middleware after all other middleware internally within the server
after() {
// Fix whitescreen bug on build with Electron BrowserWindow
exec('electron . --dev');
}
},
// Where webpack looks to start building the bundle
entry: {
'electron': './electron', // Electron entry point
'corejs': 'core-js/client/shim', // Angular dependency
'zonejs': 'zone.js/dist/zone', // Angular dependency
'app': './src/main.ts' // App entry point
},
// How the different types of modules within a project will be treated
module: {
rules: [
{
// All files with a '.ts' extension will be handled by ts-loader
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
},
{
// All files with a '.scss' extension will be handled by sass-loader
test: /\.s?css$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[hash:10].css'
}
},
'extract-loader',
{
loader: 'css-loader',
options: {
minimize: !dev
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: dev
}
},
'resolve-url-loader',
'sass-loader'
],
},
{
// All files with a '.html' extension will be handled by html-loader and save into external file
test: /\.html$/,
exclude: /node_modules/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[hash:10].html',
}
},
'extract-loader',
'html-loader'
]
},
{
// All images and fonts will be optimized and their paths will be solved
enforce: 'pre',
test: /\.(png|jpe?g|gif|svg|woff2?|eot|ttf|otf|wav)(\?.*)?$/,
use: [{
loader: 'url-loader',
options: {
name: '[name].[hash:10].[ext]',
limit: 8192
}
}, {
loader: 'img-loader'
}],
},
{
test: /\.hbs$/,
exclude: /node_modules/,
use: {
loader: 'underscore-template-loader',
query: {
attributes: ['img:src', 'link:href']
}
}
},
// Ignore warnings about System.import in Angular
{
test: /[\/\\]@angular[\/\\].+\.js$/,
parser: {
system: true
}
}
]
},
// Configure how modules are resolved
resolve: {
extensions: [".ts", ".js"]
},
// How and where webpack should output bundles, assets and anything else
output: {
path: path.resolve('./dist'),
filename: '[name].js'
},
// What bundle information gets displayed
stats: {
warnings: false
},
// Target a specific environment (cf. doc)
target: 'electron-main',
// Configure whether to polyfill or mock certain Node.js globals and modules
node: {
__dirname: false
},
// Customize the webpack build process with additionals plugins
plugins: [
new HtmlWebpackPlugin(indexConfig),
new Webpack.ContextReplacementPlugin(/angular([\\\/])core([\\\/])/, path.resolve(__dirname, './src')),
new CopyWebpackPlugin(['./package.json']),
],
};
// UglifyJs and clean output folder only for prod
if (!dev) {
webpackConfig.plugins.push(new CleanWebpackPlugin(pathsToClean));
webpackConfig.plugins.push(new UglifyJsPlugin());
}
// Export the config
module.exports = webpackConfig;