umijs / babel-plugin-import

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

Cannot read property 'file' of undefined #123

Open starInEcust opened 7 years ago

starInEcust commented 7 years ago

我发现了一个神奇的bug

ERROR in ./src/js/svgRenderCore/method/handleScore/sourceToRenderData.js
Module build failed: TypeError: /Users/star/code/subvertingMusic/src/js/svgRenderCore/method/handleScore/sourceToRenderData.js: Cannot read property 'file' of undefined
    at Plugin.buildExpressionHandler (/Users/star/code/subvertingMusic/node_modules/babel-plugin-import/lib/Plugin.js:50:26)
    at Plugin.ExpressionStatement (/Users/star/code/subvertingMusic/node_modules/babel-plugin-import/lib/Plugin.js:202:14)
    at applyInstance (/Users/star/code/subvertingMusic/node_modules/babel-plugin-import/lib/index.js:27:26)
    at PluginPass.ret.visitor.(anonymous function) (/Users/star/code/subvertingMusic/node_modules/babel-plugin-import/lib/index.js:86:9)
    at newFn (/Users/star/code/subvertingMusic/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/Users/star/code/subvertingMusic/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/Users/star/code/subvertingMusic/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/Users/star/code/subvertingMusic/node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (/Users/star/code/subvertingMusic/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitMultiple (/Users/star/code/subvertingMusic/node_modules/babel-traverse/lib/context.js:103:17)
    at TraversalContext.visit (/Users/star/code/subvertingMusic/node_modules/babel-traverse/lib/context.js:190:19)
 @ ./src/js/svgRenderCore/index.js 13:26-76
 @ ./src/js/views/ScoreDetail/ScoreStage.js
 @ ./src/js/views/ScoreDetail/index.js
 @ ./src/js/containers/Root.js
 @ ./src/js/containers/App.js
 @ ./src/js/index.js
 @ multi js/index.js webpack-hot-middleware/client

我把代码删减了一下发现在使用import plugin的时候使用如下代码会产生这个错误

export default function () {
  for (let index = 0; index < 1; index++) {
    const getBarNextNote = (nowIndex = index) => {};
    index = 2;
  }
}

写了个demo里面有webpack的配置和babel.rc

bkniffler commented 7 years ago

Also happens when using https://github.com/rofrischmann/fela/tree/master/packages/babel-plugin-fela

DotCoyote commented 6 years ago

You need to set "modules": false" in your .babelrc:

{
  "presets": [
    [
      "@babel/preset-env", {
        "useBuiltIns": "entry",
        "debug": false,
        "modules": false
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-object-rest-spread",
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}
DotCoyote commented 6 years ago

And I needed to install babel-plugin-dynamic-import-node