-
Notifications
You must be signed in to change notification settings - Fork 12
/
webpack.config.js
187 lines (179 loc) · 5.84 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
const path = require('path');
var glob = require('glob-all');
const webpack = require('webpack');
const HandlebarsPlugin = require('handlebars-webpack-plugin');
const FixStyleOnlyEntriesPlugin = require("webpack-fix-style-only-entries");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const MinifyPlugin = require('babel-minify-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const mergeJSON = require('handlebars-webpack-plugin/utils/mergeJSON');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
// Project config data.
// Go here to change stuff for the whole demo, ie, change the navbar.
// Also go here for the various data loops, ie, category products, slideshows
const projectData = mergeJSON(path.join(__dirname, '/src/data/**/*.json'));
//PurgeCSS Paths
const purgeCSSPaths = {
src: path.join(__dirname, 'src', 'html'),
partials: path.join(__dirname, 'src', 'partials')
}
// paths used in various placed in webpack config
const paths = {
src: {
imgs: './src/assets/images',
scss: './src/assets/scss',
fonts: './src/assets/fonts',
js: './src/assets/js',
favicon: './src/assets/favicon',
},
dist: {
imgs: './assets/images',
css: './assets/css',
fonts: './assets/fonts',
js: './assets/js',
favicon: './assets/favicon',
}
}
// Main webpack config options.
const wPackConfig = {
entry: {
'libs': [paths.src.scss + '/libs.scss'],
'theme': [paths.src.js + '/theme.js', paths.src.scss + '/theme.scss']
},
output: {
filename: paths.dist.js + '/[name].bundle.js',
},
devtool: 'source-map',
mode: 'development',
module: {
rules: [{
test: /\.(sass|scss|css)$/,
include: path.resolve(__dirname, paths.src.scss.slice(2)),
use: [{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
url: false,
// sourceMap: true,
},
},
{
loader: 'postcss-loader'
},
{
loader: 'sass-loader',
// options: {
// sourceMap: true,
// sassOptions: {
// indentWidth: 4,
// outputStyle: 'expanded',
// sourceComments: true
// }
// }
},
],
}, ]
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/](node_modules)[\\/].+\.js$/,
name: 'vendor',
chunks: 'all'
}
}
},
minimizer: [
new OptimizeCssAssetsPlugin({
cssProcessorOptions: {
map: {
inline: false,
},
},
cssProcessorPluginOptions: {
preset: [
'default',
{
discardComments: {
removeAll: true,
},
},
],
},
}),
new TerserPlugin({
extractComments: false,
terserOptions: {
output: {
comments: false,
},
},
}),
],
},
plugins: [
new webpack.ProgressPlugin(),
new CopyPlugin({
patterns: [{
from: paths.src.fonts,
to: paths.dist.fonts,
noErrorOnMissing: true
},
{
from: paths.src.imgs,
to: paths.dist.imgs,
noErrorOnMissing: true
},
{
from: paths.src.favicon,
to: paths.dist.favicon,
noErrorOnMissing: true
}
],
}),
new HandlebarsPlugin({
entry: path.join(process.cwd(), 'src', 'html', '**', '*.html'),
output: path.join(process.cwd(), 'dist', '[path]', '[name].html'),
partials: [path.join(process.cwd(), 'src', 'partials', '**', '*.{html,svg}')],
data: projectData,
helpers: {
webRoot: function() {
return '{{webRoot}}';
},
config: function(data) {
return data;
},
ifEquals: function(arg1, arg2, options) {
if (arg1 === arg2) {
return options.fn(this);
}
return options.inverse(this);
},
log: function(data) {
console.log(data);
},
limit: function(arr, limit) {
if (!Array.isArray(arr)) { return []; }
return arr.slice(0, limit);
},
isEven: function(val, options) {
return val % 2 === 0 ? options.fn(this) : options.inverse(this);
}
},
onBeforeSave: function(Handlebars, res, file) {
const elem = file.split('//').pop().split('/').length;
return res.split('{{webRoot}}').join('.'.repeat(elem));
},
}),
new FixStyleOnlyEntriesPlugin(),
new MiniCssExtractPlugin({
filename: paths.dist.css + '/[name].bundle.css',
}),
]
};
module.exports = wPackConfig;