webpack-configs/static-html/webpack.config.js

103 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-01-03 16:31:48 +00:00
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2021-01-03 18:28:42 +00:00
const CssnanoPlugin = require ('cssnano-webpack-plugin');
2021-01-03 16:31:48 +00:00
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
2021-01-03 18:28:42 +00:00
module.exports = env => {
console.log(env);
2021-01-03 16:31:48 +00:00
2021-01-03 18:28:42 +00:00
return {
mode: env.production ? 'production' : 'development',
target: 'web',
entry: './src/js/index.js',
output: {
filename: env.production ? '[name].[contenthash].bundle.js' : '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
devtool: env.production ? 'cheap-source-map' : 'inline-source-map',
devServer: {
contentBase: './dist',
hot: true,
},
plugins: [
new MiniCssExtractPlugin({
filename: env.production ? '[name].[contenthash].css' : '[name].css',
}),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
2021-01-03 21:30:11 +00:00
template: './src/templates/index.ejs'
2021-01-03 18:28:42 +00:00
}),
],
module: {
rules: [
2021-01-03 21:30:11 +00:00
{
test: /\.ejs$/,
use: {
loader: 'ejs-compiled-loader',
options: {}, // supply empty options object because of https://github.com/bazilio91/ejs-compiled-loader/issues/46
},
},
2021-01-03 19:55:19 +00:00
{
test: /\.js?/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
2021-01-03 18:28:42 +00:00
{
test: /\.inline\.css$/i,
include: path.resolve(__dirname, 'src/css'),
use: [
{
loader: 'style-loader',
options: {
insert: function insertAtTop(element) {
var parent = document.querySelector('head');
// eslint-disable-next-line no-underscore-dangle
var lastInsertedElement =
window._lastElementInsertedByStyleLoader;
2021-01-03 16:31:48 +00:00
2021-01-03 18:28:42 +00:00
if (!lastInsertedElement) {
parent.insertBefore(element, parent.firstChild);
} else if (lastInsertedElement.nextSibling) {
parent.insertBefore(element, lastInsertedElement.nextSibling);
} else {
parent.appendChild(element);
}
// eslint-disable-next-line no-underscore-dangle
window._lastElementInsertedByStyleLoader = element;
},
},
},
'css-loader',
'postcss-loader'],
},
{
test: /\.css$/i,
include: path.resolve(__dirname, 'src/css'),
exclude: /\.inline\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
test: /.(png|svg|jpg|jpeg|gif)$/i,
include: path.resolve(__dirname, 'src'),
type: 'asset',
},
]
},
optimization: {
minimizer: [
new CssnanoPlugin({
sourceMap: true,
2021-01-03 19:55:19 +00:00
}),
'...', // access defaults (defaults are overridden when specifying minimizer: array)
2021-01-03 18:28:42 +00:00
],
},
}
2021-01-03 16:31:48 +00:00
};