webpack.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const { composePlugins, withNx } = require('@nx/webpack');
  2. const { withReact } = require('@nx/react');
  3. const webpack = require('webpack');
  4. const css_prefix = 'lsf-';
  5. // Nx plugins for webpack.
  6. module.exports = composePlugins(
  7. withNx({
  8. // Skip TypeScript type checking during build to avoid UI library type errors
  9. skipTypeChecking: true,
  10. }),
  11. withReact({
  12. // Uncomment this line if you don't want to use SVGR
  13. // See: https://react-svgr.com/
  14. // svgr: false
  15. }),
  16. (config) => {
  17. // 开发环境代理配置
  18. if (config.devServer) {
  19. config.devServer.proxy = [
  20. {
  21. context: ['/api'],
  22. target: 'http://localhost:8003',
  23. secure: false,
  24. changeOrigin: true,
  25. },
  26. ];
  27. }
  28. // Enable WebAssembly support
  29. config.experiments = {
  30. ...config.experiments,
  31. asyncWebAssembly: true,
  32. syncWebAssembly: true,
  33. topLevelAwait: true,
  34. };
  35. // Add fallbacks for Node.js core modules
  36. config.resolve = {
  37. ...config.resolve,
  38. fallback: {
  39. ...config.resolve.fallback,
  40. path: false,
  41. fs: false,
  42. crypto: false,
  43. stream: false,
  44. buffer: false,
  45. util: false,
  46. process: false,
  47. },
  48. // Provide empty module for problematic audio decoder
  49. alias: {
  50. ...config.resolve.alias,
  51. '@humansignal/audio-file-decoder': require.resolve('./src/utils/empty-module.js'),
  52. },
  53. };
  54. // Ignore source map warnings and module not found errors
  55. config.ignoreWarnings = [
  56. /Failed to parse source map/,
  57. /export .* was not found/,
  58. /Can't resolve 'a'/,
  59. /decode-audio\.wasm/,
  60. /audio-file-decoder/,
  61. /Critical dependency: the request of a dependency is an expression/,
  62. ];
  63. // Add plugins
  64. config.plugins = [
  65. ...config.plugins,
  66. // Replace audio decoder with empty module
  67. new webpack.NormalModuleReplacementPlugin(
  68. /@humansignal\/audio-file-decoder/,
  69. require.resolve('./src/utils/empty-module.js')
  70. ),
  71. // Ignore specific modules that cause issues
  72. new webpack.IgnorePlugin({
  73. resourceRegExp: /^\.\/locale$/,
  74. contextRegExp: /moment$/,
  75. }),
  76. // Define CSS_PREFIX for LabelStudio
  77. new webpack.DefinePlugin({
  78. 'process.env.CSS_PREFIX': JSON.stringify(css_prefix),
  79. }),
  80. ];
  81. // Configure CSS modules with lsf- prefix for LabelStudio editor styles
  82. config.module.rules.forEach((rule) => {
  83. const testString = rule.test?.toString() || '';
  84. const isScss = testString.includes('scss');
  85. const isCssModule = testString.includes('.module');
  86. if (rule.test?.toString().match(/scss|sass/) && !isCssModule) {
  87. const r = rule.oneOf?.filter((r) => {
  88. // we don't need rules that don't have loaders
  89. if (!r.use) return false;
  90. const testString = r.test?.toString() || '';
  91. // we also don't need css modules as these are used directly
  92. // in the code and don't need prefixing
  93. if (testString.match(/module|raw|antd/)) return false;
  94. // we only target pre-processors that has 'css-loader included'
  95. return testString.match(/scss|sass/) && r.use.some((u) => u.loader && u.loader.includes('css-loader'));
  96. });
  97. r?.forEach((_r) => {
  98. const cssLoader = _r.use.find((use) => use.loader && use.loader.includes('css-loader'));
  99. if (!cssLoader) return;
  100. const isSASS = _r.use.some((use) => use.loader && use.loader.match(/sass|scss/));
  101. if (isSASS) _r.exclude = /node_modules/;
  102. if (cssLoader.options) {
  103. cssLoader.options.modules = {
  104. localIdentName: `${css_prefix}[local]`, // Add lsf- prefix
  105. getLocalIdent(_ctx, _ident, className) {
  106. if (className.includes('ant')) return className;
  107. },
  108. };
  109. }
  110. });
  111. }
  112. });
  113. // Add rule for XML files and WASM
  114. config.module = {
  115. ...config.module,
  116. rules: [
  117. ...config.module.rules,
  118. {
  119. test: /\.xml$/,
  120. type: 'asset/source',
  121. },
  122. {
  123. test: /\.wasm$/,
  124. type: 'webassembly/async',
  125. },
  126. ],
  127. };
  128. return config;
  129. }
  130. );