Last active
November 5, 2020 18:51
-
-
Save web2ls/2e49e250fa39f6f0ad643ae881404504 to your computer and use it in GitHub Desktop.
Basic Webpack 5 + HMR config setup (single file)
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 webpack = require('webpack'); | |
| const path = require('path'); | |
| const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
| const CopyPlugin = require('copy-webpack-plugin'); | |
| const { CleanWebpackPlugin } = require('clean-webpack-plugin'); | |
| const MiniCssExtractPlugin = require('mini-css-extract-plugin'); | |
| const isProd = process.env.NODE_ENV === 'production'; | |
| const isDev = !isProd; | |
| const filename = ext => isProd ? `bundle.[fullhash].${ext}` : `bundle.${ext}`; | |
| module.exports = { | |
| target: 'web', | |
| context: path.resolve(__dirname, 'src'), | |
| mode: 'development', | |
| entry: './index.js', | |
| output: { | |
| filename: filename('js'), | |
| path: path.resolve(__dirname, 'dist') | |
| }, | |
| resolve: { | |
| extensions: ['.js'], | |
| alias: { | |
| '@': path.resolve(__dirname, 'src'), | |
| '@core': path.resolve(__dirname, 'src/core') | |
| } | |
| }, | |
| module: { | |
| rules: [ | |
| { | |
| test: /\.m?js$/, | |
| exclude: /node_modules/, | |
| use: { | |
| loader: "babel-loader", | |
| options: { | |
| presets: ['@babel/preset-env'] | |
| } | |
| } | |
| }, | |
| { | |
| test: /\.s[ac]ss$/i, | |
| use: [ | |
| MiniCssExtractPlugin.loader, | |
| // Translates CSS into CommonJS | |
| 'css-loader', | |
| // Compiles Sass to CSS | |
| 'sass-loader', | |
| ], | |
| }, | |
| ] | |
| }, | |
| devtool: isDev ? 'source-map' : false, | |
| devServer: { | |
| open: true, | |
| compress: true, | |
| hot: true, | |
| port: 8080, | |
| }, | |
| plugins: [ | |
| new CleanWebpackPlugin(), | |
| new HtmlWebpackPlugin({ | |
| template: 'index.html', | |
| minify: { | |
| removeComments: isProd, | |
| collapseWhitespace: isProd | |
| } | |
| }), | |
| new CopyPlugin({ | |
| patterns: [ | |
| { | |
| from: path.resolve(__dirname, 'src/favicon.ico'), | |
| to: path.resolve(__dirname, 'dist'), | |
| } | |
| ] | |
| }), | |
| new MiniCssExtractPlugin({ | |
| filename: filename('css'), | |
| }), | |
| new webpack.HotModuleReplacementPlugin(), | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment