umijs / babel-plugin-import

Modularly import plugin for babel.
3.16k stars 405 forks source link

使用babel7后,按需加载执行报错 #424

Open ForeverPx opened 4 years ago

ForeverPx commented 4 years ago

错误信息

image

对应代码

import { DatePicker } from 'antd';
const { RangePicker } = DatePicker;

.babelrc

{
    "presets": [
        "@babel/preset-react",
        [
            "@babel/preset-env"
        ],
        "@babel/preset-typescript"
    ],
    "ignore": [
        "./src/common/UEditor/ueditor.all.js"
    ],
    "plugins": [
        "react-hot-loader/babel",
        [
            "react-css-modules",
            {
                "filetypes": {
                    ".less": {
                        "syntax": "postcss-less"
                    }
                },
                "webpackHotModuleReloading": true,
                "generateScopedName": "[name]__[local]__[hash:base64:5]"
            }
        ],
        ["@babel/plugin-transform-runtime", {
            "corejs": false, // 下面详解
            "helpers": false, // 助手函数是否提取,同babel-plugin-transform
            "regenerator": true, // 同babel-plugin-transform
            "useESModules": false
        }],
        [
            "import",
            {
                "libraryName": "antd",
                "libraryDirectory": "es",
                "style": "css"
            }
        ],
        [
            "module-resolver",
            {
                "root": [
                    "./src"
                ],
                "alias": {
                    "@common": "./src/common",
                    "@main": "./src/app/main"
                }
            }
        ],
        "@babel/plugin-syntax-dynamic-import",
        "@babel/plugin-syntax-import-meta",
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-proposal-json-strings",
        [
            "@babel/plugin-proposal-decorators",
            {
                "legacy": true
            }
        ],
        "@babel/plugin-proposal-function-sent",
        "@babel/plugin-proposal-export-namespace-from",
        "@babel/plugin-proposal-numeric-separator",
        "@babel/plugin-proposal-throw-expressions",
        "@babel/plugin-proposal-export-default-from",
        "@babel/plugin-proposal-logical-assignment-operators",
        "@babel/plugin-proposal-optional-chaining",
        [
            "@babel/plugin-proposal-pipeline-operator",
            {
                "proposal": "minimal"
            }
        ],
        "@babel/plugin-proposal-nullish-coalescing-operator",
        "@babel/plugin-proposal-do-expressions",
        "@babel/plugin-proposal-function-bind",
        "@babel/plugin-transform-destructuring",
        "@babel/plugin-transform-modules-commonjs"
    ],
    "env": {
        "dev": {
            "plugins": [
                "istanbul"
            ]
        }
    }
}
youngBrain1893 commented 4 years ago

istanbuljs/babel-plugin-istanbul#161

同样的问题 +1

当使用 babel-plugin-istanbul + 导出内容解构 的时候 import of DatePicker 被丢掉了

本应该得到

// 不开启 istanbul 插件
import "antd/es/date-picker/style/css";
import _DatePicker from "antd/es/date-picker";
const {
  RangePicker
} = _DatePicker;

实际实际编译后只剩下了,由于没有 DatePicker 引入 RangePicker 结构失败

const {
  RangePicker
} = (cov_1rg8pvnktm().s[0]++, DatePicker);

最小可复现内容 .babelrc

{
  "presets": [],
  "plugins": [
    [
      "import",
      {
          "libraryName": "antd",
          "libraryDirectory": "es",
          "style": "css"
      }
    ],
    "babel-plugin-istanbul"
  ]
}

index.js

import { DatePicker } from 'antd';
const { RangePicker } = DatePicker;
// const RangePicker = DatePicker.RangePicker // 不使用解构语法的时候也没问题

console.log(RangePicker);
chenabc1 commented 4 years ago

请问这问题解决了吗现在?