babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

Using plug-ins to report errors Cannot read property 'getCode' of undefined #946

Open bosens-China opened 5 years ago

bosens-China commented 5 years ago

Describe the bug

To Reproduce

Minimal code to reproduce the bug

const {
  production,
} = require('./config/index');

const date = {
  presets: [
    [
      '@babel/preset-env',
      {
        useBuiltIns: 'usage',
      },
    ],
    '@babel/preset-typescript',
  ],
  plugins: ['@babel/proposal-class-properties', '@babel/proposal-object-rest-spread'],
};
if (production) {
  date.presets.push('minify');
  date.plugins.push('transform-remove-console', 'transform-remove-debugger',
    'minify-dead-code-elimination', 'minify-guarded-expressions', 'minify-mangle-names',
    'minify-replace', 'minify-simplify', 'transform-undefined-to-void', 'minify-type-constructors',
    'transform-merge-sibling-variables', 'transform-minify-booleans');
}

module.exports = date;

Actual Output

If there is no Error thrown,

Module build failed (from ./node_modules/happypack/loader.js):
TypeError: Cannot read property 'getCode' of undefined
    at NodePath.getSource (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\path\introspection.js:164:27)
    at PluginPass.Program (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\babel-plugin-minify-simplify\lib\index.js:490:41)
    at newFn (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\visitors.js:193:21)
    at NodePath._call (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\path\context.js:53:20)
    at NodePath.call (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\path\context.js:40:17)
    at NodePath.visit (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\path\context.js:88:12)
    at TraversalContext.visitQueue (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\context.js:118:16)
    at TraversalContext.visitSingle (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\context.js:90:19)
    at TraversalContext.visit (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\context.js:146:19)
    at Function.traverse.node (C:\Users\Administrator\Desktop\WebPackMultiPageConfiguration\node_modules\@babel\traverse\lib\index.js:94:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! configure@1.0.0 build: `cross-env NODE_ENV=production webpack --config build/webpack.prod.js`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the configure@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Expected Output

Successful output

Stack Trace

If applicable,

Configuration

How are you using babel-minify?

webpack4

babel-minify version: 0.4.3

babel version : 7.0.0-beta.49

babel-minify-config:

{
  removeConsole: true,
  keepFnNames: true
}

babelrc:

{
  plugins: [],
  presets: []
}

Possible solution

Additional context

budarin commented 5 years ago

I also have this error if babel-plugin-closure-elimination is in plugins array

shrey150 commented 5 years ago

Can confirm this error occurs when babel-plugin-minify-mangle-names is in .babelrc plugins

weyert commented 4 years ago

Yes, I have this problem to with Storybook when I am using Typescript

crra commented 4 years ago

Hello @shrey150,

Can confirm this error occurs when babel-plugin-minify-mangle-names is in .babelrc plugins

Do you use the preset minify by any chance? If so you need to remove the plugin from the plugin section and set the options via the preset. e.g:

"env": {
  "production": {
    "presets": [
      ["minify", {
        "mangle": {
           "topLevel": true,
           "keepFnName": false,
           "keepClassName": false
         },