Skip to content

Instantly share code, notes, and snippets.

@krystyna93
Created June 10, 2023 08:20
Show Gist options
  • Save krystyna93/d261ddf8126a717e733dc84299e62bfc to your computer and use it in GitHub Desktop.
Save krystyna93/d261ddf8126a717e733dc84299e62bfc to your computer and use it in GitHub Desktop.
webpack sample
import './scss/style.scss';
// Example jQuery usage:
$(document).ready(() => {
$('h1').css('color', 'red');
});
{
"name": "my-project",
"version": "1.0.0",
"description": "My awesome project",
"main": "index.js",
"scripts": {
"start": "webpack serve --mode development --open",
"build": "webpack --mode production"
},
"keywords": [
"webpack",
"normalize.css",
"sass",
"css"
],
"author": "Your Name",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.14.8",
"@babel/preset-env": "^7.14.9",
"babel-loader": "^8.2.2",
"clean-webpack-plugin": "^4.0.0",
"css-loader": "^6.3.0",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.3.2",
"mini-css-extract-plugin": "^2.2.0",
"node-sass": "^6.0.1",
"optimize-css-assets-webpack-plugin": "^6.0.1",
"postcss-loader": "^6.1.1",
"sass-loader": "^12.1.0",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.2.3",
"webpack": "^5.38.1",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"jquery": "^3.6.0",
"normalize.css": "^8.0.1"
}
}
const path = require('path');
const settings = require('./settings');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = (env, options) => {
const isDevelopment = options.mode === 'development';
return {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
devServer: {
contentBase: './dist',
watchContentBase: true,
watchOptions: {
poll: !!parseInt(settings.devServerConfig.poll()),
ignored: /node_modules/,
},
optimization: {
minimizer: [
new TerserJSPlugin({}),
new OptimizeCSSAssetsPlugin({})
],
splitChunks: {
chunks: 'all'
}
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.(sa|sc|c)ss$/,
use: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader'
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.css$/i,
use: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader'
]
}
]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
new MiniCssExtractPlugin({
filename: isDevelopment ? '[name].css' : '[name].[contenthash].css',
chunkFilename: isDevelopment ? '[id].css' : '[id].[contenthash].css'
}),
new HtmlWebpackPlugin({
template: 'src/index.html',
filename: 'index.php',
minify: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
}
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
}),
new CleanWebpackPlugin()
],
devtool: 'eval-cheap-module-source-map',
performance: {
maxAssetSize: 500000,
maxEntrypointSize: 500000
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment