forked from VvbirdvV/java-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
executable file
·217 lines (184 loc) · 6.88 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
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const fs = require('fs')
const exec = require('child_process').exec;
const GoogleFontsPlugin = require('google-fonts-plugin')
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const gchelpers = require('./src/_scripts/_modules/helpers.js')
async function getConfig() {
// Configurable options for the build
const userConfig = {
/* Languages to be supported by syntax highlighting in Reveal
(the more fonts, the heavier the bundle will be) */
HIGHLIGHT_LANGUAGES: ['xml', 'java', 'bash', 'swift'],
/* Fonts and formats to be included in build. Could be several from ['ttf', 'eot', 'woff', 'woff2']*/
GOOGLE_FONTS: [{ "family": "Source Sans Pro" },
{ "family": "Passion One" }],
GOOGLE_FONTS_FORMATS: ['ttf'],
/* FONT AWESOME. CSS or SVG backend. If CSS chosen, then either link to the CDN
or link to local .css file (automatically downloaded)*/
FONTAWESOME_BACKEND: process.env.FONT_AWESOME, // 'css' or 'svg'
FONTAWESOME_CDN: "https://use.fontawesome.com/releases/v5.2.0/css/all.css",
FONTAWESOME_USE_LOCAL: true
}
// Check if all the font formats already present in dist folder.
// const FONTS_DONWLOAD = userConfig.GOOGLE_FONTS_FORMATS.map(format => fs.existsSync(`./dist/lib/css/${format}.css`))
// .reduce((acc, bool) => acc && bool, true) ? false : true
const FONTS_DONWLOAD = false
const htmlList = await gchelpers.getEntries('./src/')
const [entries, htmlPluginList] = gchelpers.getEntriesAndHTMLPlugins(htmlList, userConfig.FONTAWESOME_BACKEND == 'svg')
console.log('Google fonts download:', FONTS_DONWLOAD)
console.log('FontAwesome framework:', userConfig.FONTAWESOME_BACKEND)
return {
entry: entries,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'lib/js/[name].js'
},
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
// // see https://github.com/FortAwesome/Font-Awesome/
// // and https://github.com/fabiosantoscode/terser/issues/50
// collapse_vars: true,
},
output: null,
},
sourceMap: false
}),
new OptimizeCSSAssetsPlugin({})
]
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
// options: {
// modules: true,
// sourceMap: true,
// importLoader: 2
// }
},
'sass-loader',
],
},
{
test: /(eot|woff|woff2|ttf|svg)(\?\S*)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
// publicPath: './../../',
publicPath: '../webfonts/',
outputPath: 'lib/webfonts/',
emitFile: true
}
}
]
},
]
},
resolve: {
alias: {
nodePath: path.join(__dirname, 'node_modules'),
stylesPath: path.join(__dirname, 'src/_styles'),
}
},
devServer: {
contentBase: path.join(__dirname, "dist/"),
port: 9000
},
plugins: [
...htmlPluginList,
new webpack.ProvidePlugin({
Reveal: 'reveal.js',
}),
/* Copy some needed files in hierarchy */
new CopyWebpackPlugin([
// speaker note base window
{ from: 'node_modules/reveal.js/plugin/notes/notes.html', to: 'lib/js/reveal.js-dependencies/notes.html' },
// styles for slides export to to pdf
{ from: { glob: 'node_modules/reveal.js/css/print/*.css' }, to: 'lib/css/[name].css' },
// any files in content
{
context: 'src/content',
from: '**/*',
to: 'content/'
}
]),
new CopyWebpackPlugin([
// Style from reveal.js-menu, css (not compatible with svg inline)
{ from: 'node_modules/reveal.js-menu/menu.css', to: 'lib/css/menu.css' },
]),
new webpack.DefinePlugin({
HIGHLIGHT_LANGUAGES: JSON.stringify(Object.assign({}, userConfig.HIGHLIGHT_LANGUAGES)),
FA_CSS_LOCAL: userConfig.FONTAWESOME_BACKEND == 'css' && userConfig.FONTAWESOME_USE_LOCAL && !isEnv('production-web-css'),
}),
/* Include only Highlights.js languages that are specified in configEnv.HIGHLIGHT_LANGUAGES */
new webpack.ContextReplacementPlugin(
/highlight\.js\/lib\/languages$/,
new RegExp(`^./(${userConfig.HIGHLIGHT_LANGUAGES.join('|')})$`),
),
new MiniCssExtractPlugin({
filename: 'lib/css/[name].css',
// chunkFilename: "lib/css/[name].css"
}),
/* !!!! FONTS AWESOME !!!!
If (FONTAWESOME_BACKEND=='css' && FONTAWESOME_USE_LOCAL==false) just link
to the FA CDN */
(isEnv('production-web-css') || (userConfig.FONTAWESOME_BACKEND == 'css' && !userConfig.FONTAWESOME_USE_LOCAL))
&& new HtmlWebpackIncludeAssetsPlugin({
assets: [userConfig.FONTAWESOME_CDN],
append: true
}),
/* If font formats missing, then download them */
(FONTS_DONWLOAD)
&& new GoogleFontsPlugin({
"fonts": userConfig.GOOGLE_FONTS,
"formats": userConfig.GOOGLE_FONTS_FORMATS,
"outputDir": "dist/lib/css"
}),
/* Include fonts */
new HtmlWebpackIncludeAssetsPlugin({
assets: userConfig.GOOGLE_FONTS_FORMATS.map(format => `lib/css/${format}.css`),
append: true
}),
// clean up generatedEntries folder of file-specific tree shaking for FA icons
// only when not in dev-server
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
if (process.env.NODE_ENV != 'dev-server') {
exec('rm -R ./src/_scripts/_generatedEntries', (err, stdout, stderr) => {
if (stdout) process.stdout.write(stdout);
if (stderr) process.stderr.write(stderr);
});
}
});
}
},
].filter((plugin) => plugin !== false) // filter out skipped conditions
};
}
const isEnv = (name) => process.env.NODE_ENV === name
module.exports = getConfig();