DocSpace-client/packages/login/webpack.config.js

212 lines
5.4 KiB
JavaScript

const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack").container
.ModuleFederationPlugin;
const ExternalTemplateRemotesPlugin = require("external-remotes-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const combineUrl = require("@docspace/common/utils/combineUrl");
const minifyJson = require("@docspace/common/utils/minifyJson");
const AppServerConfig = require("@docspace/common/constants/AppServerConfig");
const sharedDeps = require("@docspace/common/constants/sharedDependencies");
const { proxyURL } = AppServerConfig;
const path = require("path");
const pkg = require("./package.json");
const deps = pkg.dependencies || {};
const homepage = pkg.homepage; // combineUrl(proxyURL, pkg.homepage);
const title = pkg.title;
var config = {
entry: "./src/index",
target: "web",
mode: "development",
devServer: {
devMiddleware: {
publicPath: homepage,
},
static: {
directory: path.join(__dirname, "dist"),
publicPath: homepage,
},
port: 5011,
historyApiFallback: {
// Paths with dots should still use the history fallback.
// See https://github.com/facebook/create-react-app/issues/387.
disableDotRule: true,
index: homepage,
},
hot: false,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers":
"X-Requested-With, content-type, Authorization",
},
},
output: {
publicPath: "auto",
chunkFilename: "static/js/[id].[contenthash].js",
//assetModuleFilename: "static/images/[hash][ext][query]",
path: path.resolve(process.cwd(), "dist"),
filename: "static/js/[name].[contenthash].bundle.js",
},
resolve: {
extensions: [".jsx", ".js", ".json"],
fallback: {
crypto: false,
},
alias: {
PUBLIC_DIR: path.resolve(__dirname, "../../public"),
},
},
performance: {
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
module: {
rules: [
{
test: /\.(png|jpe?g|gif|ico)$/i,
type: "asset/resource",
generator: {
filename: "static/images/[hash][ext][query]",
},
},
{
test: /\.m?js/,
type: "javascript/auto",
resolve: {
fullySpecified: false,
},
},
{
test: /\.react.svg$/,
use: [
{
loader: "@svgr/webpack",
options: {
svgoConfig: {
plugins: [{ removeViewBox: false }],
},
},
},
],
},
{ test: /\.json$/, loader: "json-loader" },
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
{
loader: "css-loader",
options: {
url: {
filter: (url, resourcePath) => {
// resourcePath - path to css file
// Don't handle `/static` urls
if (url.startsWith("/static") || url.startsWith("data:")) {
return false;
}
return true;
},
},
},
},
// Compiles Sass to CSS
"sass-loader",
],
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-react", "@babel/preset-env"],
plugins: [
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-export-default-from",
],
},
},
"source-map-loader",
],
},
],
},
plugins: [
new CleanWebpackPlugin(),
new ModuleFederationPlugin({
name: "login",
filename: "remoteEntry.js",
remotes: {
client: `client@${combineUrl(proxyURL, "/remoteEntry.js")}`,
},
exposes: {
"./login": "./src/Login.jsx",
"./roomsLogin": "./src/RoomsLogin.jsx",
"./codeLogin": "./src/CodeLogin.jsx",
"./moreLogin": "./src/sub-components/more-login.js",
},
shared: {
...deps,
...sharedDeps,
},
}),
new ExternalTemplateRemotesPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html",
publicPath: homepage,
title: title,
// templateParameters: {
// proxyURL: proxyURL,
// },
base: `${homepage}/`,
}),
new CopyPlugin({
patterns: [
{
context: path.resolve(__dirname, "public"),
from: "locales/**/*.json",
transform: minifyJson,
},
],
}),
],
};
module.exports = (env, argv) => {
if (argv.mode === "production") {
config.mode = "production";
config.optimization = {
splitChunks: { chunks: "all" },
minimize: !env.minimize,
minimizer: [new TerserPlugin()],
};
} else {
config.devtool = "cheap-module-source-map";
}
return config;
};