babel.config.js 1.8 KB

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