-
Notifications
You must be signed in to change notification settings - Fork 94
/
webpack.config.babel.js
180 lines (173 loc) · 6.01 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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`);
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
module.exports = env => {
const { ifProduction, ifDevelopment } = getIfUtils(env);
const config = merge.smartStrategy(plConfig.app.webpackMerge)(
{
devtool: ifDevelopment("source-map"),
context: resolve(__dirname, plConfig.paths.source.root),
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(__dirname, `${plConfig.paths.source.js}**/*.js`),
"!**/*.test.js"
])
.map(function(filePath) {
return filePath;
})
},
output: {
path: resolve(__dirname, plConfig.paths.public.root),
filename: "[name].js"
},
optimization: {
minimizer: [new UglifyJsPlugin(plConfig.app.uglify)],
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: "initial",
name: "js/pl-source-vendor",
priority: 10,
enforce: true
}
}
}
},
plugins: removeEmpty([
ifDevelopment(
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
),
// Remove with PL Core 3.x
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
}
]),
ifDevelopment(
new EventHooksPlugin({
afterEmit: function(compilation) {
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|md|yaml|yml)`,
`${plConfig.paths.source.data}**/*.(json|md|yaml|yml)`,
`${plConfig.paths.source.fonts}**/*`,
`${plConfig.paths.source.images}**/*`,
`${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(__dirname, filePath);
});
patternFiles.forEach(item => {
compilation.fileDependencies.add(item);
});
});
}
})
),
new EventHooksPlugin({
done: function(stats) {
let cleanPublic = plConfig.cleanPublic;
process.argv.forEach((val, index) => {
if (val.includes("cleanPublic")) {
val = val.split("=");
cleanPublic = JSON.parse(val[1]);
}
});
patternlab.build(() => {}, cleanPublic);
}
})
]),
devServer: {
contentBase: resolve(__dirname, plConfig.paths.public.root),
publicPath: `${plConfig.app.webpackDevServer.url}:${plConfig.app.webpackDevServer.port}`,
port: plConfig.app.webpackDevServer.port,
open: true,
hot: true,
watchContentBase: plConfig.app.webpackDevServer.watchContentBase,
watchOptions: plConfig.app.webpackDevServer.watchOptions
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: "babel-loader",
options: {
cacheDirectory: true
}
}
]
}
]
}
},
customization(env)
);
return config;
};