Last active
November 5, 2020 18:50
-
-
Save web2ls/5745de26f17aa17e1a1c213a77f9496a to your computer and use it in GitHub Desktop.
Basic Webpack 5 + HMR basic setup (multiple files)
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 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'), | |
| entry: './index.js', | |
| output: { | |
| path: path.resolve(__dirname, '../dist'), | |
| filename: filename('js'), | |
| }, | |
| 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', | |
| ], | |
| }, | |
| ] | |
| }, | |
| 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'), | |
| }) | |
| ] | |
| } |
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 webpack = require('webpack'); | |
| const { merge } = require('webpack-merge'); | |
| const common = require('./webpack.common'); | |
| const isProd = process.env.NODE_ENV === 'production'; | |
| const isDev = !isProd; | |
| module.exports = merge(common, { | |
| mode: 'development', | |
| devtool: isDev ? 'source-map' : false, | |
| devServer: { | |
| open: true, | |
| compress: true, | |
| hot: true, | |
| port: 8080, | |
| }, | |
| plugins: [ | |
| new webpack.HotModuleReplacementPlugin(), | |
| ] | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment