-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strip less-important logging from modular variant
In order to reduce bundle size further, we’ve decided to strip all logging from the modular variant of the SDK, except for errors and certain network events. (We also considered providing a separate tree-shakable module with all of this logging code so that a user of the modular variant of the library can opt in to it, but decided against it for now; we might add it in later. This does mean that there is currently no version of the SDK that allows you to use both deltas and verbose logging on web.) I couldn’t find any out-of-the-box esbuild functionality that let us do this. The only stuff I could find related to stripping code was: - the `pure` option, but that code only gets stripped if you minify the code (and even in that case I couldn’t actually get it to be stripped, perhaps would have been able to with further trying though), but minifying our generated modules bundle causes the bundle size of those who use it (as tested by our modulereport script) to increase considerably (for reasons I’m not sure of) - the `drop` option, but that only lets you remove calls to `console` or `debugger` So instead I’ve implemented it as an esbuild plugin. Resolves #1526
- Loading branch information
1 parent
8cf7433
commit 0f1fe78
Showing
14 changed files
with
239 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -58,6 +58,7 @@ module.exports = { | |
"typedoc/generated", | ||
"react", | ||
"Gruntfile.js", | ||
"grunt", | ||
], | ||
settings: { | ||
jsdoc: { | ||
|
This file contains 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 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 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,111 @@ | ||
var path = require('path'); | ||
var fs = require('fs/promises'); | ||
var babel = { | ||
types: require('@babel/types'), | ||
parser: require('@babel/parser'), | ||
traverse: require('@babel/traverse'), | ||
generator: require('@babel/generator'), | ||
}; | ||
|
||
// This function is copied from | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping | ||
function escapeRegExp(string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string | ||
} | ||
|
||
// This esbuild plugin strips all log messages from the modular variant of | ||
// the library, except for error-level logs and other logging statements | ||
// explicitly marked as not to be stripped. | ||
const stripLogsPlugin = { | ||
name: 'stripLogs', | ||
setup(build) { | ||
let foundLogToStrip = false; | ||
let foundErrorLog = false; | ||
let foundNoStripLog = false; | ||
|
||
const filter = new RegExp(`^${escapeRegExp(path.join(__dirname, '..', '..', 'src') + path.sep)}.*\\.[tj]s$`); | ||
build.onLoad({ filter }, async (args) => { | ||
const contents = (await fs.readFile(args.path)).toString(); | ||
const lines = contents.split('\n'); | ||
const ast = babel.parser.parse(contents, { sourceType: 'module', plugins: ['typescript'] }); | ||
const errors = []; | ||
|
||
babel.traverse.default(ast, { | ||
enter(path) { | ||
if ( | ||
path.isCallExpression() && | ||
babel.types.isMemberExpression(path.node.callee) && | ||
babel.types.isIdentifier(path.node.callee.object, { name: 'Logger' }) | ||
) { | ||
if (babel.types.isIdentifier(path.node.callee.property, { name: 'logAction' })) { | ||
const firstArgument = path.node.arguments[0]; | ||
|
||
if ( | ||
babel.types.isMemberExpression(firstArgument) && | ||
babel.types.isIdentifier(firstArgument.object, { name: 'Logger' }) && | ||
firstArgument.property.name.startsWith('LOG_') | ||
) { | ||
if (firstArgument.property.name === 'LOG_ERROR') { | ||
// `path` is a call to `Logger.logAction(Logger.LOG_ERROR, ...)`; preserve it. | ||
foundErrorLog = true; | ||
} else { | ||
// `path` is a call to `Logger.logAction(Logger.LOG_*, ...) for some other log level; strip it. | ||
foundLogToStrip = true; | ||
path.remove(); | ||
} | ||
} else { | ||
// `path` is a call to `Logger.logAction(...)` with some argument other than a `Logger.LOG_*` expression; raise an error because we can’t determine whether to strip it. | ||
errors.push({ | ||
location: { | ||
file: args.path, | ||
column: firstArgument.loc.start.column, | ||
line: firstArgument.loc.start.line, | ||
lineText: lines[firstArgument.loc.start.line - 1], | ||
}, | ||
text: `First argument passed to Logger.logAction() must be Logger.LOG_*, got \`${ | ||
babel.generator.default(firstArgument).code | ||
}\``, | ||
}); | ||
} | ||
} else if (babel.types.isIdentifier(path.node.callee.property, { name: 'logActionNoStrip' })) { | ||
// `path` is a call to `Logger.logActionNoStrip(...)`; preserve it. | ||
foundNoStripLog = true; | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
return { contents: babel.generator.default(ast).code, loader: 'ts', errors }; | ||
}); | ||
|
||
build.onEnd(() => { | ||
const errorMessages = []; | ||
|
||
// Perform a sense check to make sure that we found some logging | ||
// calls to strip (to protect us against accidentally changing the | ||
// internal logging API in such a way that would cause us to no | ||
// longer strip any calls). | ||
|
||
if (!foundLogToStrip) { | ||
errorMessages.push('Did not find any Logger.logAction(...) calls to strip'); | ||
} | ||
|
||
// Perform a sense check to make sure that we found some logging | ||
// calls to preserve (to protect us against accidentally changing the | ||
// internal logging API in such a way that would cause us to | ||
// accidentally strip all logging calls). | ||
|
||
if (!foundErrorLog) { | ||
errorMessages.push('Did not find any Logger.logAction(Logger.LOG_ERROR, ...) calls to preserve'); | ||
} | ||
|
||
if (!foundNoStripLog) { | ||
errorMessages.push('Did not find any Logger.logActionNoStrip(...) calls to preserve'); | ||
} | ||
|
||
return { errors: errorMessages.map((text) => ({ text })) }; | ||
}); | ||
}, | ||
}; | ||
|
||
exports.default = stripLogsPlugin; |
This file contains 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
Oops, something went wrong.