Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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'
@waynebloss

This comment has been minimized.

Copy link

@waynebloss waynebloss commented May 1, 2019

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

@waynebloss

This comment has been minimized.

Copy link

@waynebloss waynebloss commented May 1, 2019

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

@fa7ad

This comment has been minimized.

Copy link

@fa7ad 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

This comment has been minimized.

Copy link

@GraxMonzo GraxMonzo commented Mar 15, 2021

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