YahooArchive / strip-loader

Webpack loader to strip arbitrary functions out of your production code.
Other
282 stars 14 forks source link

Error if stripped function is the only line in if condition. #8

Open gowravshekar opened 9 years ago

gowravshekar commented 9 years ago

Using the configuration

{test: /\.js$/, loader: 'strip-loader?strip[]=console.log'}

For the below code snippet in the file, leads to error.

if(condition)
    console.log('...');
else{
   /* Some statements */
}

Error

Unexpected token else You may need an appropriate loader to handle this file type.

eschiebel commented 8 years ago

This is why always using braces around your if clauses is good coding practice.

gowravshekar commented 8 years ago

I caught this error when using some library code. I ended up using UglifyJsPlugin to accomplish the same.

new webpack.optimize.UglifyJsPlugin({
  compress: {
    drop_console: true
  }
})

This works with the above code.

Vijar commented 8 years ago

This sucks but the only real way to do this is to convert code to an AST, strip function calls, then back to code. Which is what I was attempting to do with recast, but I just haven't had time for it.

If anyone else would like to help with that, I'd welcome it.

jvilk commented 8 years ago

I've actually written code to do this as a Grunt task in my DoppioJVM project, but it doesn't work for functions invoked on an object (e.g. works for log(), but not console.log()). It's MIT licensed, so feel free to use it as a starting point.