Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error.prepareStackTrace may not be called with webpack #61

Open
warpdesign opened this issue Nov 4, 2019 · 1 comment
Open

Error.prepareStackTrace may not be called with webpack #61

warpdesign opened this issue Nov 4, 2019 · 1 comment

Comments

@warpdesign
Copy link

While trying to understand why when bindings was used with webpack it returned this kind of error:

TypeError: Cannot read property 'indexOf' of undefined
When calling getFileName

I noticed that webpack, when configured to build for production, appears to be very agressive and simply removes this line from bindings.js:

Error.captureStackTrace(dummy);
dummy.stack // <- this line gets stripped from the build

As a consequence, Error.prepareStackTrace that is defined above never gets called (that's because that the Error.prepareStackTrace is supposed to be called when the stack property is accessed), and this explains why there is a crash since fileName is undefined when this line is called:

  if (fileName.indexOf(fileSchema) === 0) {
    fileName = fileURLToPath(fileName);
  }

I created a small project that shows the problem: https://github.com/warpdesign/bindings-webpack

Simply checkout the repository, call npm install && npm run build and then open the build/main.js file.

Notice how the .stack property access is missing:

Error.captureStackTrace(i),/* stack property access should be there */Error.prepareStackTrace=o
@warpdesign
Copy link
Author

warpdesign commented Nov 4, 2019

After a few hours digging...

I'll provide my own solution :)

For anyone using webpack, it's actually the minimizer that's the cause of the problem: by default, the compressor has the reduce_vars option set to true (see: Terser compress options). With this option, dummy.stack line gets removed.

The workaround is to configure the minimizer to not use that option:

const TerserPlugin = require('terser-webpack-plugin');
// ...
const webPackConfig = {
   // ...
    optimization: {
        minimizer: [
            new TerserPlugin({
                cache: true,
                parallel: true,
                terserOptions: {
                    compress: {
                        reduce_vars: false
                    }
                }
              })
        ]
    }
}

With this code, the dummy.stack line is back, and the crash doesn't happen anymore since prepareStackTrace() gets called as expected. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant