Skip to content

Instantly share code, notes, and snippets.

@araphiel
Last active September 8, 2019 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save araphiel/dd51aa44485daf544f7a90419ec77899 to your computer and use it in GitHub Desktop.
Save araphiel/dd51aa44485daf544f7a90419ec77899 to your computer and use it in GitHub Desktop.
Example Webpack Config for Static Sites
{
"name": "static-site",
"version": "2.1.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "run-s webpack-prod hugo-prod",
"dev": "run-p webpack-dev hugo-dev",
"hugo-dev": "wait-on assets/dist && hugo server",
"hugo-prod": "hugo",
"webpack-dev": "webpack --mode development --env.production=false --watch",
"webpack-prod": "webpack --mode production --env.production"
},
"keywords": [],
"author": "",
"license": "ISC",
"private": true,
"browserslist": "> 0.25%, not dead",
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^2.0.2",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"friendly-errors-webpack-plugin": "^1.7.0",
"mini-css-extract-plugin": "^0.6.0",
"npm-run-all": "^4.1.5",
"sass": "^1.20.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"uglifyjs-webpack-plugin": "^2.1.3",
"wait-on": "^3.2.0",
"webpack": "^4.32.1",
"webpack-cli": "^3.3.2"
},
"dependencies": {
"bootstrap": "^4.3.1",
"jquery": "^3.4.1",
"popper.js": "^1.15.0"
}
}
const { resolve } = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
module.exports = (env, argv) => {
return {
entry: [
resolve(__dirname, 'src', 'js', 'main.js'),
resolve(__dirname, 'src', 'scss', 'main.scss'),
],
output: {
filename: '[name].js',
path: resolve(__dirname, 'assets', 'dist'),
publicPath: '/dist/'
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: { publicPath: 'assets/styles' }
},
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 2,
url: false
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
implementation: require("sass")
}
}
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: ['file-loader']
},
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['assets/dist']
}),
new FriendlyErrorsWebpackPlugin(),
],
optimization: {
splitChunks: {
chunks: 'all'
},
minimizer: [new UglifyJsPlugin()]
},
performance: {
maxEntrypointSize: 400000
},
watchOptions: {
ignored: ['node_modules']
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment