Created
March 11, 2021 08:56
-
-
Save hoangbits/d17ede93d6716426351e108b5605d985 to your computer and use it in GitHub Desktop.
typescript, scss module react webpackt
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 LinkTypePlugin = require("html-webpack-link-type-plugin") | |
.HtmlWebpackLinkTypePlugin; | |
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer") | |
.BundleAnalyzerPlugin; | |
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); | |
const { CleanWebpackPlugin } = require("clean-webpack-plugin"); | |
const CopyPlugin = require("copy-webpack-plugin"); | |
const Dotenv = require("dotenv-webpack"); | |
const isDev = process.env.NODE_ENV === "dev"; | |
const config = { | |
entry: ["react-hot-loader/patch", "./src/index.tsx"], | |
output: { | |
path: path.resolve(__dirname, "dist"), | |
filename: "[name].[contenthash].js", | |
publicPath: "/", | |
}, | |
devtool: process.env.NODE_ENV !== "prod" ? "inline-source-map" : "source-map", | |
module: { | |
rules: [ | |
{ | |
test: /\.css$/, | |
use: [ | |
isDev ? "style-loader" : MiniCssExtractPlugin.loader, | |
{ | |
loader: "css-loader", | |
options: { | |
importLoaders: 1, | |
}, | |
}, | |
"postcss-loader", | |
], | |
exclude: /\.module\.css$/, | |
}, | |
{ | |
test: /\.(js|jsx|ts|tsx)$/, | |
use: "babel-loader", | |
exclude: /node_modules/, | |
}, | |
{ | |
test: /\.css$/, | |
use: [ | |
isDev ? "style-loader" : MiniCssExtractPlugin.loader, | |
{ | |
loader: "css-loader", | |
options: { | |
importLoaders: 1, | |
modules: true, | |
}, | |
}, | |
"postcss-loader", | |
], | |
include: /\.module\.css$/, | |
}, | |
{ | |
test: /\.scss$/, | |
use: [ | |
isDev ? "style-loader" : MiniCssExtractPlugin.loader, | |
"css-loader", | |
{ loader: "sass-loader", options: { sourceMap: isDev } }, | |
], | |
exclude: /\.module\.scss$/, | |
}, | |
{ | |
test: /\.scss$/, | |
use: [ | |
isDev ? "style-loader" : MiniCssExtractPlugin.loader, | |
{ | |
loader: "css-loader", | |
options: { | |
modules: true, | |
sourceMap: isDev, | |
}, | |
}, | |
{ | |
loader: "sass-loader", | |
options: { | |
sourceMap: isDev, | |
}, | |
}, | |
], | |
include: /\.module\.scss$/, | |
}, | |
{ | |
test: /\.svg$/, | |
use: "file-loader", | |
}, | |
{ | |
test: /\.png$/, | |
use: [ | |
{ | |
loader: "url-loader", | |
options: { | |
mimetype: "image/png", | |
}, | |
}, | |
], | |
}, | |
], | |
}, | |
resolve: { | |
extensions: [".js", ".jsx", ".tsx", ".ts"], | |
alias: { | |
"react-dom": "@hot-loader/react-dom", | |
"@": path.resolve(__dirname, "src"), | |
}, | |
}, | |
plugins: [ | |
new Dotenv({ | |
path: "./environments/.env", // load this now instead of the ones in '.env' | |
safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file. | |
allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing) | |
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs. | |
silent: true, // hide any errors | |
defaults: false, // load '.env.defaults' as the default values if empty. | |
}), | |
new MiniCssExtractPlugin({ | |
filename: "[name].css", | |
chunkFilename: "[name].[id].css", | |
}), | |
new HtmlWebpackPlugin({ | |
title: "CSA", | |
template: "src/index.html", | |
favicon: "./public/favicon.ico", | |
xhtml: true, // to have /> for self closing tag | |
}), | |
new LinkTypePlugin({ | |
"**/*.css": "text/css", | |
}), | |
new CopyPlugin({ | |
patterns: [{ from: "src/assets", to: "assets" }], | |
}), | |
new BundleAnalyzerPlugin({ | |
analyzerMode: "static", | |
openAnalyzer: false, | |
}), | |
new CleanWebpackPlugin(), | |
], | |
devServer: { | |
contentBase: "./dist", | |
publicPath: "/", | |
host: "0.0.0.0", | |
port: 48080, | |
disableHostCheck: true, | |
historyApiFallback: true, | |
}, | |
optimization: { | |
runtimeChunk: "single", | |
splitChunks: { | |
cacheGroups: { | |
vendor: { | |
test: /[\\/]node_modules[\\/]/, | |
name: "vendors", | |
chunks: "all", | |
}, | |
}, | |
}, | |
}, | |
}; | |
module.exports = (env, argv) => { | |
if (argv.hot) { | |
// Cannot use 'contenthash' when hot reloading is enabled. | |
config.output.filename = "[name].[hash].js"; | |
} | |
return config; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment