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

Random crash #229

Closed
malj opened this issue Aug 7, 2021 · 4 comments
Closed

Random crash #229

malj opened this issue Aug 7, 2021 · 4 comments

Comments

@malj
Copy link

malj commented Aug 7, 2021

  • Webpack Version: 5.47.9
  • Operating System (or Browser): Ubuntu 20.04.2 (WSL 2)
  • Node Version: 16.6.0
  • webpack-plugin-serve Version: 1.5.0

How Do We Reproduce?

Run Webpack CLI in watch mode with the following config:

new ServePlugin({
    progress: "minimal",
    host: "localhost",
    hmr: "refresh-on-failure",
})

Expected Behavior

No crashes

Actual Behavior

Webpack process randomly crashes after a while (cca 15 minutes on my machine with Intel i7-8750H and 32 GB RAM) with the following console output:

[webpack-cli] TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at stringify (/path/to/my/project/node_modules/json-stringify-safe/stringify.js:5:15)
    at prep (/path/to/my/project/node_modules/webpack-plugin-serve/lib/routes.js:15:24)
    at WebpackPluginServe.<anonymous> (/path/to/my/project/node_modules/webpack-plugin-serve/lib/routes.js:103:58)
    at WebpackPluginServe.emit (node:events:406:35)
    at WebpackPluginServe.emit (/path/to/my/project/node_modules/webpack-plugin-serve/lib/index.js:158:13)
    at /path/to/my/project/node_modules/webpack-plugin-serve/lib/index.js:210:35
    at Hook.eval [as callAsync] (eval at create (/path/to/my/project/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:42:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/path/to/my/project/node_modules/tapable/lib/Hook.js:18:14)
    at Watching._done (/path/to/my/project/node_modules/webpack/lib/Watching.js:273:28)

Related issues:

@shellscape
Copy link
Owner

This is an issue with json-stringify-safe, not this lib.

@malj
Copy link
Author

malj commented Aug 7, 2021

I know, I submitted a PR, but the lib was last updated in 2015 so I'm not expecting it to be merged anytime soon.

I'm not even sure where big ints are coming from because I'm not using them in my code, but the fix seems fairly trivial. I was hoping you'd consider patching it with a custom replacer, if it wouldn't affect anything else. It's a bit tedious having to kill orphaned child processes and restart webpack several times per hour while working with this plugin.

@shellscape
Copy link
Owner

Yeah the last thing I want to do is handle stringifying myself. Not a fun thing. You might be able to contact the maintainer via Twitter or email.

@cahnory
Copy link

cahnory commented Sep 21, 2021

While this is not solved, here's a very dirty hack (but at least it'll only affect tooling code) to put in your webpack config file:

/**
 * This hack is used to prevent an annoying issue
 * with a dependency of webpack-plugin-serve:
 * - https://github.com/shellscape/webpack-plugin-serve/issues/229
 *
 * It only runs when bundling and is NOT PRESENT in bundled code
 */
const nativeJSONStringify = JSON.stringify
const bigIntReplacer = (key, input) => {
  if (typeof input !== 'bigint') {
    return input
  }

  return `${input}`
}
JSON.stringify = (input, replacer, space) => {
  let output = input

  if (
    Array.isArray(replacer) &&
    typeof output === 'object' &&
    !Array.isArray(output)
  ) {
    output = Object.fromEntries(
      Object.entries(output).filter(([key]) => replacer.includes(key)),
    )
  }

  return nativeJSONStringify(
    output,
    typeof replacer === 'function'
      ? function intermediateReplacer(key, value) {
          return replacer.call(this, key, bigIntReplacer(key, value))
        }
      : bigIntReplacer,
    space,
  )
}

Note that the replacer part has not been heavily tested.

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

3 participants