prod.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import type { UserConfigExport } from '@tarojs/cli';
  2. export default {
  3. mini: {},
  4. h5: {
  5. /**
  6. * WebpackChain 插件配置
  7. * @docs https://github.com/neutrinojs/webpack-chain
  8. */
  9. webpackChain(chain) {
  10. // /**
  11. // * 如果 h5 端编译后体积过大,可以使用 webpack-bundle-analyzer 插件对打包体积进行分析。
  12. // * @docs https://github.com/webpack-contrib/webpack-bundle-analyzer
  13. // */
  14. // chain.plugin('analyzer')
  15. // .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [])
  16. // /**
  17. // * 如果 h5 端首屏加载时间过长,可以使用 prerender-spa-plugin 插件预加载首页。
  18. // * @docs https://github.com/chrisvfritz/prerender-spa-plugin
  19. // */
  20. // const path = require('path')
  21. // const Prerender = require('prerender-spa-plugin')
  22. // const staticDir = path.join(__dirname, '..', 'dist')
  23. // chain
  24. // .plugin('prerender')
  25. // .use(new Prerender({
  26. // staticDir,
  27. // routes: [ '/pages/index/index' ],
  28. // postProcess: (context) => ({ ...context, outputPath: path.join(staticDir, 'index.html') })
  29. // }))
  30. chain.module
  31. .rule('replace-a2b')
  32. .test(/\.[jt]sx?$/) // 作用于 .js/.ts/.jsx/.tsx
  33. .use('string-replace-loader')
  34. .loader('string-replace-loader')
  35. .options({
  36. search: 'localhost:10086', // 要替换的字符串/正则
  37. replace: '192.168.11.12:6001', // 替换成的字符串
  38. flags: 'g', // 全局替换
  39. })
  40. .before('babel-loader'); // 必须在 babel-loader 之前执行,防止被转码后匹配不到
  41. },
  42. },
  43. } satisfies UserConfigExport<'webpack5'>;