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

395 lines
10 KiB
JavaScript
Raw Normal View History

2024-03-21 14:09:55 +00:00
// (c) Copyright Ascensio System SIA 2009-2024
//
2024-03-17 23:13:02 +00:00
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
2024-03-21 14:09:55 +00:00
//
2024-03-17 23:13:02 +00:00
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
2024-03-21 14:09:55 +00:00
//
2024-03-17 23:13:02 +00:00
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
2024-03-21 14:09:55 +00:00
//
2024-03-17 23:13:02 +00:00
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
2024-03-21 14:09:55 +00:00
//
2024-03-17 23:13:02 +00:00
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
2024-03-21 14:09:55 +00:00
//
2024-03-17 23:13:02 +00:00
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2023-06-15 13:23:03 +00:00
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
2023-07-27 21:24:42 +00:00
const ModuleFederationPlugin =
require("webpack").container.ModuleFederationPlugin;
2023-06-15 13:23:03 +00:00
const DefinePlugin = require("webpack").DefinePlugin;
const BannerPlugin = require("webpack").BannerPlugin;
2023-06-15 13:23:03 +00:00
const ExternalTemplateRemotesPlugin = require("external-remotes-plugin");
const TerserPlugin = require("terser-webpack-plugin");
2024-01-11 13:38:53 +00:00
const minifyJson = require("@docspace/shared/utils/minifyJson");
2024-01-10 12:07:28 +00:00
const sharedDeps = require("@docspace/shared/constants/sharedDependencies");
2023-06-15 13:23:03 +00:00
const path = require("path");
const pkg = require("./package.json");
const deps = pkg.dependencies || {};
2024-01-11 13:38:53 +00:00
const homepage = pkg.homepage;
2023-06-15 13:23:03 +00:00
const title = pkg.title;
const version = pkg.version;
const config = {
entry: "./src/index",
target: "web",
mode: "development",
devServer: {
allowedHosts: "all",
2023-06-15 13:23:03 +00:00
devMiddleware: {
2023-09-04 10:46:15 +00:00
publicPath: "/",
2023-06-15 13:23:03 +00:00
},
static: {
directory: path.join(__dirname, "dist"),
publicPath: homepage,
},
client: {
logging: "info",
// Can be used only for `errors`/`warnings`
//
// overlay: {
// errors: true,
// warnings: true,
// }
overlay: {
warnings: false,
},
progress: true,
},
port: 5015,
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",
},
open: {
target: [`http://localhost:8092`],
app: {
name: "google-chrome",
arguments: ["--incognito", "--new-window"],
},
},
},
resolve: {
extensions: [".jsx", ".js", ".tsx", ".ts", ".json"],
fallback: {
crypto: false,
},
alias: {
PUBLIC_DIR: path.resolve(__dirname, "../../public"),
ASSETS_DIR: path.resolve(__dirname, "./public"),
SRC_DIR: path.resolve(__dirname, "./src"),
2023-07-27 21:24:42 +00:00
CLIENT_PUBLIC_DIR: path.resolve(__dirname, "../client/public"),
2023-06-15 13:23:03 +00:00
PACKAGE_FILE: path.resolve(__dirname, "package.json"),
COMMON_DIR: path.resolve(__dirname, "../common"),
2023-06-15 13:23:03 +00:00
},
},
output: {
publicPath: "auto",
chunkFilename: "static/js/[id].[contenthash].js",
path: path.resolve(process.cwd(), "dist"),
filename: "static/js/[name].[contenthash].bundle.js",
assetModuleFilename: (pathData) => {
//console.log({ pathData });
let result = pathData.filename
.substr(pathData.filename.indexOf("public/"))
.split("/")
.slice(1);
result.pop();
let folder = result.join("/");
folder += result.length === 0 ? "" : "/";
return `static/${folder}[name][ext]?hash=[contenthash]`; // `${folder}/[name].[contenthash][ext]`;
},
},
performance: {
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
module: {
rules: [
{
test: /\.(png|jpe?g|gif|ico|woff2)$/i,
type: "asset/resource",
generator: {
emit: false,
},
},
{
test: /\.svg$/i,
type: "asset/resource",
generator: {
emit: false,
},
resourceQuery: /url/, // *.svg?url
},
{
test: /\.json$/,
resourceQuery: /url/,
use: [
{
loader: "file-loader",
options: {
emitFile: false,
name: (resourcePath) => {
let result = resourcePath
.split(`public${path.sep}`)[1]
.split(path.sep);
result.pop();
let folder = result.join("/");
folder += result.length === 0 ? "" : "/";
return `${folder}[name].[ext]?hash=[contenthash]`; // `${folder}/[name].[contenthash][ext]`;
},
},
},
],
},
{
test: /\.json$/,
resourceQuery: { not: [/url/] }, // exclude if *.json?url,
loader: "json-loader",
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: { not: [/url/] }, // exclude react component if *.svg?url
use: [
{
loader: "@svgr/webpack",
options: {
svgoConfig: {
plugins: [{ removeViewBox: false }],
},
},
},
],
},
{
test: /\.m?js/,
type: "javascript/auto",
resolve: {
fullySpecified: false,
},
},
{
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|ts|tsx)$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: [
"@babel/preset-react",
"@babel/preset-env",
"@babel/preset-typescript",
],
plugins: [
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-export-default-from",
],
},
},
"source-map-loader",
],
},
],
},
plugins: [
new CleanWebpackPlugin(),
new ExternalTemplateRemotesPlugin(),
new CopyPlugin({
patterns: [
// {
// context: path.resolve(__dirname, "public"),
// from: "images/**/*.*",
// },
{
context: path.resolve(__dirname, "public"),
from: "locales/**/*.json",
transform: minifyJson,
},
],
}),
],
};
const getBuildDate = () => {
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
return JSON.stringify(today.toISOString().split(".")[0] + "Z");
};
const getBuildYear = () => {
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
return today.getFullYear();
};
2023-06-15 13:23:03 +00:00
module.exports = (env, argv) => {
config.devtool = "source-map";
2023-06-15 13:23:03 +00:00
if (argv.mode === "production") {
config.mode = "production";
config.optimization = {
splitChunks: { chunks: "all" },
minimize: !env.minimize,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: /\*\s*\(c\)\s+Copyright\s+Ascensio\s+System\s+SIA/i,
},
},
extractComments: false,
}),
],
2023-06-15 13:23:03 +00:00
};
} else {
config.devtool = "cheap-module-source-map";
}
const remotes = {
2023-06-16 09:46:37 +00:00
client: "client@/remoteEntry.js",
2023-06-15 13:23:03 +00:00
};
config.plugins.push(
new ModuleFederationPlugin({
name: "management",
filename: "remoteEntry.js",
remotes: remotes,
exposes: {},
shared: {
...deps,
...sharedDeps,
},
})
);
const htmlTemplate = {
title: title,
template: "./public/index.html",
publicPath: homepage,
base: `${homepage}/`,
};
2023-06-15 13:23:03 +00:00
if (!!env.hideText) {
htmlTemplate.custom = `
<style type="text/css">
div,
p,
a,
span,
button,
h1,
h2,
h3,
h4,
h5,
h6,
::placeholder {
color: rgba(0, 0, 0, 0) !important;
}
</style>`;
2023-06-15 13:23:03 +00:00
}
config.plugins.push(new HtmlWebpackPlugin(htmlTemplate));
2023-06-15 13:23:03 +00:00
const defines = {
VERSION: JSON.stringify(version),
BUILD_AT: DefinePlugin.runtimeValue(function () {
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
return JSON.stringify(today.toISOString().split(".")[0] + "Z");
}, true),
};
config.plugins.push(new DefinePlugin(defines));
config.plugins.push(
new BannerPlugin({
raw: true,
banner: `/*
* (c) Copyright Ascensio System SIA 2009-${getBuildYear()}. All rights reserved
*
* https://www.onlyoffice.com/
*
* Version: ${version} (build: ${getBuildDate()})
*/`,
})
);
2023-06-15 13:23:03 +00:00
return config;
};