-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack.config.babel.js
197 lines (189 loc) · 5.6 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import path from 'path';
import fs from 'fs';
require('dotenv').config();
const app = {
title: 'Progressive ToDo List (VueJS)',
short: 'ToDo PWA (VueJS)',
description: 'A ToDo List as a Progressive Web App based on VueJS',
};
class TailwindExtractor {
static extract(content) {
return content.match(/[A-Za-z0-9-_:\/]+/g) || [];
}
}
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import WebpackPwaManifest from 'webpack-pwa-manifest';
import { InjectManifest } from 'workbox-webpack-plugin';
import PurgecssPlugin from 'purgecss-webpack-plugin';
import TerserJSPlugin from 'terser-webpack-plugin';
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import glob from 'glob-all';
import tailwindcss from 'tailwindcss';
module.exports = (env, argv) => {
const dirDist = path.resolve(__dirname, 'dist');
const dirSrc = path.resolve(__dirname, 'src');
const dev = argv.mode !== 'production';
let serveHttps = false;
if (process.env.SSL_KEY && process.env.SSL_CRT && process.env.SSL_PEM) {
serveHttps = {
key: fs.readFileSync(process.env.SSL_KEY),
cert: fs.readFileSync(process.env.SSL_CRT),
ca: fs.readFileSync(process.env.SSL_PEM),
};
}
return {
optimization: {
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
},
entry: {
app: `${dirSrc}/index.js`,
},
devServer: {
contentBase: dirDist,
compress: true,
port: process.env.PORT || 8080,
https: serveHttps,
hot: true,
historyApiFallback: true,
},
output: {
path: dirDist,
filename: 'assets/[name]-[hash].js',
},
devtool: dev ? `cheap-module-eval-source-map` : undefined,
plugins: [
new CleanWebpackPlugin({
cleanStaleWebpackAssets: false,
}),
new MiniCssExtractPlugin({
filename: dev ? 'assets/[name].css' : 'assets/[name].[hash].css',
chunkFilename: dev
? 'assets/[name].[id].css'
: 'assets/[name].[id].[hash].css',
}),
...(dev
? []
: [
new PurgecssPlugin({
paths: glob.sync([`${dirSrc}/**/*.vue`, `${dirSrc}/index.html`]),
extractors: [
{
extractor: TailwindExtractor,
extensions: ['html', 'js', 'vue'],
},
],
}),
]),
new CopyWebpackPlugin([
{
from: 'src/assets/static',
to: 'assets/static',
},
]),
new HtmlWebpackPlugin({
title: app.title,
description: app.description,
template: 'src/index.html',
filename: './index.html',
chunksSortMode: 'none',
minify: dev
? false
: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
},
}),
new WebpackPwaManifest({
name: app.title,
short_name: app.short,
description: app.description,
theme_color: '#000',
background_color: '#fff',
display: 'standalone',
crossorigin: 'use-credentials',
icons: [
{
src: path.resolve('./src/assets/favicon.png'),
sizes: [96, 128, 192, 256, 384, 512],
destination: path.join('assets', 'icon'),
ios: true,
},
],
/**
* The share target allows your app to be registered as a share target.
* It works quite similar to an HTML Form
*/
share_target: {
action: '/vue/', // the URL where you want to send the request
method: 'GET', // The HTTP Method (POST would be possible but keep in mind that POST Request should then be handled on the server or in the ServiceWorker)
params: {
// The Params you want to accept and pass to the application the prompt usually uses the params title, text, url and files
title: 'title',
text: 'text',
url: 'url',
},
},
}),
new InjectManifest({
swSrc: './src/service-worker.js',
include: [/\.html$/, /\.js$/, /\.css$/],
maximumFileSizeToCacheInBytes: 5000000,
}),
],
module: {
rules: [
{
test: /\.svg$/,
exclude: /node_modules/,
loader: ['babel-loader', 'raw-loader'],
},
{
test: /\.vue$/,
loader: 'vue-loader',
exclude: /node_modules/,
},
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
},
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: dev,
//reloadAll: true,
},
},
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => [
require('postcss-nested'),
tailwindcss('./tailwind.config.js'),
require('autoprefixer'),
],
},
},
],
},
],
},
resolve: {
alias: {},
extensions: ['.js', '.vue'],
},
};
};