-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
149 lines (133 loc) · 4.95 KB
/
webpack.common.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
const webpack = require('webpack')
//////////////////////////////////////////////////////////////////////
// 1. ADD LOADERS TO HELP WEBPACK UNDERSTAND THINGS.
// By defauly Webpack nativley only understands JS.
// Use Loaders to help webpack understand more filetypes.
//////////////////////////////////////////////////////////////////////
// Enable Webpack to read common non-JS files
const FileLoader = require("file-loader");
// Enable Webpack to read common non-JS files and make small file into embedded data
const UrlLoader = require("url-loader");
//////////////////////////////////////////////////////////////////////
// 2A. ADD PLUGINS THAT DO CONVERSIONS BETWEEN THINGS
//////////////////////////////////////////////////////////////////////
// Autogenerates index.js into a index.html with auto script tags
const HtmlWebpackPlugin = require('html-webpack-plugin');
// Bundles (CSS) to own CSS file rather than embedded in CSS.
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
//////////////////////////////////////////////////////////////////////
// 2B. ADD PLUGINS THAT TIDY UP THINGS
//////////////////////////////////////////////////////////////////////
// Purges unused CSS (Great for use with a style framework like Tailwind)
const PurgecssPlugin = require('purgecss-webpack-plugin');
// Wipes docs directory on recompiling, keeping the directory clean.
const CleanWebpackPlugin = require('clean-webpack-plugin');
//----------------------------------------------------------------------//
//////////////////////////////////////////////////////////////////////
// 3. Set some constants
//////////////////////////////////////////////////////////////////////
const tailwindcss = require('tailwindcss');
const path = require('path');
const isProd = process.env.NODE_ENV === 'production';
// Custom PurgeCSS extractor for Tailwind that allows special characters in
// class names.
//
// https://github.com/FullHuman/purgecss#extractor
class TailwindExtractor {
static extract(content) {
return content.match(/[A-z0-9-:\/]+/g) || []
}
}
//--------------------- Real code starts here ------------------------//
module.exports = {
entry: ['./src/index.js','./src/styleguide.js'],
module: {
rules: [{
test: /\.(png|jp(e*)g|svg)$/,
use: [{
loader: 'url-loader', // url-loader defaults back to loading with file-loader if size over size limit.
options: {
limit: 8000, // Convert files < 8kb to base64 strings
name: 'assets/images/[hash]-[name].[ext]'
}
}]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [{
loader: 'url-loader', // url-loader defaults back to loading with file-loader if size over size limit.
options: {
limit: 8000, // Convert files < 8kb to base64 strings
name: 'assets/fonts/[name].[ext]'
}
}]
}
]
},
plugins: [
// Clean the 'docs' folder before each build is run
console.log("Running..."),
isProd && new CleanWebpackPlugin(['docs']),
isProd && new PurgecssPlugin({
paths: glob.sync(path.join(__dirname, 'src') + '/**/*'),
extractors: [{
extractor: TailwindExtractor,
// File extensions to include when scanning for class names.
extensions: ['html', 'js']
}]
}),
new ExtractTextPlugin('[name].css'),
new CopyWebpackPlugin([{
context: 'src/assets/stylesheets/',
from: '*.css',
to: path.resolve(__dirname, 'docs/assets/stylesheets/')
}, {
context: 'src/assets/images/',
from: '*.png',
to: path.resolve(__dirname, 'docs/assets/images/')
}, {
context: 'src/assets/stylesheets',
from: '*.svg',
to: path.resolve(__dirname, 'docs/assets/stylesheets')
}, {
context: 'src/assets/stylesheets/fonts',
from: '*/*',
test: /\.(woff|woff2|eot|ttf|otf)$/,
to: path.resolve(__dirname, 'docs/assets/stylesheets/fonts')
}]),
new HtmlWebpackPlugin({
inject: true,
template: './src/index.html',
minify: isProd && {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}),
new HtmlWebpackPlugin({
inject: true,
filename: 'styleguide.html',
template: './src/styleguide.html',
minify: isProd && {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}),
].filter(Boolean)
}