Last active
April 21, 2025 09:39
-
-
Save krystyna93/bf72b783b495ccd0be602ccb8f5f5e4c to your computer and use it in GitHub Desktop.
classic themeing: webpack sample gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import './scss/style.scss'; | |
// Example jQuery usage: | |
$(document).ready(() => { | |
$('h1').css('color', 'red'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "fictional-university-theme", | |
"version": "1.0.0", | |
"description": "A fictional university WordPress theme.", | |
"main": "index.js", | |
"scripts": { | |
"dev": "webpack serve --config webpack.config.js --env.mode=development", | |
"build": "webpack --config webpack.config.js --env.mode=production" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@babel/core": "^7.15.8", | |
"@babel/preset-env": "^7.15.8", | |
"@babel/preset-react": "^7.14.5", | |
"autoprefixer": "^10.4.2", | |
"babel-loader": "^8.2.3", | |
"clean-webpack-plugin": "^4.0.0", | |
"css-loader": "^6.5.1", | |
"css-minimizer-webpack-plugin": "^7.0.0", | |
"fse": "^3.0.1", | |
"mini-css-extract-plugin": "^2.5.2", | |
"postcss-color-function": "^5.0.0", | |
"postcss-hexrgba": "^4.0.0", | |
"postcss-import": "^14.0.2", | |
"postcss-mixins": "^7.0.0", | |
"postcss-nested": "^5.0.5", | |
"postcss-simple-vars": "^6.0.3", | |
"postcss-loader": "^6.2.1", | |
"terser-webpack-plugin": "^6.0.0", | |
"webpack": "^5.64.4", | |
"webpack-cli": "^4.9.1", | |
"webpack-dev-server": "^4.6.0", | |
"webpack-manifest-plugin": "^5.3.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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