babel.config.js 2.0 KB

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