DocSpace-buildtools/web/ASC.Web.Editor/webpack.config.js

216 lines
5.3 KiB
JavaScript
Raw Normal View History

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 DefinePlugin = require("webpack").DefinePlugin;
const combineUrl = require("@appserver/common/utils/combineUrl");
const AppServerConfig = require("@appserver/common/constants/AppServerConfig");
const sharedDeps = require("@appserver/common/constants/sharedDependencies");
const { proxyURL } = AppServerConfig;
2021-03-10 15:27:40 +00:00
const path = require("path");
const pkg = require("./package.json");
const deps = pkg.dependencies || {};
2021-03-22 14:35:33 +00:00
const homepage = pkg.homepage; // combineUrl(AppServerConfig.proxyURL, pkg.homepage);
2021-03-10 15:27:40 +00:00
const title = pkg.title;
2021-03-05 18:15:48 +00:00
2021-03-10 15:27:40 +00:00
const config = {
entry: "./src/index",
target: "web",
mode: "development",
2021-03-05 18:15:48 +00:00
devServer: {
devMiddleware: {
publicPath: homepage,
},
static: {
directory: path.join(__dirname, "dist"),
publicPath: homepage,
},
2021-03-05 18:15:48 +00:00
port: 5013,
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",
2021-03-05 18:15:48 +00:00
},
},
resolve: {
extensions: [".jsx", ".js", ".json"],
2021-03-05 18:15:48 +00:00
fallback: {
crypto: false,
},
},
2021-03-10 15:27:40 +00:00
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",
2021-03-10 15:27:40 +00:00
},
performance: {
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
2021-03-05 18:15:48 +00:00
module: {
rules: [
2021-03-10 15:27:40 +00:00
{
2021-03-10 16:33:10 +00:00
test: /\.(png|jpe?g|gif|ico)$/i,
type: "asset/resource",
generator: {
filename: "static/images/[hash][ext][query]",
},
2021-03-10 15:27:40 +00:00
},
2021-03-05 18:15:48 +00:00
{
test: /\.m?js/,
type: "javascript/auto",
2021-03-05 18:15:48 +00:00
resolve: {
fullySpecified: false,
},
},
{
test: /\.react.svg$/,
use: [
{
loader: "@svgr/webpack",
2021-03-05 18:15:48 +00:00
options: {
svgoConfig: {
2021-03-10 15:27:40 +00:00
plugins: [{ removeViewBox: false }],
2021-03-05 18:15:48 +00:00
},
},
},
],
},
{ test: /\.json$/, loader: "json-loader" },
2021-03-10 15:27:40 +00:00
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
2021-03-10 15:27:40 +00:00
},
2021-03-05 18:15:48 +00:00
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
"style-loader",
2021-03-05 18:15:48 +00:00
// 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;
},
},
},
},
2021-03-05 18:15:48 +00:00
// Compiles Sass to CSS
"sass-loader",
2021-03-05 18:15:48 +00:00
],
},
2021-03-05 18:15:48 +00:00
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
2021-03-05 18:15:48 +00:00
options: {
presets: ["@babel/preset-react", "@babel/preset-env"],
2021-03-05 18:15:48 +00:00
plugins: [
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-export-default-from",
2021-03-05 18:15:48 +00:00
],
},
},
"source-map-loader",
2021-03-05 18:15:48 +00:00
],
},
],
},
plugins: [
new CleanWebpackPlugin(),
new ModuleFederationPlugin({
name: "editor",
filename: "remoteEntry.js",
2021-03-26 12:05:41 +00:00
remotes: {
studio: `studio@${combineUrl(proxyURL, "/remoteEntry.js")}`,
files: `files@${combineUrl(
proxyURL,
"/products/files/remoteEntry.js"
)}`,
2021-03-26 12:05:41 +00:00
},
2021-03-05 18:15:48 +00:00
exposes: {
"./app": "./src/Editor.jsx",
2021-03-05 18:15:48 +00:00
},
shared: {
...deps,
...sharedDeps,
2021-03-05 18:15:48 +00:00
},
}),
2021-07-23 14:20:42 +00:00
new ExternalTemplateRemotesPlugin(),
2021-03-05 18:15:48 +00:00
new HtmlWebpackPlugin({
template: "./public/index.html",
2021-03-05 18:15:48 +00:00
publicPath: homepage,
2021-03-10 15:27:40 +00:00
title: title,
base: `${homepage}/`,
2021-03-05 18:15:48 +00:00
}),
2021-03-10 15:27:40 +00:00
new CopyPlugin({
patterns: [
{
from: "public",
2021-03-10 15:27:40 +00:00
globOptions: {
dot: true,
gitignore: true,
ignore: ["**/index.html"],
2021-03-10 15:27:40 +00:00
},
},
],
}),
2021-03-05 18:15:48 +00:00
],
};
module.exports = (env, argv) => {
if (argv.mode === "production") {
config.mode = "production";
2021-03-10 15:27:40 +00:00
config.optimization = {
splitChunks: { chunks: "all" },
2021-12-21 07:59:11 +00:00
minimize: !env.minimize,
2021-03-10 15:27:40 +00:00
minimizer: [new TerserPlugin()],
};
2021-03-05 18:15:48 +00:00
} else {
config.devtool = "cheap-module-source-map";
}
2022-04-28 07:41:27 +00:00
config.plugins.push(
new DefinePlugin({
IS_PERSONAL: env.personal || false,
})
);
2021-03-05 18:15:48 +00:00
return config;
};