-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.babel.js
163 lines (155 loc) · 5.39 KB
/
webpack.config.babel.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
// webpack.config.js
const webpack = require('webpack');
const {resolve} = require('path');
const globby = require('globby');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const EventHooksPlugin = require('event-hooks-webpack-plugin');
const plConfig = require('./patternlab-config.json');
const patternlab = require('patternlab-node')(plConfig);
const patternEngines = require('patternlab-node/core/lib/pattern_engines');
const merge = require('webpack-merge');
const customization = require(`${plConfig.paths.source.app}/webpack.app.js`);
module.exports = env => {
const {ifProd, ifDev} = getIfUtils(env);
const config = merge.smartStrategy(plConfig.webpackMerge)({
devtool: ifDev('source-map'),
context: resolve(__dirname, 'source'),
node: {
fs: "empty"
},
entry: {
// Gathers any Source JS files and creates a bundle
//NOTE: This name can be changed, if so, make sure to update _meta/01-foot.mustache
"js/pl-source":
globby.sync([resolve(plConfig.paths.source.js + '**/*.js')]).map(function (filePath) {
return filePath;
})
},
output: {
path: resolve(__dirname, plConfig.paths.public.rootDir),
filename: '[name].js'
},
plugins: removeEmpty([
ifDev(
// Live reloading in development only
new webpack.HotModuleReplacementPlugin()
),
ifProd(
new webpack.optimize.UglifyJsPlugin({
// Compresses in production any bundle
sourceMap: true,
uglifyOptions: {
mangle: false
}
})
),
new webpack.optimize.CommonsChunkPlugin({
// Combines any node module libraries used into their own file
name: 'js/pl-vendor-libraries',
chunks: ['js/pl-source'],
minChunks: module => {
return module.context && /node_modules/.test(module.context);
}
}),
new CopyWebpackPlugin([
{
// Copy all images from source to public
context: resolve(plConfig.paths.source.images),
from: './**/*.*',
to: resolve(plConfig.paths.public.images)
},
{
// Copy favicon from source to public
context: resolve(plConfig.paths.source.root),
from: './*.ico',
to: resolve(plConfig.paths.public.root)
},
{
// Copy all web fonts from source to public
context: resolve(plConfig.paths.source.fonts),
from: './*',
to: resolve(plConfig.paths.public.fonts)
},
{
// Copy all css from source to public
context: resolve(plConfig.paths.source.css),
from: './*.css',
to: resolve(plConfig.paths.public.css)
},
{
// Styleguide Copy everything but css
context: resolve(plConfig.paths.source.styleguide),
from: './**/*',
to: resolve(plConfig.paths.public.root),
ignore: ['*.css']
},
{
// Styleguide Copy and flatten css
context: resolve(plConfig.paths.source.styleguide),
from: './**/*.css',
to: resolve(plConfig.paths.public.styleguide, 'css'),
flatten: true
}
]),
new EventHooksPlugin({
// Before WebPack compiles, call the pattern build API, once done, bundle continues
'before-compile': function(compilationParams, callback){
patternlab.build(callback, plConfig.cleanPublic);
}
}),
new EventHooksPlugin({
'after-compile': function(compilation, callback) {
// watch supported templates
const supportedTemplateExtensions = patternEngines.getSupportedFileExtensions();
const templateFilePaths = supportedTemplateExtensions.map(function (dotExtension) {
return plConfig.paths.source.patterns + '/**/*' + dotExtension;
});
// additional watch files
const watchFiles = [
plConfig.paths.source.patterns + '/**/*.json',
plConfig.paths.source.patterns + '**/*.md',
plConfig.paths.source.data + '**/*.json',
plConfig.paths.source.fonts + '**/*',
plConfig.paths.source.images + '**/*',
plConfig.paths.source.js + '**/*',
plConfig.paths.source.meta + '**/*',
plConfig.paths.source.annotations + '**/*'
];
const allWatchFiles = watchFiles.concat(templateFilePaths);
allWatchFiles.forEach(function(globPath) {
const patternFiles = globby.sync(globPath).map(function (filePath) {
return resolve(filePath);
});
compilation.fileDependencies = compilation.fileDependencies.concat(patternFiles);
});
// signal done and continue with build
callback();
}
}),
]),
devServer: {
contentBase: resolve(__dirname, plConfig.paths.public.rootDir),
port: plConfig.server.port,
open:true,
watchContentBase: false
},
module: {
rules: [
{
test: /\.js$/,
exclude: resolve('node_modules'),
use: [{
loader: 'babel-loader',
options: {
presets: [
['es2015', { modules: false }]
]
}
}]
}
]
}
}, customization(env))
return config
}