I thought that, as a control freak, I would enjoy making my own webpack more than I would enjoy using Parcel.js.
Nope. Turns out that Parcel is delightful and does everything I need. Oh well.
I'll leave this up for posterity. I present to you... my webpack configuration...
- Markup: Pug or HTML => HTML
- Style: Sass or CSS => autoprefixer/cssnano => CSS
- Logic: ES6 => Babel => JS
That's it.
npm install
npm run dev
(runs webpack-dev-server --open --config webpack.dev.js
)
npm run build
(runs webpack --config webpack.prod.js"
)
src/styles/
contains pages' styles as .css or .scss- Webpack automatically detects .css or .scss files
src/scripts
contains pages' scripts as ES6 .js- Webpack must be told to watch these files by adding them to the
entry
section ofwebpack.common.js
- The format for the
entry
section is{ chunk_name: './src/scripts/chunk_file.js }
- Webpack must be told to watch these files by adding them to the
src/pages
contains pages' markup, as either .pug (pug templates) or .html (none/lodash templates)- Webpack must be told to watch these files by adding them to the
plugins
section ofwebpack.common.js
- These files are added using the html-webpack-plugin
- Scripts for a page can be added by adding the above-defined
chunk-name
to thechunks
section of thenew HtmlWebpackPlugin({})
- Styles for a page can be added by importing them in a chunk.
- Webpack must be told to watch these files by adding them to the
This is a minimal example showing the bullet points from the previous section. You'll find approximately the same thing in the repo.
src/styles/about.scss
@import 'base.scss';
$primary-color: red;
html {
background: $primary-color;
}
src/scripts/about.js
import 'base.js';
import '../styles/about.scss';
console.log('Hello from About!')
src/pages/layouts/base.pug
html
head
title #{title}
body
h1 Base
block content
src/pages/about.pug
extends layouts/base.pug
block content
h2 About
img(
src=require("../img/sofia.jpeg")
width=400
)
webpack.common.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
// === Add Scripts Here === //
entry: {
about: './src/scripts/about.js',
},
// === Add Webpages Here === //
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
filename: 'about/index.html',
template: 'src/pages/about.pug',
chunks: ['about'] // - Link webpages to scripts here - //
}),
],
// Don't Worry About This Too Much (unless you're feeling confident)
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}, {
test: /\.pug$/,
exclude: /node_modules/,
loader: 'pug-loader',
}
]
},
};