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(
  5. require('@dcloudio/vue-cli-plugin-uni-optimize/packages/babel-plugin-uni-api/index.js'),
  6. );
  7. }
  8. if (
  9. (process.env.UNI_PLATFORM === 'app-plus' && process.env.UNI_USING_V8) ||
  10. (process.env.UNI_PLATFORM === 'h5' && process.env.UNI_H5_BROWSER === 'builtin')
  11. ) {
  12. const path = require('path');
  13. const isWin = /^win/.test(process.platform);
  14. const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path);
  15. const input = normalizePath(process.env.UNI_INPUT_DIR);
  16. try {
  17. plugins.push([
  18. require('@dcloudio/vue-cli-plugin-hbuilderx/packages/babel-plugin-console'),
  19. {
  20. file(file) {
  21. file = normalizePath(file);
  22. if (file.indexOf(input) === 0) {
  23. return path.relative(input, file);
  24. }
  25. return false;
  26. },
  27. },
  28. ]);
  29. } catch (e) {}
  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;