-
Notifications
You must be signed in to change notification settings - Fork 18
/
webpack.config.js
executable file
·152 lines (137 loc) · 3.77 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
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const TerserPlugin = require('terser-webpack-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const HtmlWebPackPlugin = require('html-webpack-plugin')
const HtmlBeautifyPlugin = require('html-beautify-webpack-plugin')
const FaviconsWebpackPlugin = require('favicons-webpack-plugin')
const OpenBrowserPlugin = require('opn-browser-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = (env, argv) => {
const pages = []
fs.readdirSync('./src/pages/').forEach(file => {
const page = file.split('.')[0]
let filename = './'
let title = 'Logins and persistence for static sites'
let desc = 'The easiest way to add user accounts & persistence to your static site.'
if (page !== 'index') {
const pagePath = page.split('_')
for (let i = 0; i < pagePath.length; i++) {
filename += pagePath[i] + '/'
}
fs.readFile('./src/pages/' + file, 'utf-8', (err, data) => {
if (err) throw err
data = data.split('\n')
if (data[0].startsWith('<!-- ') && data[0].endsWith('-->')) {
title = data[0].replace('<!-- ', '').replace('-->', '').trim()
desc = 'Userbase: ' + title
}
})
}
filename += 'index.html'
pages.push(new HtmlWebPackPlugin({
template: './src/template.html',
filename,
templateParameters() { return { page, title, desc } }
}))
})
const config = {
entry: {
main: './src/index.js'
},
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/',
filename: '[name].js',
globalObject: 'this'
},
target: 'web',
devtool: 'source-map',
resolve: {
extensions: ['.js', '.jsx'],
},
performance: {
hints: false
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader',
]
},
{
enforce: 'pre',
test: /\.js$/,
use: ['source-map-loader'],
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.(woff|woff2|eot|ttf|otf|png|svg|jpg|gif)$/,
use: ['file-loader']
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
chunkFilename: 'style.css',
ignoreOrder: false
}),
...pages,
new FaviconsWebpackPlugin('./src/img/icon.png'),
new HtmlBeautifyPlugin({
config: {
html: {
end_with_newline: true,
indent_size: 2,
indent_with_tabs: false,
indent_inner_html: false,
preserve_newlines: true
}
}
}),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.WatchIgnorePlugin(['./dist']),
new CopyWebpackPlugin({
patterns: [
{ from: './src/bitcoin.pdf' }
]
}),
]
}
if (argv.mode == 'development') {
config.devtool = 'inline-source-map'
config.devServer = {
watchContentBase: true,
hot: true,
inline: true,
host: '0.0.0.0',
port: 3000
}
config.plugins.push(new webpack.HotModuleReplacementPlugin())
config.plugins.push(new OpenBrowserPlugin({
url: 'https://localhost:3000'
}))
}
if (argv.mode == 'production') {
config.optimization = {
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true
}),
new OptimizeCSSAssetsPlugin({})
]
}
}
return config
}