-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.parts.js
246 lines (233 loc) · 5.84 KB
/
webpack.parts.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Copyright (c) 2020-present, Bantr, Inc.
* Emiel Van Severen
*/
// const CleanWebpackPlugin = require('clean-webpack-plugin')
const webpack = require('webpack');
const dotenv = require('dotenv');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const path = require('path');
const chalk = require('chalk');
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
const styledComponentsTransformer = createStyledComponentsTransformer();
exports.start = () => console.log('Webpack has been started..');
exports.IO = () => ({
devtool: '',
target: 'web',
// startpoint
entry: [path.join(__dirname, '/src/index.tsx')],
// on production this returns one bundled js file. To avoid caching a hash is added.
output: {
chunkFilename: '[name].js',
filename: '[name].bundle.[hash:8].js',
publicPath: '/' // fixes mime type error when links are clicked (history fallback api bug)
},
});
exports.progress = () => ({
plugins: [
new ProgressBarPlugin({
format: 'build [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)',
clear: false
})
]
});
exports.dotEnv = (env) => ({
plugins: [
new webpack.DefinePlugin(env)
]
});
exports.CopyPublicFolder = () => ({
plugins: [
new CopyWebpackPlugin([
{ from: 'public', to: '' }
])
]
})
exports.devServer = ({ host, port } = { host: 'localhost', port: 8080 }) => ({
devServer: {
host: host,
port: port,
open: true,
stats: 'errors-only',
hot: true,
overlay: {
warnings: false,
errors: true
},
compress: true,
disableHostCheck: true,
public: 'localhost',
historyApiFallback: true, // path changes react router dom.,
after: () => console.log('Development server has been started.')
}
});
exports.banner = () => ({
plugins: [
new webpack.BannerPlugin({
banner: 'contributed by: github.com/emielvanseveren, github.com/niekcandaele hash:[hash] name:[name]'
})
]
});
exports.cleanDist = () => ({
plugins: [
new CleanWebpackPlugin({
verbose: false
})
]
});
exports.sourceMap = () => ({
devtool: 'source-map'
});
exports.RebuildOnModuleInstall = () => ({
plugins: [new WatchMissingNodeModulesPlugin(path.resolve('node_modules'))]
});
exports.loadHtml = () => ({
plugins: [
new HtmlWebPackPlugin({
template: './src/index.html',
filename: 'index.html'
})
]
});
exports.cssExtract = () => ({
plugins: [
new MiniCssExtractPlugin({
filename: '[name].min.css',
chunkFilename: '[id].css'
})
]
});
exports.minify = () => ({
optimization: {
minimizer: [
new TerserPlugin({
parallel: 4,
sourceMap: false,
terserOptions: {
ecma: 7,
warnings: false,
output: true,
ie8: false,
compress: {},
mangle: true,
safari10: false
}
}),
new OptimizeCSSAssetsPlugin({})
],
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 0
}
}
});
exports.manifest = () => ({
plugins: [new ManifestPlugin()]
});
exports.minimizeImages = () => ({
plugins: [
new ImageminPlugin({
test: 'dist/**',
pngquant: {
quality: '95-100'
}
})
]
});
exports.ServiceWorker = () => ({
plugins: [
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true, // Whether or not the service worker should start controlling any existing clients as soon as it activates.
exclude: [/\.map$/, /asset-manifest\.json$/]
})
]
});
exports.aliases = () => ({
resolve: {
extensions: ['.ts', '.tsx', '.js', 'jsx', '.json'],
alias: {
'lib': path.resolve(__dirname, 'lib')
}
}
});
exports.loaders = ({ filename }) => ({
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
'babel-loader',
{
loader: 'ts-loader',
options: { getCustomTransformers: () => ({ before: [styledComponentsTransformer] }) }
}
]
},
{
test: /\.(jsx)$/,
exclude: /node_modules/,
use: ['babel']
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader'
}
]
},
{
test: /\.(css|scss)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 2
}
},
{
loader: 'sass-loader'
}
]
},
{
test: /\.(jp?g|png|svg|webp|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 8192,
fallback: 'file-loader',
name: filename,
outputPath: 'images'
}
}
},
{
test: /\.(ogg|mp3|wav|mpe?g)$/i,
loader: 'file-loader',
options: {
name: '[path][name].[ext]'
}
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
use: ['graphql-tag/loader']
}
]
}
});