css-framework/webpack.config.js

131 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-02-17 23:44:20 +00:00
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2021-02-23 14:10:48 +00:00
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
2021-02-17 23:44:20 +00:00
const EsLintPlugin = require('eslint-webpack-plugin');
const StylelintPlugin = require('stylelint-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = (env) => ({
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,
2021-02-24 15:30:40 +00:00
host: '0.0.0.0',
2021-02-17 23:44:20 +00:00
},
plugins: [
new MiniCssExtractPlugin({
filename: env.production ? '[name].[contenthash].css' : '[name].css',
}),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
// weird loader string syntax because https://github.com/bazilio91/ejs-compiled-loader/issues/46
template: '!!ejs-compiled-loader?{}!./src/templates/index.ejs',
}),
new EsLintPlugin(),
new StylelintPlugin(),
],
module: {
rules: [
{
test: /\.ejs$/,
use: {
// weird loader string syntax because https://github.com/bazilio91/ejs-compiled-loader/issues/46
// NOTE - talso an alternative workaround that involves passing
// the usual 'ejs-compiled-loader' as the loader and passing
// an empty options object, however hmr did not work with that fix.
loader: '!!ejs-compiled-loader?{}!',
},
},
{
2021-02-28 13:09:45 +00:00
test: /\.js$/,
2021-02-17 23:44:20 +00:00
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
{
test: /\.inline\.css$/i,
include: path.resolve(__dirname, 'src/css'),
use: [
{
loader: 'style-loader',
options: {
insert: function insertAtTop(element) {
const parent = document.querySelector('head');
// eslint-disable-next-line no-underscore-dangle
const lastInsertedElement = window._lastElementInsertedByStyleLoader;
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,
2021-02-28 13:09:45 +00:00
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '',
},
},
'css-loader',
'postcss-loader',
],
2021-02-17 23:44:20 +00:00
},
2021-02-27 18:54:22 +00:00
{
test: /\.scss$/i,
include: path.resolve(__dirname, 'src/sass'),
2021-02-28 13:09:45 +00:00
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '',
},
},
'css-loader',
'postcss-loader',
'sass-loader',
],
2021-02-27 18:54:22 +00:00
},
2021-02-17 23:44:20 +00:00
{
test: /.(png|svg|jpg|jpeg|gif)$/i,
include: path.resolve(__dirname, 'src'),
type: 'asset',
},
],
},
optimization: {
2021-02-23 14:10:48 +00:00
minimize: true,
2021-02-17 23:44:20 +00:00
minimizer: [
2021-02-23 14:10:48 +00:00
// For webpack@5 the `...` syntax extends existing minimizers
// `...`,
new CssMinimizerPlugin(),
2021-02-17 23:44:20 +00:00
],
},
});