dev.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env node
  2. // 设置环境变量以抑制各种警告
  3. process.env.NODE_ENV = 'development';
  4. process.env.SASS_SILENCE_DEPRECATIONS = '*';
  5. process.env.NODE_OPTIONS = '--no-deprecation --no-warnings --disable-warning=ExperimentalWarning';
  6. process.env.NPM_CONFIG_AUDIT = 'false';
  7. process.env.NPM_CONFIG_FUND = 'false';
  8. process.env.DISABLE_OPENCOLLECTIVE = 'true';
  9. process.env.SUPPRESS_NO_CONFIG_WARNING = 'true';
  10. process.env.CI = 'true';
  11. const { spawn } = require('child_process');
  12. const path = require('path');
  13. // 找到 webpack-cli 的真实路径
  14. const webpackCliPath = require.resolve('webpack-cli/bin/cli.js');
  15. // 设置 Node.js 内存限制和优化参数
  16. const nodeArgs = [
  17. '--max-old-space-size=8192', // 8GB 内存限制
  18. '--gc-interval=100', // 更频繁的垃圾回收
  19. webpackCliPath, // <--- 修改这里:直接使用JS文件的绝对路径
  20. 'serve',
  21. '--config',
  22. path.resolve(__dirname, '../webpack.config.js'),
  23. '--mode',
  24. 'development'
  25. ];
  26. // // 设置 Node.js 内存限制和优化参数
  27. // const nodeArgs = [
  28. // '--max-old-space-size=8192', // 8GB 内存限制
  29. // '--gc-interval=100', // 更频繁的垃圾回收
  30. // path.resolve(__dirname, '../node_modules/.bin/webpack'),
  31. // 'serve',
  32. // '--config',
  33. // path.resolve(__dirname, '../webpack.config.js'),
  34. // '--mode',
  35. // 'development'
  36. // ];
  37. console.log('🚀 启动开发服务器...');
  38. console.log('💾 内存限制: 8GB');
  39. console.log('⚡ 启用内存优化模式');
  40. const child = spawn('node', nodeArgs, {
  41. stdio: ['inherit', 'pipe', 'pipe'],
  42. env: {
  43. ...process.env,
  44. NODE_ENV: 'development',
  45. NODE_OPTIONS: '--max-old-space-size=8192'
  46. }
  47. });
  48. // 处理输出,过滤不必要的信息
  49. child.stdout.on('data', (data) => {
  50. const output = data.toString();
  51. // 过滤掉Babel和其他不必要的提示
  52. const shouldFilter = [
  53. '[BABEL] Note:',
  54. 'code generator has deoptimised',
  55. 'exceeds the max of',
  56. 'Deprecation Warning',
  57. 'WARNING in',
  58. 'export \'',
  59. 'was not found in'
  60. ].some(warning => output.includes(warning));
  61. if (shouldFilter) {
  62. return; // 直接忽略这些输出
  63. }
  64. // 显示重要信息
  65. if (output.includes('webpack compiled') ||
  66. output.includes('Local:') ||
  67. output.includes('Network:') ||
  68. output.includes('webpack://') ||
  69. output.includes('ERROR') ||
  70. output.includes('ready') ||
  71. output.includes('Project is running') ||
  72. output.includes('webpack-dev-server')) {
  73. process.stdout.write(output);
  74. }
  75. });
  76. child.stderr.on('data', (data) => {
  77. const output = data.toString();
  78. // 过滤掉各种类型的警告
  79. const shouldFilter = [
  80. 'Deprecation',
  81. 'experiment',
  82. 'warning',
  83. 'WARNING in',
  84. '@import rules are deprecated',
  85. 'More info and automated migrator',
  86. 'sass-lang.com/d/import',
  87. 'Global built-in functions are deprecated',
  88. 'Use meta.inspect instead',
  89. 'Use string.slice instead',
  90. 'Use string.index instead',
  91. 'Use color.mix instead',
  92. 'export \'', // 模块导出警告
  93. 'Invalid deprecation', // 无效的弃用警告
  94. 'Module Warning (from ./node_modules/sass-loader',
  95. '[BABEL] Note:', // Babel提示
  96. 'code generator has deoptimised', // Babel代码生成提示
  97. 'exceeds the max of', // 文件大小提示
  98. 'was not found in' // 导出未找到警告
  99. ].some(warning => output.includes(warning));
  100. if (!shouldFilter) {
  101. process.stderr.write(output);
  102. }
  103. });
  104. child.on('close', (code) => {
  105. console.log(`开发服务器退出,代码: ${code}`);
  106. });
  107. child.on('error', (error) => {
  108. console.error('❌ 启动失败:', error.message);
  109. console.log('\n🔧 请尝试手动运行: npm run dev:simple');
  110. });
  111. // 处理进程退出
  112. process.on('SIGINT', () => {
  113. console.log('\n正在关闭开发服务器...');
  114. child.kill('SIGINT');
  115. });
  116. process.on('SIGTERM', () => {
  117. child.kill('SIGTERM');
  118. });