Skip to content

Instantly share code, notes, and snippets.

@pcampina
Created October 29, 2021 13:02
Show Gist options
  • Save pcampina/35cb876d3e00387ce2d890d08cf45ea9 to your computer and use it in GitHub Desktop.
Save pcampina/35cb876d3e00387ce2d890d08cf45ea9 to your computer and use it in GitHub Desktop.
Webpack config to work with CSS and JS (Browsersync)
{
"name": "your-project-name",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "webpack --watch",
"build": "webpack --mode=production"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/runtime-corejs2": "^7.11.2",
"@wordpress/babel-preset-default": "^4.19.0",
"autoprefixer": "^10.0.1",
"babel-loader": "^8.1.0",
"browser-sync": "^2.26.13",
"browser-sync-webpack-plugin": "^2.2.2",
"cross-env": "^7.0.2",
"css-loader": "^4.3.0",
"extract-loader": "^5.1.0",
"file-loader": "^6.1.0",
"mini-css-extract-plugin": "^1.3.3",
"node-sass": "^4.0.0",
"optimize-css-assets-webpack-plugin": "^5.0.4",
"postcss-cli": "^8.0.0",
"postcss-loader": "^4.0.3",
"sass-loader": "^10.0.2",
"stylelint": "^13.7.2",
"stylelint-webpack-plugin": "^2.1.1",
"uglifyjs-webpack-plugin": "^2.2.0",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12"
}
}
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
module.exports = {
context: __dirname,
entry: {
frontend: ['./src/js/main.js', './src/css/main.scss'],
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/main.js',
},
mode: 'development',
devtool: 'cheap-eval-source-map',
module: {
rules: [
{
enforce: 'pre',
exclude: /node_modules/,
test: /\.jsx$/,
loader: 'eslint-loader'
},
{
test: /\.jsx?$/,
loader: 'babel-loader'
},
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
},
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
options: {
extract: true,
spriteFilename: 'svg-defs.svg'
}
},
{
test: /\.(jpe?g|png|gif)\$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images/',
name: '[name].[ext]'
}
},
'img-loader'
]
}
]
},
plugins: [
new StyleLintPlugin(),
new MiniCssExtractPlugin({
filename: 'css/main.css'
}),
new BrowserSyncPlugin({
files: '**/*.php',
proxy: 'http://your-project.dev'
})
],
optimization: {
minimizer: [new UglifyJsPlugin(), new OptimizeCssAssetsPlugin()]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment