Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sasha7/8f75b859affd19b7c954d6d156eadcf7 to your computer and use it in GitHub Desktop.
Save sasha7/8f75b859affd19b7c954d6d156eadcf7 to your computer and use it in GitHub Desktop.
Using webpack and its polling watch feature (via watchpack) can drain CPU which is due to watchpack polling all the npm deps within node_modules folder. This is a quick hack/fix until proper fix is merged & available in watchpack.
#!/bin/bash
# Should be used until https://github.com/webpack/watchpack/pull/23 is merged and available in npm
# See https://github.com/webpack/watchpack/issues/2#issuecomment-135204573 for more info
# Ensure we have npm
if ! hash npm 2>/dev/null; then
echo 'No NPM installed!'
exit 1
fi
# npm@3 uses flat tree structure so account for that
if npm -v 2>/dev/null | grep -q "^2."; then
FILEPATH="node_modules/webpack/node_modules/watchpack/lib/DirectoryWatcher.js"
else
FILEPATH="node_modules/watchpack/lib/DirectoryWatcher.js"
fi
INSERTION_POINT_BEFORE="followSymlinks: false,"
MISSING_OPTION="ignored: /node_modules/,"
if ! cat ${FILEPATH} 2>/dev/null | grep -q "${MISSING_OPTION}"; then
echo 'Fixing webpack watch (polling) slowness with a manual hack. See https://github.com/webpack/watchpack/pull/23 for more info.'
sed -i "s|${INSERTION_POINT_BEFORE}|&\n${MISSING_OPTION}|" "${FILEPATH}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment