taro 1.3.4配置 config的index配置文件

偏执的太偏执、 2023-01-17 09:18 328阅读 0赞

复制代码 dev.js
// NOTE H5 端使用 devServer 实现跨域,需要修改 package.json 的运行命令,加入环境变量
const isH5 = process.env.CLIENT_ENV === ‘h5’
const HOST = ‘“https://min“‘
const HOST_M = ‘“https://“‘

  1. module.exports = \{
  2. env: \{
  3. NODE\_ENV: '"development"'
  4. \},
  5. defineConstants: \{
  6. HOST: isH5 ? '"/api"' : HOST,
  7. HOST\_M: isH5 ? '"/api-m"' : HOST\_M
  8. \},
  9. weapp: \{\},
  10. h5: \{
  11. devServer: \{
  12. proxy: \{
  13. '/api/': \{
  14. target: JSON.parse(HOST),
  15. pathRewrite: \{
  16. '^/api/': '/'
  17. \},
  18. changeOrigin: true
  19. \},
  20. '/api-m/': \{
  21. target: JSON.parse(HOST\_M),
  22. pathRewrite: \{
  23. '^/api-m/': '/'
  24. \},
  25. changeOrigin: true
  26. \}
  27. \}
  28. \}
  29. \}
  30. \}



prod.js
const isH5 = process.env.CLIENT_ENV === ‘h5’

  1. const HOST = '"https://mi"'
  2. const HOST\_M = '"https://m"'
  3. // XXX 搭了个 proxy 用于演示 prod 环境的 H5
  4. const HOST\_H5 = '"http://"'
  5. const HOST\_M\_H5 = '"http://"'
  6. module.exports = \{
  7. env: \{
  8. NODE\_ENV: '"production"'
  9. \},
  10. defineConstants: \{
  11. HOST: isH5 ? HOST\_H5 : HOST,
  12. HOST\_M: isH5 ? HOST\_M\_H5 : HOST\_M
  13. \},
  14. weapp: \{\},
  15. h5: \{
  16. publicPath: '/taro-yanxuan'
  17. \}
  18. \}
  19. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
  20. index.js:
  21. const path = require('path')
  22. // NOTE 在 sass 中通过别名(@ 或 ~)引用需要指定路径
  23. const sassImporter = function(url) \{
  24. if (url\[0\] === '~' && url\[1\] !== '/') \{
  25. return \{
  26. file: path.resolve(\_\_dirname, '..', 'node\_modules', url.substr(1))
  27. \}
  28. \}
  29. const reg = /^@styles\\/(.\*)/
  30. return \{
  31. file: reg.test(url) ? path.resolve(\_\_dirname, '..', 'src/styles', url.match(reg)\[1\]) : url
  32. \}
  33. \}
  34. const config = \{
  35. projectName: 'taro-demo',
  36. date: '2019-12-1',
  37. designWidth: 750,
  38. deviceRatio: \{
  39. '640': 2.34 / 2,
  40. '750': 1,
  41. '828': 1.81 / 2
  42. \},
  43. sourceRoot: 'src',
  44. outputRoot: 'dist',
  45. plugins: \{
  46. babel: \{
  47. sourceMap: true,
  48. presets: \[
  49. \['env', \{
  50. modules: false
  51. \}\]
  52. \],
  53. plugins: \[
  54. 'transform-decorators-legacy',
  55. 'transform-class-properties',
  56. 'transform-object-rest-spread'
  57. \]
  58. \},
  59. sass: \{
  60. importer: sassImporter
  61. \}
  62. \},
  63. defineConstants: \{
  64. \},
  65. alias: \{
  66. '@actions': path.resolve(\_\_dirname, '..', 'src/actions'),
  67. '@assets': path.resolve(\_\_dirname, '..', 'src/assets'),
  68. '@components': path.resolve(\_\_dirname, '..', 'src/components'),
  69. '@constants': path.resolve(\_\_dirname, '..', 'src/constants'),
  70. '@reducers': path.resolve(\_\_dirname, '..', 'src/reducers'),
  71. '@styles': path.resolve(\_\_dirname, '..', 'src/styles'),
  72. '@utils': path.resolve(\_\_dirname, '..', 'src/utils')
  73. \},
  74. copy: \{
  75. patterns: \[
  76. \],
  77. options: \{
  78. \}
  79. \},
  80. weapp: \{
  81. module: \{
  82. postcss: \{
  83. autoprefixer: \{
  84. enable: true,
  85. config: \{
  86. browsers: \[
  87. 'last 3 versions',
  88. 'Android >= 4.1',
  89. 'ios >= 8'
  90. \]
  91. \}
  92. \},
  93. pxtransform: \{
  94. enable: true,
  95. config: \{
  96. \}
  97. \},
  98. url: \{
  99. enable: true,
  100. config: \{
  101. limit: 10240 // 设定转换尺寸上限
  102. \}
  103. \},
  104. cssModules: \{
  105. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  106. config: \{
  107. namingPattern: 'module', // 转换模式,取值为 global/module
  108. generateScopedName: '\[name\]\_\_\[local\]\_\_\_\[hash:base64:5\]'
  109. \}
  110. \}
  111. \}
  112. \}
  113. \},
  114. h5: \{
  115. // NOTE H5 打包静态资源时带 hash 值,方便缓存、版本管理
  116. publicPath: '/',
  117. staticDirectory: 'static',
  118. output: \{
  119. filename: 'js/\[name\].\[hash\].js',
  120. chunkFilename: 'js/\[name\].\[chunkhash\].js'
  121. \},
  122. imageUrlLoaderOption: \{
  123. limit: 5000,
  124. name: 'static/images/\[name\].\[hash\].\[ext\]'
  125. \},
  126. miniCssExtractPluginOption: \{
  127. filename: 'css/\[name\].\[hash\].css',
  128. chunkFilename: 'css/\[name\].\[chunkhash\].css',
  129. \},
  130. module: \{
  131. postcss: \{
  132. autoprefixer: \{
  133. enable: true,
  134. config: \{
  135. browsers: \[
  136. 'last 3 versions',
  137. 'Android >= 4.1',
  138. 'ios >= 8'
  139. \]
  140. \}
  141. \},
  142. cssModules: \{
  143. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  144. config: \{
  145. namingPattern: 'module', // 转换模式,取值为 global/module
  146. generateScopedName: '\[name\]\_\_\[local\]\_\_\_\[hash:base64:5\]'
  147. \}
  148. \}
  149. \}
  150. \},
  151. sassLoaderOption: \{
  152. importer: sassImporter
  153. \}
  154. \},
  155. rn: \{
  156. appJson: \{
  157. // NOTE taro-native-shell 中默认用的是 taroDemo
  158. name: 'taroDemo'
  159. \}
  160. \}
  161. \}
  162. module.exports = function (merge) \{
  163. if (process.env.NODE\_ENV === 'development') \{
  164. return merge(\{\}, config, require('./dev'))
  165. \}
  166. return merge(\{\}, config, require('./prod'))
  167. \}

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ssh配置config文件

    在使用ssh连接服务器时,经常要输入一些不同的主机地址和密码,使用config文件可以很好的解决这个问题。 在配置之前我们先生成ssh密钥。 使用以下命令 一路回

    相关 taro 页面配置

    1、配置全局页面样式 在app.config.js中配置,配置规则和小程序相似 2、页面单独配置 在页面的xx.jsx文件创建相