Skip to content

Instantly share code, notes, and snippets.

@handrihmw
Created November 16, 2022 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handrihmw/d5e5e219d0752311d2897e6bbfad0f12 to your computer and use it in GitHub Desktop.
Save handrihmw/d5e5e219d0752311d2897e6bbfad0f12 to your computer and use it in GitHub Desktop.
Webpack Multi
const path = require("path");
const Dotenv = require("dotenv-webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
let appConf = (env) => {
return {
mode: env.NODE_ENV,
devtool: "nosources-source-map",
entry: {
"app.style": "@nstyle/app.scss",
"app.script": "@nscript/app.js"
},
output: {
path: path.resolve(__dirname, "public/assets"),
filename: "js/[name].js",
},
resolve: {
alias: {
"@nstyle": path.resolve(__dirname, "./resources/scss"),
"@nscript": path.resolve(__dirname, "./resources/js")
},
},
devServer: {
port: 8000,
headers: {
"Access-Control-Allow-Origin": "*",
},
},
performance: {
hints: false,
},
stats: {
warnings: false
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.(sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: "css-loader",
options: {
url: false,
},
},
{
loader: "sass-loader",
},
{
loader: "postcss-loader",
},
],
},
{
test: /\.(?:ico|gif|png|jpg|jpeg|webp|svg)$/i,
type: "asset/resource",
use: [
"url-loader?limit=10000",
{
loader: "img-loader",
},
{
loader: "lqip-loader",
options: {
base64: true,
palette: false,
},
},
{
loader: "url-loader",
options: {
limit: 8000,
},
},
{
loader: "svg-url-loader",
options: {
encoding: "base64",
iesafe: true,
},
},
],
},
{
test: /\.(ttf|eot|woff|woff2)|(arunicon\.svg)$/,
type: "asset/inline",
},
],
},
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
"default",
{
discardComments: {
removeAll: true
},
},
],
},
}),
new TerserPlugin({
parallel: true,
terserOptions: {
format: {
comments: false,
},
},
extractComments: true,
}),
],
splitChunks: {
cacheGroups: {
styles: {
test: /\.css$/,
name: "styles",
chunks: "all",
enforce: true,
},
},
},
},
plugins: [
new Dotenv(),
new MiniCssExtractPlugin({
filename: "css/[name].css",
chunkFilename: "css/[name].[chunkhash:4].css",
}),
],
};
};
let pageConf = (env) => {
return {
mode: env.NODE_ENV,
devtool: "nosources-source-map",
entry: {
'homepage': '@nstyle/pages/_homepage.scss',
'about': '@nstyle/pages/_about.scss',
'contact': '@nstyle/pages/_contact.scss',
},
output: {
path: path.resolve(__dirname, "public/assets"),
filename: "js/pages/[name].js",
},
resolve: {
alias: {
"@nstyle": path.resolve(__dirname, "./resources/scss"),
},
},
devServer: {
port: 8000,
headers: {
"Access-Control-Allow-Origin": "*",
},
},
performance: {
hints: false,
},
stats: {
warnings: false
},
module: {
rules: [
{
test: /\.(sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: "css-loader",
options: {
url: false,
},
},
{
loader: "sass-loader",
},
{
loader: "postcss-loader",
},
],
},
{
test: /\.(?:ico|gif|png|jpg|jpeg|webp|svg)$/i,
type: "asset/resource",
use: [
"url-loader?limit=10000",
{
loader: "img-loader",
},
{
loader: "lqip-loader",
options: {
base64: true,
palette: false,
},
},
{
loader: "url-loader",
options: {
limit: 8000,
},
},
{
loader: "svg-url-loader",
options: {
encoding: "base64",
iesafe: true,
},
},
],
},
{
test: /\.(ttf|eot|woff|woff2)|(arunicon\.svg)$/,
type: "asset/inline",
},
],
},
optimization: {
minimize: true,
minimizer: [
new CssMinimizerPlugin({
parallel: true,
minimizerOptions: {
preset: [
"default",
{
discardComments: {
removeAll: true
},
},
],
},
}),
],
splitChunks: {
cacheGroups: {
styles: {
test: /\.css$/,
name: "styles",
chunks: "all",
enforce: true,
},
},
},
},
plugins: [
new Dotenv(),
new MiniCssExtractPlugin({
filename: "css/[name].css",
chunkFilename: "css/[name].[chunkhash:4].css",
}),
],
};
};
module.exports = (env) => {
return [appConf(env), pageConf(env)];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment