This repository has been archived by the owner on Jan 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.js
287 lines (235 loc) · 8.01 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const { resolve } = require( 'path' );
const capitalize = require( 'lodash.capitalize' );
const merge = require( 'webpack-merge' );
const webpack = require( 'webpack' );
// plugins
/**
* This is a webpack plugin that simplifies creation of HTML files to serve your webpack bundles.
* This is especially useful for webpack bundles that include a hash in the filename which changes every compilation.
* https://github.com/ampedandwired/html-webpack-plugin
*/
const HtmlWebpackPlugin = require( 'html-webpack-plugin' );
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
// webpack config helpers
const { getIfUtils, removeEmpty } = require( 'webpack-config-utils' );
const context = resolve( __dirname );
module.exports = ( env ) => {
const { ifProd, ifNotProd, ifTest, ifDev, ifSite } = getIfUtils( env, ['prod', 'test', 'dev', 'site'] );
const { ifProdOrSite, ifDevOrSite } = getCustomIfUtils( { ifDev, ifProd, ifSite } );
const packagePath = resolve( context, './packages', env.element || '' );
const baseConfig = {
// The base directory, an absolute path, for resolving entry points and loaders from configuration.
context,
// The point or points to enter the application.
entry: getEntryPointConfig( packagePath, {
isTest: ifTest(),
isProd: ifProd(),
} ),
output: {
filename: ifProd('[name].min.js', '[name].js'),
path: getPathConfig( env.element, {
isSite: ifSite(),
isTest: ifTest(),
}),
// Include comments with information about the modules.
pathinfo: ifNotProd()
},
resolve: {
extensions: [ '.js', '.ts', '.tsx' ]
},
// Allow connection from any IP, so that it is accessible from VMs/external devices
devServer: {
host: '0.0.0.0',
port: ifTest(8090,false)
},
/**
* Developer tool to enhance debugging
*
* See: https://webpack.js.org/configuration/devtool/#devtool
* See: https://github.com/webpack/docs/wiki/build-performance#sourcemaps
*/
devtool: 'source-map',
module: {
rules: [
// Typescript
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [ 'awesome-typescript-loader' ]
},
// CSS
{
// Do not transform vendor's CSS with CSS-modules
// The point is that they remain in global scope.
test: /\.css$/,
include: /node_modules/,
// @TODO replace with "use", we need to use legacy "loader" instead of "use" to make [email protected] work
loader:
[
'style-loader',
{
loader: 'css-loader',
query: { sourceMap: true }
}
]
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [ 'to-string-loader', 'css-loader', 'sass-loader' ]
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
},
{
test: /\.md$/,
use: [
'html-loader',
'markdown-loader'
]
},
{
test: /\.svg$/,
use: [ 'file-loader' ]
},
{
test: /\.gif|\.jpg$/,
use: [ 'url-loader' ]
},
]
},
/**
* Since Loaders only execute transforms on a per-file basis,
* Plugins are most commonly used (but not limited to) performing actions and custom functionality on "compilations"
* or "chunks" of your bundled modules (and so much more).
* In order to use a plugin, you just need to require() it and add it to the plugins array.
* Since most plugins are customizable via options, you need to create an instance of it by calling it with new.
*/
plugins: removeEmpty([
// Set NODE_ENV to enable production react version
new webpack.DefinePlugin( {
'process.env': { NODE_ENV: ifProd( '"production"', '"development"' ) }
} ),
new webpack.LoaderOptionsPlugin( {
// The UglifyJsPlugin will no longer put loaders into minimize mode, and the debug option has been deprecated.
// These options are simply moved into a new plugin, LoaderOptionsPlugin, for separation of concerns reasons.
// Default webpack build options saves a couple of kBs
minimize: ifProdOrSite(),
debug: ifDev(),
quiet: ifProdOrSite(),
}),
// Uglify bundles
ifProdOrSite(new webpack.optimize.UglifyJsPlugin( {
compress: {
screw_ie8: true,
warnings: false
},
output: { comments: false }
} ) ),
ifSite( new FaviconsWebpackPlugin( './assets/blaze-elements-logo.svg' ) ),
/**
* Use the HtmlWebpackPlugin plugin to make index.html a template so css and js can dynamically be added to the page.
* This will also take care of moving the index.html file to the build directory using the index.html in src as a template.
* https://github.com/ampedandwired/html-webpack-plugin
*/
ifDevOrSite(new HtmlWebpackPlugin({
template: resolve( context, isBlazeElementsMainPackage( env.element ) ? 'index.html' : 'index.package.html' ),
packages: getPackagesForBuild( env.element, require('./package.json').packages ),
excludeChunks: [ 'index', 'index-with-dependencies' ], // Exclude 'index' & 'index-with-dependencies' as it is included in 'main.demo'
inject: 'head',
chunksSortMode: buildChunksSort([ 'polyfills', 'styles', 'index', 'index-with-dependencies', 'main.demo', 'test-helpers', 'test' ])
}))
]),
performance: {
hints: ifProd() && 'warning'
}
}
const withDependenciesConfig = merge(
baseConfig,
{
output: {
filename: ifProd('index-with-dependencies.min.js', 'index-with-dependencies.js')
}
}
);
const withoutDependenciesConfig = merge(
baseConfig,
{
output: {
filename: ifProd('index.min.js', 'index.js')
},
externals: {
skatejs: 'skatejs'
}
}
);
return ifProd(
[ withDependenciesConfig, withoutDependenciesConfig ],
baseConfig
);
function getCustomIfUtils( { ifDev, ifProd, ifSite } = {} ){
return {
ifProdOrSite: function( value, alternate ) {
return getByEnvValue( ifProd() || ifSite(), value, alternate );
},
ifDevOrSite: function( value, alternate ) {
return getByEnvValue( ifDev() || ifSite(), value, alternate );
}
}
function getByEnvValue( envValue, value, alternate ) {
return isUndefined( value ) ? envValue : propIf( envValue, value, alternate )
}
function isUndefined( val ) {
return typeof val === "undefined"
}
function getValue( val ) {
return JSON.parse( val );
}
function propIf( add, value, alternate ) {
return getValue( add ) ? value : alternate
}
}
};
function getEntryPointConfig( basePath, { isTest, isProd } = {} ) {
if ( isTest ) {
return {
'test': resolve( basePath, 'index.test.ts' ),
'test-helpers' : resolve( context, 'test-helpers.ts' )
};
}
if ( isProd ) {
return {
'index': resolve( basePath, 'index.ts' )
};
}
return {
'main.demo': resolve( basePath, 'index.demo.tsx'),
'polyfills': resolve( context, 'polyfills.ts'),
'styles': resolve( context, 'styles.ts')
};
}
/**
* Build sort function for chunksSortMode from array
*/
function buildChunksSort( order ) {
return (a, b) => order.indexOf(a.names[0]) - order.indexOf(b.names[0]);
}
function getPackagesForBuild( element, allPackages ) {
if ( isBlazeElementsMainPackage( element ) ) {
return allPackages;
}
return element;
}
function isBlazeElementsMainPackage(packageName) {
return packageName === 'blaze-elements';
}
function getPathConfig( packageName, { isSite, isTest } = {} ) {
if ( isSite ) {
return resolve( context, 'tmp', 'site' );
}
if ( isTest ) {
return resolve( context, 'tmp', 'tests' );
}
return resolve( context, 'packages', packageName, 'dist' );
}