Files
mcb-platform-monorepo/packages/webpack-config/lib/config/webpack/WebpackConfigBuilder.js
T
2025-07-21 17:12:41 +03:00

111 lines
4.9 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebpackConfigBuilder = void 0;
const webpack_merge_1 = require("webpack-merge");
const node_path_1 = __importDefault(require("node:path"));
const setDevServer_1 = require("./setDevServer");
const setModuleOptions_1 = require("./setModuleOptions");
const setOptimizationRules_1 = require("./setOptimizationRules");
const setPlugins_1 = require("./setPlugins");
class WebpackConfigBuilder {
constructor(appConfig, mode) {
var _a, _b;
this.config = {};
this.appConfig = appConfig;
this.isDevelopmentMode = mode === 'development';
this.isProductionMode = mode === 'production';
this.cwd = process.cwd();
this.srcPath = node_path_1.default.resolve(this.cwd, ((_a = appConfig.paths) === null || _a === void 0 ? void 0 : _a.srcPath) || 'src');
const entry = ((_b = appConfig.paths) === null || _b === void 0 ? void 0 : _b.entry) || node_path_1.default.resolve(this.srcPath, 'index.ts');
const target = this.isDevelopmentMode ? 'web' : 'browserslist';
const devtool = this.isDevelopmentMode ? 'eval-cheap-module-source-map' : 'source-map';
this.config = {
mode,
target,
devtool,
entry,
};
}
get configuration() {
return this.config;
}
applyOutput() {
var _a, _b, _c;
const port = (_a = this.appConfig.devServerOptions) === null || _a === void 0 ? void 0 : _a.port;
this.config.output = {
path: ((_b = this.appConfig.paths) === null || _b === void 0 ? void 0 : _b.outputPath) || node_path_1.default.resolve(this.cwd, 'dist'),
filename: '[name].[contenthash:8].js',
chunkFilename: '[name].[contenthash:8].js',
publicPath: this.isDevelopmentMode && port ? `http://localhost:${port}/` : ((_c = this.appConfig.paths) === null || _c === void 0 ? void 0 : _c.publicUrl) || '/',
clean: true,
};
}
applyResolve() {
this.config.resolve = {
alias: {
// FSD alias
'@/app': node_path_1.default.resolve(this.srcPath, 'app'),
'@/shared': node_path_1.default.resolve(this.srcPath, 'shared'),
'@/pages': node_path_1.default.resolve(this.srcPath, 'pages'),
'@/widgets': node_path_1.default.resolve(this.srcPath, 'widgets'),
'@/features': node_path_1.default.resolve(this.srcPath, 'features'),
'@/entities': node_path_1.default.resolve(this.srcPath, 'entities'),
// jsx-runtime
'react/jsx-dev-runtime': 'react/jsx-dev-runtime.js',
'react/jsx-runtime': 'react/jsx-runtime.js',
},
extensions: ['.js', '.jsx', '.ts', '.tsx'],
};
}
applyDevServer() {
var _a;
if (this.isDevelopmentMode) {
const devServerOptions = Object.assign(Object.assign({}, this.appConfig.devServerOptions), { port: ((_a = this.appConfig.devServerOptions) === null || _a === void 0 ? void 0 : _a.port) || 3001 });
this.config.devServer = (0, setDevServer_1.setDevServer)(devServerOptions);
}
}
applyPerfomance() {
if (this.isProductionMode) {
this.config.performance = {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
};
}
}
applyOptimizationRules() {
if (this.isProductionMode) {
this.config.optimization = (0, setOptimizationRules_1.setOptimizationRules)(this.appConfig.optimizationOptions);
}
}
applyModuleOptions() {
this.config.module = (0, setModuleOptions_1.setModuleOptions)({
isDevelopmentMode: this.isDevelopmentMode,
isProductionMode: this.isProductionMode,
});
}
applyPlugins() {
var _a;
const publicPath = ((_a = this.appConfig.paths) === null || _a === void 0 ? void 0 : _a.publicPath) || 'public';
this.config.plugins =
(0, setPlugins_1.setPlugins)({
isDevelopmentMode: this.isDevelopmentMode,
isProductionMode: this.isProductionMode,
publicPath,
moduleName: this.appConfig.moduleName,
moduleFederationOptions: this.appConfig.moduleFederationOptions,
}) || [];
const otherPlugins = this.appConfig.plugins;
if (Array.isArray(otherPlugins) && Array.isArray(this.config.plugins)) {
this.config.plugins = this.config.plugins.concat(otherPlugins);
}
}
merge(newConfig) {
(0, webpack_merge_1.merge)(this.config, newConfig);
}
}
exports.WebpackConfigBuilder = WebpackConfigBuilder;