@gravity-ui/app-builder 0.15.0 → 0.15.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.webpackConfigFactory = webpackConfigFactory;
30
+ exports.rspackConfigFactory = rspackConfigFactory;
30
31
  exports.configureModuleRules = configureModuleRules;
31
32
  exports.configureResolve = configureResolve;
32
33
  exports.configureOptimization = configureOptimization;
@@ -43,6 +44,11 @@ const react_refresh_webpack_plugin_1 = __importDefault(require("@pmmmwh/react-re
43
44
  const moment_timezone_data_webpack_plugin_1 = __importDefault(require("moment-timezone-data-webpack-plugin"));
44
45
  const webpack_plugin_1 = __importDefault(require("@statoscope/webpack-plugin"));
45
46
  const circular_dependency_plugin_1 = __importDefault(require("circular-dependency-plugin"));
47
+ const core_1 = require("@rspack/core");
48
+ const rspack_manifest_plugin_1 = require("rspack-manifest-plugin");
49
+ const rspack_1 = require("./rspack");
50
+ const ts_checker_rspack_plugin_1 = require("ts-checker-rspack-plugin");
51
+ const plugin_react_refresh_1 = __importDefault(require("@rspack/plugin-react-refresh"));
46
52
  const paths_1 = __importDefault(require("../paths"));
47
53
  const babel_1 = require("../babel");
48
54
  const progress_plugin_1 = require("./progress-plugin");
@@ -53,10 +59,11 @@ const utils_2 = require("../typescript/utils");
53
59
  const node_externals_1 = require("./node-externals");
54
60
  const imagesSizeLimit = 2048;
55
61
  const fontSizeLimit = 8192;
56
- async function webpackConfigFactory(webpackMode, config, { logger, isSsr = false } = {}) {
62
+ const assetsManifestFile = 'assets-manifest.json';
63
+ function getHelperOptions({ webpackMode, config, logger, isSsr = false, }) {
57
64
  const isEnvDevelopment = webpackMode === "development" /* WebpackMode.Dev */;
58
65
  const isEnvProduction = webpackMode === "production" /* WebpackMode.Prod */;
59
- const helperOptions = {
66
+ return {
60
67
  config,
61
68
  logger,
62
69
  isEnvDevelopment,
@@ -66,6 +73,11 @@ async function webpackConfigFactory(webpackMode, config, { logger, isSsr = false
66
73
  entriesDirectory: isSsr ? paths_1.default.appSsrEntry : paths_1.default.appEntry,
67
74
  isSsr,
68
75
  };
76
+ }
77
+ async function webpackConfigFactory(options) {
78
+ const { config } = options;
79
+ const helperOptions = getHelperOptions(options);
80
+ const { isSsr, isEnvProduction } = helperOptions;
69
81
  let externals = config.externals;
70
82
  if (isSsr) {
71
83
  externals =
@@ -77,7 +89,7 @@ async function webpackConfigFactory(webpackMode, config, { logger, isSsr = false
77
89
  });
78
90
  }
79
91
  let webpackConfig = {
80
- mode: webpackMode,
92
+ mode: isEnvProduction ? 'production' : 'development',
81
93
  context: paths_1.default.app,
82
94
  bail: isEnvProduction,
83
95
  target: isSsr ? 'node' : undefined,
@@ -88,7 +100,7 @@ async function webpackConfigFactory(webpackMode, config, { logger, isSsr = false
88
100
  module: {
89
101
  rules: configureModuleRules(helperOptions),
90
102
  },
91
- plugins: configurePlugins(helperOptions),
103
+ plugins: configureWebpackPlugins(helperOptions),
92
104
  optimization: configureOptimization(helperOptions),
93
105
  externals,
94
106
  node: config.node,
@@ -106,12 +118,57 @@ async function webpackConfigFactory(webpackMode, config, { logger, isSsr = false
106
118
  },
107
119
  cache: config.cache,
108
120
  };
109
- webpackConfig = await config.webpack(webpackConfig, { configType: webpackMode, isSsr });
121
+ webpackConfig = await config.webpack(webpackConfig, {
122
+ configType: isEnvProduction ? 'production' : 'development',
123
+ isSsr,
124
+ });
110
125
  if (config.debugWebpack) {
111
126
  (0, log_config_1.logConfig)('Preview webpack config', webpackConfig);
112
127
  }
113
128
  return webpackConfig;
114
129
  }
130
+ async function rspackConfigFactory(options) {
131
+ const { config } = options;
132
+ const helperOptions = getHelperOptions(options);
133
+ const { isSsr, isEnvProduction, isEnvDevelopment } = helperOptions;
134
+ // Cache is required for lazy compilation
135
+ const cache = Boolean(config.cache) || (isEnvDevelopment && Boolean(config.lazyCompilation));
136
+ let rspackConfig = {
137
+ mode: isEnvProduction ? 'production' : 'development',
138
+ context: paths_1.default.app,
139
+ bail: isEnvProduction,
140
+ target: isSsr ? 'node' : undefined,
141
+ devtool: configureDevTool(helperOptions),
142
+ entry: configureEntry(helperOptions),
143
+ output: configureRspackOutput(helperOptions),
144
+ resolve: configureRspackResolve(helperOptions),
145
+ module: {
146
+ rules: (0, rspack_1.prepareRspackRules)(configureModuleRules(helperOptions)),
147
+ },
148
+ plugins: configureRspackPlugins(helperOptions),
149
+ optimization: configureRspackOptimization(helperOptions),
150
+ // TODO externals,
151
+ node: config.node,
152
+ watchOptions: configureWatchOptions(helperOptions),
153
+ ignoreWarnings: [/Failed to parse source map/],
154
+ infrastructureLogging: config.verbose
155
+ ? {
156
+ colors: true,
157
+ level: 'verbose',
158
+ }
159
+ : undefined,
160
+ experiments: configureRspackExperiments(helperOptions),
161
+ cache,
162
+ };
163
+ rspackConfig = await config.rspack(rspackConfig, {
164
+ configType: isEnvProduction ? 'production' : 'development',
165
+ isSsr,
166
+ });
167
+ if (config.debugWebpack) {
168
+ (0, log_config_1.logConfig)('Preview rspack config', rspackConfig);
169
+ }
170
+ return rspackConfig;
171
+ }
115
172
  function configureModuleRules(helperOptions, additionalRules = []) {
116
173
  const jsLoader = createJavaScriptLoader(helperOptions);
117
174
  return [
@@ -182,6 +239,56 @@ function configureExperiments({ config, isEnvProduction, isSsr, }) {
182
239
  lazyCompilation,
183
240
  };
184
241
  }
242
+ function configureRspackExperiments({ config, isEnvProduction, isSsr, }) {
243
+ if (isSsr) {
244
+ return config.ssr?.moduleType === 'esm' ? { outputModule: true } : undefined;
245
+ }
246
+ if (isEnvProduction) {
247
+ return undefined;
248
+ }
249
+ let lazyCompilation;
250
+ let port;
251
+ if (config.lazyCompilation) {
252
+ if (typeof config.lazyCompilation === 'object') {
253
+ port = config.lazyCompilation.port;
254
+ }
255
+ lazyCompilation = {
256
+ // Lazy compilation works without problems only with lazy imports
257
+ // See https://github.com/web-infra-dev/rspack/issues/8503
258
+ entries: false,
259
+ imports: true,
260
+ backend: {
261
+ client: require.resolve('./lazy-client.js'),
262
+ ...(port
263
+ ? {
264
+ listen: {
265
+ port,
266
+ },
267
+ }
268
+ : {}),
269
+ },
270
+ test(module) {
271
+ // make sure that lazy-client.js won't be lazy compiled)
272
+ return !module.nameForCondition().endsWith('lazy-client.js');
273
+ },
274
+ };
275
+ }
276
+ return {
277
+ cache: {
278
+ type: 'persistent',
279
+ snapshot: {
280
+ managedPaths: config.watchOptions?.watchPackages ? [] : undefined,
281
+ },
282
+ storage: {
283
+ type: 'filesystem',
284
+ directory: typeof config.cache === 'object' && 'cacheDirectory' in config.cache
285
+ ? config.cache.cacheDirectory
286
+ : undefined,
287
+ },
288
+ },
289
+ lazyCompilation,
290
+ };
291
+ }
185
292
  function configureResolve({ isEnvProduction, config }) {
186
293
  const alias = { ...config.alias };
187
294
  for (const [key, value] of Object.entries(alias)) {
@@ -203,6 +310,16 @@ function configureResolve({ isEnvProduction, config }) {
203
310
  fallback: config.fallback,
204
311
  };
205
312
  }
313
+ function configureRspackResolve(helperOptions) {
314
+ const { alias, modules, extensions, symlinks, fallback } = configureResolve(helperOptions);
315
+ return {
316
+ alias: Array.isArray(alias) ? undefined : alias,
317
+ modules,
318
+ extensions,
319
+ symlinks,
320
+ fallback: Array.isArray(fallback) ? undefined : fallback,
321
+ };
322
+ }
206
323
  function createEntryArray(entry) {
207
324
  return [require.resolve('./public-path'), entry];
208
325
  }
@@ -249,6 +366,18 @@ function configureOutput(options) {
249
366
  ...ssrOptions,
250
367
  };
251
368
  }
369
+ function configureRspackOutput(options) {
370
+ const { filename, chunkFilename, library, chunkFormat, path, pathinfo } = configureOutput(options);
371
+ return {
372
+ filename: typeof filename === 'string' ? filename : undefined,
373
+ chunkFilename: typeof chunkFilename === 'string' ? chunkFilename : undefined,
374
+ library,
375
+ chunkFormat,
376
+ path,
377
+ pathinfo,
378
+ clean: false,
379
+ };
380
+ }
252
381
  function createJavaScriptLoader({ isEnvProduction, isEnvDevelopment, configType, config, isSsr, }) {
253
382
  const plugins = [];
254
383
  if (!isSsr) {
@@ -326,7 +455,9 @@ function createWorkerRule(options) {
326
455
  loader: require.resolve('./worker/worker-loader'),
327
456
  }
328
457
  : {
329
- loader: require.resolve('worker-loader'),
458
+ loader: require.resolve(options.config.bundler === 'rspack'
459
+ ? 'worker-rspack-loader'
460
+ : 'worker-loader'),
330
461
  // currently workers located on cdn are not working properly, so we are enforcing loading workers from
331
462
  // service instead
332
463
  options: {
@@ -370,6 +501,7 @@ function createStylesRule(options) {
370
501
  };
371
502
  }
372
503
  function getCssLoaders({ isEnvDevelopment, isEnvProduction, config, isSsr }, additionalRules) {
504
+ const isRspack = config.bundler === 'rspack';
373
505
  const loaders = [];
374
506
  if (!config.transformCssWithLightningCss) {
375
507
  loaders.push({
@@ -409,11 +541,17 @@ function getCssLoaders({ isEnvDevelopment, isEnvProduction, config, isSsr }, add
409
541
  },
410
542
  });
411
543
  if (isEnvProduction) {
412
- loaders.unshift({ loader: mini_css_extract_plugin_1.default.loader, options: { emit: !isSsr } });
544
+ loaders.unshift({
545
+ loader: isRspack ? core_1.CssExtractRspackPlugin.loader : mini_css_extract_plugin_1.default.loader,
546
+ options: { emit: !isSsr },
547
+ });
413
548
  }
414
549
  if (isEnvDevelopment) {
415
550
  if (isSsr || config.ssr) {
416
- loaders.unshift({ loader: mini_css_extract_plugin_1.default.loader, options: { emit: !isSsr } });
551
+ loaders.unshift({
552
+ loader: isRspack ? core_1.CssExtractRspackPlugin.loader : mini_css_extract_plugin_1.default.loader,
553
+ options: { emit: !isSsr },
554
+ });
417
555
  }
418
556
  else {
419
557
  loaders.unshift({
@@ -567,47 +705,42 @@ function createMomentTimezoneDataPlugin(options = {}) {
567
705
  const endYear = options.endYear ?? currentYear;
568
706
  return new moment_timezone_data_webpack_plugin_1.default({ ...options, startYear, endYear });
569
707
  }
570
- function configurePlugins(options) {
571
- const { isEnvDevelopment, isEnvProduction, config, isSsr } = options;
572
- const excludeFromClean = config.excludeFromClean || [];
573
- const manifestFile = 'assets-manifest.json';
574
- const plugins = [
575
- new clean_webpack_plugin_1.CleanWebpackPlugin({
576
- verbose: config.verbose,
577
- cleanOnceBeforeBuildPatterns: [
578
- '**/*',
579
- ...(isEnvDevelopment ? ['!manifest.json'] : []),
580
- ...excludeFromClean,
581
- ],
582
- }),
583
- new webpack_manifest_plugin_1.WebpackManifestPlugin({
584
- writeToFileEmit: true,
585
- publicPath: '',
586
- }),
587
- new webpack_assets_manifest_1.default(isEnvProduction
588
- ? {
589
- entrypoints: true,
590
- output: manifestFile,
591
- }
592
- : {
593
- entrypoints: true,
594
- writeToDisk: true,
595
- output: path.resolve(options.buildDirectory, manifestFile),
596
- }),
597
- new webpack.DefinePlugin({
598
- 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
599
- 'process.env.IS_SSR': JSON.stringify(isSsr),
600
- ...config.definitions,
601
- }),
602
- ];
603
- if (options.logger) {
604
- plugins.push(new progress_plugin_1.ProgressPlugin({ logger: options.logger }));
708
+ function getDefinitions({ config, isSsr }) {
709
+ return {
710
+ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
711
+ 'process.env.IS_SSR': JSON.stringify(isSsr),
712
+ ...config.definitions,
713
+ };
714
+ }
715
+ function getContextReplacements({ config, isSsr }) {
716
+ const contextReplacement = config.contextReplacement || {};
717
+ if (isSsr) {
718
+ return [];
605
719
  }
606
- if (process.env.WEBPACK_PROFILE === 'true') {
607
- plugins.push(new webpack.debug.ProfilingPlugin());
720
+ const replacements = [
721
+ {
722
+ resourceRegExp: /moment[\\/]locale$/, // eslint-disable-next-line security/detect-non-literal-regexp
723
+ newResource: new RegExp(`^\\./(${(contextReplacement.locale || ['ru']).join('|')})$`),
724
+ },
725
+ {
726
+ resourceRegExp: /dayjs[\\/]locale$/,
727
+ // eslint-disable-next-line security/detect-non-literal-regexp
728
+ newResource: new RegExp(`^\\./(${(contextReplacement.locale || ['ru']).join('|')})\\.js$`),
729
+ },
730
+ ];
731
+ if (contextReplacement['highlight.js']) {
732
+ replacements.push({
733
+ resourceRegExp: /highlight\.js[\\/]lib[\\/]languages$/,
734
+ // eslint-disable-next-line security/detect-non-literal-regexp
735
+ newResource: new RegExp(`^\\./(${contextReplacement['highlight.js'].join('|')})$`),
736
+ });
608
737
  }
609
- if (config.forkTsChecker !== false) {
610
- plugins.push(new fork_ts_checker_webpack_plugin_1.default({
738
+ return replacements;
739
+ }
740
+ function getForkTsCheckerOptions({ config, }) {
741
+ return config.forkTsChecker === false
742
+ ? undefined
743
+ : {
611
744
  ...config.forkTsChecker,
612
745
  typescript: {
613
746
  typescriptPath: (0, utils_2.resolveTypescript)(),
@@ -618,8 +751,30 @@ function configurePlugins(options) {
618
751
  mode: 'write-references',
619
752
  ...config.forkTsChecker?.typescript,
620
753
  },
621
- }));
622
- }
754
+ };
755
+ }
756
+ function getCssExtractPluginOptions({ isEnvProduction }) {
757
+ return {
758
+ filename: isEnvProduction ? 'css/[name].[contenthash:8].css' : 'css/[name].css',
759
+ chunkFilename: isEnvProduction
760
+ ? 'css/[name].[contenthash:8].chunk.css'
761
+ : 'css/[name].chunk.css',
762
+ ignoreOrder: true,
763
+ };
764
+ }
765
+ function configureCommonPlugins(options) {
766
+ const { isEnvDevelopment, isEnvProduction, config, isSsr } = options;
767
+ const excludeFromClean = config.excludeFromClean || [];
768
+ const plugins = [
769
+ new clean_webpack_plugin_1.CleanWebpackPlugin({
770
+ verbose: config.verbose,
771
+ cleanOnceBeforeBuildPatterns: [
772
+ '**/*',
773
+ ...(isEnvDevelopment ? ['!manifest.json'] : []),
774
+ ...excludeFromClean,
775
+ ],
776
+ }),
777
+ ];
623
778
  if (config.detectCircularDependencies) {
624
779
  let circularPluginOptions = {
625
780
  exclude: /node_modules/,
@@ -638,27 +793,6 @@ function configurePlugins(options) {
638
793
  reportFilename: 'stats.html',
639
794
  }));
640
795
  }
641
- if (config.analyzeBundle === 'statoscope') {
642
- const customStatoscopeConfig = config.statoscopeConfig || {};
643
- plugins.push(new webpack_plugin_1.default({
644
- saveReportTo: path.resolve(options.buildDirectory, 'report.html'),
645
- saveStatsTo: path.resolve(options.buildDirectory, 'stats.json'),
646
- open: false,
647
- statsOptions: {
648
- all: true,
649
- },
650
- ...customStatoscopeConfig,
651
- }));
652
- }
653
- }
654
- if (isEnvProduction || isSsr || config.ssr) {
655
- plugins.push(new mini_css_extract_plugin_1.default({
656
- filename: isEnvProduction ? 'css/[name].[contenthash:8].css' : 'css/[name].css',
657
- chunkFilename: isEnvProduction
658
- ? 'css/[name].[contenthash:8].chunk.css'
659
- : 'css/[name].chunk.css',
660
- ignoreOrder: true,
661
- }));
662
796
  }
663
797
  if (!isSsr) {
664
798
  if (config.monaco) {
@@ -671,84 +805,145 @@ function configurePlugins(options) {
671
805
  publicPath: path.normalize(config.publicPathPrefix + '/build/'),
672
806
  }));
673
807
  }
674
- const contextReplacement = config.contextReplacement || {};
675
808
  plugins.push(createMomentTimezoneDataPlugin(config.momentTz));
676
- plugins.push(new webpack.ContextReplacementPlugin(/moment[\\/]locale$/,
677
- // eslint-disable-next-line security/detect-non-literal-regexp
678
- new RegExp(`^\\./(${(contextReplacement.locale || ['ru']).join('|')})$`)));
679
- plugins.push(new webpack.ContextReplacementPlugin(/dayjs[\\/]locale$/,
680
- // eslint-disable-next-line security/detect-non-literal-regexp
681
- new RegExp(`^\\./(${(contextReplacement.locale || ['ru']).join('|')})\\.js$`)));
682
- if (contextReplacement['highlight.js']) {
683
- plugins.push(new webpack.ContextReplacementPlugin(/highlight\.js[\\/]lib[\\/]languages$/,
684
- // eslint-disable-next-line security/detect-non-literal-regexp
685
- new RegExp(`^\\./(${contextReplacement['highlight.js'].join('|')})$`)));
809
+ }
810
+ if (isEnvProduction) {
811
+ if (config.sentryConfig) {
812
+ const sentryPlugin = require('@sentry/webpack-plugin').sentryWebpackPlugin;
813
+ plugins.push(sentryPlugin({ ...config.sentryConfig }));
686
814
  }
815
+ }
816
+ if (config.cdn) {
817
+ plugins.push(...(0, s3_upload_1.createS3UploadPlugins)(config, options.logger));
818
+ }
819
+ return plugins;
820
+ }
821
+ function configureWebpackPlugins(options) {
822
+ const { isEnvDevelopment, isEnvProduction, config, isSsr } = options;
823
+ const forkTsCheckerOptions = getForkTsCheckerOptions(options);
824
+ const webpackPlugins = [
825
+ ...configureCommonPlugins(options),
826
+ ...(options.logger ? [new progress_plugin_1.ProgressPlugin({ logger: options.logger })] : []),
827
+ new webpack.DefinePlugin(getDefinitions(options)),
828
+ new webpack_manifest_plugin_1.WebpackManifestPlugin({
829
+ writeToFileEmit: true,
830
+ publicPath: '',
831
+ }),
832
+ new webpack_assets_manifest_1.default(isEnvProduction
833
+ ? {
834
+ entrypoints: true,
835
+ output: assetsManifestFile,
836
+ }
837
+ : {
838
+ entrypoints: true,
839
+ writeToDisk: true,
840
+ output: path.resolve(options.buildDirectory, assetsManifestFile),
841
+ }),
842
+ ...(process.env.WEBPACK_PROFILE === 'true' ? [new webpack.debug.ProfilingPlugin()] : []),
843
+ ...(forkTsCheckerOptions ? [new fork_ts_checker_webpack_plugin_1.default(forkTsCheckerOptions)] : []),
844
+ ];
845
+ if (isEnvProduction || isSsr || config.ssr) {
846
+ webpackPlugins.push(new mini_css_extract_plugin_1.default(getCssExtractPluginOptions(options)));
847
+ }
848
+ if (isEnvProduction) {
849
+ if (config.analyzeBundle === 'statoscope') {
850
+ const customStatoscopeConfig = config.statoscopeConfig || {};
851
+ webpackPlugins.push(new webpack_plugin_1.default({
852
+ saveReportTo: path.resolve(options.buildDirectory, 'report.html'),
853
+ saveStatsTo: path.resolve(options.buildDirectory, 'stats.json'),
854
+ open: false,
855
+ statsOptions: {
856
+ all: true,
857
+ },
858
+ ...customStatoscopeConfig,
859
+ }));
860
+ }
861
+ if (config.analyzeBundle === 'rsdoctor') {
862
+ const { RsdoctorWebpackPlugin } = require('@rsdoctor/webpack-plugin');
863
+ webpackPlugins.push(new RsdoctorWebpackPlugin({
864
+ mode: 'brief',
865
+ }));
866
+ }
867
+ }
868
+ if (!isSsr) {
869
+ const contextReplacements = getContextReplacements(options);
870
+ contextReplacements.forEach(({ resourceRegExp, newResource }) => webpackPlugins.push(new webpack.ContextReplacementPlugin(resourceRegExp, newResource)));
687
871
  if (isEnvDevelopment && config.reactRefresh !== false) {
688
872
  const { webSocketPath = path.normalize(`/${config.publicPathPrefix}/build/sockjs-node`), } = config.devServer || {};
689
- plugins.push(new react_refresh_webpack_plugin_1.default(config.reactRefresh({
873
+ const reactRefreshConfig = config.reactRefresh({
690
874
  overlay: { sockPath: webSocketPath },
691
875
  exclude: [/node_modules/, /\.worker\.[jt]sx?$/],
692
- })));
876
+ });
877
+ webpackPlugins.push(new react_refresh_webpack_plugin_1.default(reactRefreshConfig));
693
878
  }
694
879
  if (config.polyfill?.process) {
695
- plugins.push(new webpack.ProvidePlugin({ process: 'process/browser.js' }));
880
+ webpackPlugins.push(new webpack.ProvidePlugin({ process: 'process/browser.js' }));
696
881
  }
697
- if (isEnvProduction) {
698
- if (config.sentryConfig) {
699
- const sentryPlugin = require('@sentry/webpack-plugin').sentryWebpackPlugin;
700
- plugins.push(sentryPlugin({ ...config.sentryConfig }));
701
- }
882
+ }
883
+ return webpackPlugins;
884
+ }
885
+ function configureRspackPlugins(options) {
886
+ const { isEnvDevelopment, isEnvProduction, config, isSsr } = options;
887
+ const forkTsCheckerOptions = getForkTsCheckerOptions(options);
888
+ const rspackPlugins = [
889
+ ...configureCommonPlugins(options),
890
+ ...(options.logger ? [new rspack_1.RspackProgressPlugin({ logger: options.logger })] : []),
891
+ new core_1.rspack.DefinePlugin(getDefinitions(options)),
892
+ new rspack_manifest_plugin_1.RspackManifestPlugin({
893
+ fileName: isEnvProduction
894
+ ? assetsManifestFile
895
+ : path.resolve(paths_1.default.appBuild, assetsManifestFile),
896
+ writeToFileEmit: true,
897
+ useLegacyEmit: true,
898
+ publicPath: '',
899
+ generate: rspack_1.generateAssetsManifest,
900
+ }),
901
+ ...(forkTsCheckerOptions ? [new ts_checker_rspack_plugin_1.TsCheckerRspackPlugin(forkTsCheckerOptions)] : []),
902
+ ];
903
+ if (isEnvProduction || isSsr || config.ssr) {
904
+ rspackPlugins.push(new core_1.CssExtractRspackPlugin(getCssExtractPluginOptions(options)));
905
+ }
906
+ if (isEnvProduction) {
907
+ if (config.analyzeBundle === 'rsdoctor') {
908
+ const { RsdoctorRspackPlugin } = require('@rsdoctor/rspack-plugin');
909
+ rspackPlugins.push(new RsdoctorRspackPlugin({
910
+ mode: 'brief',
911
+ }));
702
912
  }
703
- if (config.cdn) {
704
- let credentialsGlobal;
705
- if (process.env.FRONTEND_S3_ACCESS_KEY_ID &&
706
- process.env.FRONTEND_S3_SECRET_ACCESS_KEY) {
707
- credentialsGlobal = {
708
- accessKeyId: process.env.FRONTEND_S3_ACCESS_KEY_ID,
709
- secretAccessKey: process.env.FRONTEND_S3_SECRET_ACCESS_KEY,
710
- };
711
- }
712
- const cdns = Array.isArray(config.cdn) ? config.cdn : [config.cdn];
713
- for (let index = 0; index < cdns.length; index++) {
714
- const cdn = cdns[index];
715
- if (!cdn) {
716
- continue;
717
- }
718
- let credentials = credentialsGlobal;
719
- const accessKeyId = process.env[`FRONTEND_S3_ACCESS_KEY_ID_${index}`];
720
- const secretAccessKey = process.env[`FRONTEND_S3_SECRET_ACCESS_KEY_${index}`];
721
- if (accessKeyId && secretAccessKey) {
722
- credentials = {
723
- accessKeyId,
724
- secretAccessKey,
725
- };
726
- }
727
- plugins.push(new s3_upload_1.S3UploadPlugin({
728
- exclude: config.hiddenSourceMap ? /\.map$/ : undefined,
729
- compress: cdn.compress,
730
- s3ClientOptions: {
731
- region: cdn.region,
732
- endpoint: cdn.endpoint,
733
- credentials,
734
- },
735
- s3UploadOptions: {
736
- bucket: cdn.bucket,
737
- targetPath: cdn.prefix,
738
- cacheControl: cdn.cacheControl,
739
- },
740
- additionalPattern: cdn.additionalPattern,
741
- logger: options.logger,
742
- }));
743
- }
913
+ }
914
+ if (!isSsr) {
915
+ const contextReplacements = getContextReplacements(options);
916
+ contextReplacements.forEach(({ resourceRegExp, newResource }) => rspackPlugins.push(new core_1.rspack.ContextReplacementPlugin(resourceRegExp, newResource)));
917
+ if (isEnvDevelopment && config.reactRefresh !== false) {
918
+ const { webSocketPath = path.normalize(`/${config.publicPathPrefix}/build/sockjs-node`), } = config.devServer || {};
919
+ const { overlay, ...reactRefreshConfig } = config.reactRefresh({
920
+ overlay: { sockPath: webSocketPath },
921
+ exclude: [/node_modules/, /\.worker\.[jt]sx?$/],
922
+ });
923
+ rspackPlugins.push(new plugin_react_refresh_1.default({
924
+ ...reactRefreshConfig,
925
+ overlay: typeof overlay === 'object'
926
+ ? {
927
+ entry: typeof overlay.entry === 'string' ? overlay.entry : undefined,
928
+ module: typeof overlay.module === 'string'
929
+ ? overlay.module
930
+ : undefined,
931
+ sockPath: overlay.sockPath,
932
+ sockHost: overlay.sockHost,
933
+ sockPort: overlay.sockPort?.toString(),
934
+ sockProtocol: overlay.sockProtocol,
935
+ sockIntegration: overlay.sockIntegration === 'wds' ? 'wds' : undefined,
936
+ }
937
+ : undefined,
938
+ }));
939
+ }
940
+ if (config.polyfill?.process) {
941
+ rspackPlugins.push(new core_1.rspack.ProvidePlugin({ process: 'process/browser.js' }));
744
942
  }
745
943
  }
746
- return plugins;
944
+ return rspackPlugins;
747
945
  }
748
- function configureOptimization({ config, isSsr }) {
749
- if (isSsr) {
750
- return {};
751
- }
946
+ function getOptimizationSplitChunks({ config }) {
752
947
  const configVendors = config.vendors ?? [];
753
948
  let vendorsList = [
754
949
  'react',
@@ -770,28 +965,35 @@ function configureOptimization({ config, isSsr }) {
770
965
  vendorsList = vendorsList.concat(configVendors);
771
966
  }
772
967
  const useVendorsList = vendorsList.length > 0;
773
- const optimization = {
774
- splitChunks: {
775
- chunks: 'all',
776
- cacheGroups: {
777
- ...(useVendorsList
778
- ? {
779
- defaultVendors: {
780
- name: 'vendors',
781
- // eslint-disable-next-line security/detect-non-literal-regexp
782
- test: new RegExp(`([\\\\/])node_modules\\1(${vendorsList.join('|')})\\1`),
783
- priority: Infinity,
784
- },
785
- }
786
- : undefined),
787
- css: {
788
- type: 'css/mini-extract',
789
- enforce: true,
790
- minChunks: 2,
791
- reuseExistingChunk: true,
792
- },
968
+ return {
969
+ chunks: 'all',
970
+ cacheGroups: {
971
+ ...(useVendorsList
972
+ ? {
973
+ defaultVendors: {
974
+ name: 'vendors',
975
+ // eslint-disable-next-line security/detect-non-literal-regexp
976
+ test: new RegExp(`([\\\\/])node_modules\\1(${vendorsList.join('|')})\\1`),
977
+ priority: Infinity,
978
+ },
979
+ }
980
+ : undefined),
981
+ css: {
982
+ type: 'css/mini-extract',
983
+ enforce: true,
984
+ minChunks: 2,
985
+ reuseExistingChunk: true,
793
986
  },
794
987
  },
988
+ };
989
+ }
990
+ function configureOptimization(helperOptions) {
991
+ const { config, isSsr } = helperOptions;
992
+ if (isSsr) {
993
+ return {};
994
+ }
995
+ const optimization = {
996
+ splitChunks: getOptimizationSplitChunks(helperOptions),
795
997
  runtimeChunk: 'single',
796
998
  minimizer: [
797
999
  (compiler) => {
@@ -841,3 +1043,33 @@ function configureOptimization({ config, isSsr }) {
841
1043
  };
842
1044
  return optimization;
843
1045
  }
1046
+ function configureRspackOptimization(helperOptions) {
1047
+ const { config, isSsr } = helperOptions;
1048
+ if (isSsr) {
1049
+ return {};
1050
+ }
1051
+ const optimization = {
1052
+ splitChunks: getOptimizationSplitChunks(helperOptions),
1053
+ runtimeChunk: 'single',
1054
+ minimizer: [
1055
+ new core_1.rspack.SwcJsMinimizerRspackPlugin({
1056
+ minimizerOptions: {
1057
+ mangle: !config.reactProfiling,
1058
+ compress: {
1059
+ passes: 2,
1060
+ },
1061
+ format: {
1062
+ safari10: config.safari10,
1063
+ },
1064
+ },
1065
+ }),
1066
+ new core_1.rspack.LightningCssMinimizerRspackPlugin({
1067
+ minimizerOptions: {
1068
+ // Plugin will read the browserslist itself and generate targets
1069
+ targets: [],
1070
+ },
1071
+ }),
1072
+ ],
1073
+ };
1074
+ return optimization;
1075
+ }