Skip to content

Instantly share code, notes, and snippets.

@aripalo
Last active June 30, 2017 14:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aripalo/6d659fefc79dee72e8e3 to your computer and use it in GitHub Desktop.
Save aripalo/6d659fefc79dee72e8e3 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
@aripalo
Copy link
Author

aripalo commented Feb 25, 2016

Btw, it is useful to add this into npm scripts, if you have npm run dev then add this as "predev": "./watchpack-ignore-node-modules.sh" … or to postinstall or whatever you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment