babel.config.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const webpack = require('webpack')
  2. const plugins = []
  3. if (process.env.UNI_OPT_TREESHAKINGNG) {
  4. plugins.push(require('@dcloudio/vue-cli-plugin-uni-optimize/packages/babel-plugin-uni-api/index.js'))
  5. }
  6. if (
  7. (process.env.UNI_PLATFORM === 'app-plus' && process.env.UNI_USING_V8) ||
  8. (process.env.UNI_PLATFORM === 'h5' && process.env.UNI_H5_BROWSER === 'builtin')
  9. ) {
  10. const path = require('path')
  11. const isWin = /^win/.test(process.platform)
  12. const normalizePath = (path) => (isWin ? path.replace(/\\/g, '/') : path)
  13. const input = normalizePath(process.env.UNI_INPUT_DIR)
  14. try {
  15. plugins.push([
  16. require('@dcloudio/vue-cli-plugin-hbuilderx/packages/babel-plugin-console'),
  17. {
  18. file(file) {
  19. file = normalizePath(file)
  20. if (file.indexOf(input) === 0) {
  21. return path.relative(input, file)
  22. }
  23. return false
  24. }
  25. }
  26. ])
  27. } catch (e) {
  28. console.error(e)
  29. }
  30. }
  31. process.UNI_LIBRARIES = process.UNI_LIBRARIES || ['@dcloudio/uni-ui']
  32. process.UNI_LIBRARIES.forEach((libraryName) => {
  33. plugins.push([
  34. 'import',
  35. {
  36. libraryName: libraryName,
  37. customName: (name) => {
  38. return `${libraryName}/lib/${name}/${name}`
  39. }
  40. }
  41. ])
  42. })
  43. if (process.env.UNI_PLATFORM !== 'h5') {
  44. plugins.push('@babel/plugin-transform-runtime')
  45. }
  46. const config = {
  47. presets: [
  48. [
  49. '@vue/app',
  50. {
  51. modules: webpack.version[0] > 4 ? 'auto' : 'commonjs',
  52. useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'
  53. }
  54. ]
  55. ],
  56. plugins
  57. }
  58. const UNI_H5_TEST = '**/@dcloudio/uni-h5/dist/index.umd.min.js'
  59. if (process.env.NODE_ENV === 'production') {
  60. config.overrides = [
  61. {
  62. test: UNI_H5_TEST,
  63. compact: true
  64. }
  65. ]
  66. } else {
  67. config.ignore = [UNI_H5_TEST]
  68. }
  69. module.exports = config