-
Notifications
You must be signed in to change notification settings - Fork 9
WIP: Strip debug logs in production builds #138
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
Open
Kombustor
wants to merge
1
commit into
main
Choose a base branch
from
feature/strip-debug-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| EXPO_NO_TELEMETRY=true | ||
| EXPO_PUBLIC_GITHUB_RELEASES_URL=https://github.com/DodoraApp/DodoStream/releases | ||
| NODE_ENV=development | ||
| NODE_ENV=development | ||
|
|
||
| # Strip debug logger calls in production builds (defaults to true if not set) | ||
| # Set to 'false' to keep debug logs in production builds | ||
| STRIP_DEBUG_LOGS=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * Babel plugin to strip debug logger calls in production builds | ||
| * | ||
| * This plugin safely removes: | ||
| * - Imports from '@/utils/debug' | ||
| * - Variables initialized with useDebugLogger() or createDebugLogger() | ||
| * - All calls to those debug logger variables (regardless of variable name) | ||
| * | ||
| * Uses proper scope tracking and binding management to avoid removing unrelated code. | ||
| */ | ||
| module.exports = function ({ types: t }) { | ||
| return { | ||
| name: 'strip-debug-calls', | ||
| visitor: { | ||
| Program: { | ||
| exit(path, state) { | ||
| // Collect debug logger bindings | ||
| const debugLoggerBindings = new Set(); | ||
| const debugImportNames = new Set(); | ||
|
|
||
| // Find all imports from @/utils/debug | ||
| path.traverse({ | ||
| ImportDeclaration(importPath) { | ||
| const source = importPath.node.source.value; | ||
| if (source !== '@/utils/debug') return; | ||
|
|
||
| // Track imported function names | ||
| importPath.node.specifiers.forEach(specifier => { | ||
| if (t.isImportSpecifier(specifier) || t.isImportDefaultSpecifier(specifier)) { | ||
| debugImportNames.add(specifier.local.name); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Find all variable declarations that use the imported debug functions | ||
| path.traverse({ | ||
| VariableDeclarator(declaratorPath) { | ||
| const init = declaratorPath.node.init; | ||
| const id = declaratorPath.node.id; | ||
|
|
||
| if (!init || !t.isIdentifier(id)) return; | ||
|
|
||
| // Check if it's a call to useDebugLogger or createDebugLogger | ||
| if ( | ||
| t.isCallExpression(init) && | ||
| t.isIdentifier(init.callee) && | ||
| debugImportNames.has(init.callee.name) | ||
| ) { | ||
| // Get the binding for this debug logger variable | ||
| const binding = declaratorPath.scope.getBinding(id.name); | ||
| if (binding) { | ||
| debugLoggerBindings.add(binding); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Remove all references to debug loggers (calls to them) | ||
| debugLoggerBindings.forEach(binding => { | ||
| binding.referencePaths.forEach(refPath => { | ||
| // Only remove if it's being called | ||
| const parent = refPath.parent; | ||
| if (t.isCallExpression(parent) && parent.callee === refPath.node) { | ||
| const statement = refPath.getStatementParent(); | ||
| if (statement) { | ||
| statement.remove(); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // Remove the variable declarations for debug loggers | ||
| debugLoggerBindings.forEach(binding => { | ||
| const declarator = binding.path; | ||
| if (declarator && t.isVariableDeclarator(declarator.node)) { | ||
| const declaration = declarator.parentPath; | ||
| if ( | ||
| declaration && | ||
| t.isVariableDeclaration(declaration.node) && | ||
| declaration.node.declarations.length === 1 | ||
| ) { | ||
| declaration.remove(); | ||
| } else { | ||
| declarator.remove(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Remove imports from @/utils/debug | ||
| path.traverse({ | ||
| ImportDeclaration(importPath) { | ||
| const source = importPath.node.source.value; | ||
| if (source === '@/utils/debug') { | ||
| importPath.remove(); | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The Babel config requires both
STRIP_DEBUG_LOGSandNODE_ENV === 'production', butNODE_ENVis not set during local EAS production builds, preventing log stripping.Severity: MEDIUM
Suggested Fix
Rely on a single source of truth for enabling the plugin. Either use
api.caller()as suggested by the code comment to detect the build mode, or only checkprocess.env.STRIP_DEBUG_LOGS, which is correctly configured ineas.jsonfor different build profiles. This will ensure consistent behavior across local and CI/CD builds.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.