Skip to content

Instantly share code, notes, and snippets.

@dethi
Last active April 25, 2022 05:34
Show Gist options
  • Save dethi/417f45e8d3be22e35d48087d551b1b6f to your computer and use it in GitHub Desktop.
Save dethi/417f45e8d3be22e35d48087d551b1b6f to your computer and use it in GitHub Desktop.
create-react-app: find files not used in the app bundle, i.e. unused source code
#!/bin/bash
# Launch inside a create-react-app project after building the production build.
# Require `jq`.
diff \
<(find src -type f \( -name '*.js' -o -name '*.jsx' -o -name '*.css' \) | sort) \
<(cat build/**/*.map | jq --raw-output '.sources | join("\n")' \
| grep -v '\.\./' | grep -E '\.(js|jsx|css)$' \
| sed "s#^#src/#" | sort | uniq) \
| grep '< ' | sed "s#^< ##" | grep -v 'spec'
Copy link

ghost commented May 1, 2019

cat: 'build/**/*.map': No such file or directory

Copy link

ghost commented May 1, 2019

Changing the cat command on line 8 to cat build/**/**/*.map worked.

@fa7ad
Copy link

fa7ad commented Oct 30, 2019

since you are already using find. a better version of the cat command would be

find build -iname '*.map' -exec cat {} \; 

so

-	<(cat build/**/*.map | jq --raw-output '.sources | join("\n")' \
+	<(find build -iname '*.map' -exec cat {} \; | jq --raw-output '.sources | join("\n")' \

@GraxMonzo
Copy link

Make sure that the GENERATE_SOURCEMAP env var is true

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