Created
September 12, 2018 06:57
-
-
Save DiederikvandenB/588b8e01f5931a99757cfc761239cfe6 to your computer and use it in GitHub Desktop.
PurgeCSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* COMMON WEBPACK CONFIGURATION | |
*/ | |
const path = require('path'); | |
const webpack = require('webpack'); | |
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); | |
const dotEnv = require('dotenv-webpack'); | |
process.noDeprecation = true; | |
module.exports = (options) => ({ | |
mode: options.mode, | |
entry: options.entry, | |
output: Object.assign({ // Compile into js/build.js | |
path: path.resolve(process.cwd(), 'build'), | |
publicPath: '/', | |
}, options.output), // Merge with env dependent settings | |
module: { | |
rules: [ | |
{ | |
test: /\.js$/, // Transform all .js files required somewhere with Babel | |
exclude: /node_modules/, | |
use: { | |
loader: 'babel-loader', | |
options: options.babelQuery, | |
}, | |
}, | |
{ | |
test: /\.(graphql|gql)$/, | |
exclude: /node_modules/, | |
loader: 'graphql-tag/loader' | |
}, | |
{ | |
// Preprocess our own .css files | |
test: /\.css$/, | |
exclude: /node_modules/, | |
use: [ | |
MiniCssExtractPlugin.loader, | |
{ loader: 'css-loader', options: { importLoaders: 1 } }, | |
'postcss-loader' | |
], | |
}, | |
{ | |
// Preprocess 3rd party .css files located in node_modules | |
test: /\.css$/, | |
include: /node_modules/, | |
use: ['style-loader', 'css-loader'], | |
}, | |
{ | |
test: /\.(eot|svg|otf|ttf|woff|woff2)$/, | |
use: 'file-loader', | |
}, | |
{ | |
test: /\.(jpg|png|gif)$/, | |
use: [ | |
'file-loader', | |
{ | |
loader: 'image-webpack-loader', | |
options: { | |
query: { | |
gifsicle: { | |
interlaced: true | |
}, | |
mozjpeg: { | |
progressive: true | |
}, | |
optipng: { | |
optimizationLevel: 7 | |
}, | |
pngquant: { | |
quality: '65-90', | |
speed: 4 | |
} | |
} | |
}, | |
}, | |
], | |
}, | |
{ | |
test: /\.html$/, | |
use: 'html-loader' | |
}, | |
{ | |
test: /\.(mp4|webm)$/, | |
use: { | |
loader: 'url-loader', | |
options: { | |
limit: 10000 | |
}, | |
}, | |
}, | |
], | |
}, | |
plugins: options.plugins.concat([ | |
new webpack.ProvidePlugin({ | |
// make fetch available | |
fetch: 'exports-loader?self.fetch!whatwg-fetch' | |
}), | |
// Always expose NODE_ENV to webpack, in order to use `process.env.NODE_ENV` | |
// inside your code for any environment checks; UglifyJS will automatically | |
// drop any unreachable code. | |
new webpack.DefinePlugin({ | |
'process.env': { | |
NODE_ENV: JSON.stringify(process.env.NODE_ENV) | |
}, | |
}), | |
new dotEnv(), | |
new MiniCssExtractPlugin({ | |
filename: '[name].css', | |
}), | |
]), | |
resolve: { | |
modules: ['app', 'node_modules'], | |
extensions: [ | |
'.js', | |
'.jsx', | |
'.scss', | |
'.react.js' | |
], | |
mainFields: [ | |
'browser', | |
'jsnext:main', | |
'main' | |
] | |
}, | |
devtool: options.devtool, | |
target: 'web', // Make web variables accessible to webpack, e.g. window | |
performance: options.performance || {}, | |
optimization: { | |
namedModules: true, | |
splitChunks: { | |
name: 'vendor', | |
minChunks: 2, | |
cacheGroups: { | |
styles: { | |
name: 'styles', | |
test: /\.css$/, | |
chunks: 'all', | |
enforce: true | |
}, | |
}, | |
}, | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
// Important modules this config uses | |
const path = require('path'); | |
const glob = require('glob-all'); | |
// const webpack = require('webpack'); | |
const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
const PurgecssPlugin = require('purgecss-webpack-plugin'); | |
module.exports = require('./webpack.base.babel')({ | |
mode: 'production', | |
// In production, we skip all hot-reloading stuff | |
entry: [ | |
path.join(process.cwd(), 'app/app.js') | |
], | |
// Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets | |
output: { | |
filename: '[name].[chunkhash].js', | |
chunkFilename: '[name].[chunkhash].chunk.js' | |
}, | |
plugins: [ | |
new PurgecssPlugin({ | |
paths: glob.sync([ | |
path.join(process.cwd(), 'app/**/*.html'), | |
path.join(process.cwd(), 'app/**/*.js'), | |
]), | |
extractors: [ | |
{ | |
extractor: TailwindExtractor, | |
extensions: ['html', 'js'], | |
} | |
] | |
}), | |
// Minify and optimize the index.html | |
new HtmlWebpackPlugin({ | |
template: 'app/index.html', | |
minify: { | |
removeComments: true, | |
collapseWhitespace: true, | |
removeRedundantAttributes: true, | |
useShortDoctype: true, | |
removeEmptyAttributes: true, | |
removeStyleLinkTypeAttributes: true, | |
keepClosingSlash: true, | |
minifyJS: true, | |
minifyCSS: true, | |
minifyURLs: true, | |
}, | |
inject: true | |
}), | |
], | |
performance: { | |
assetFilter: (assetFilename) => !(/(\.map$)|(^(main\.|favicon\.))/.test(assetFilename)), | |
}, | |
}); | |
class TailwindExtractor { | |
static extract(content) { | |
return content.match(/[A-z0-9-:\/]+/g); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment