babel allows to wrap any object in transpiled EcmaScript code in manner, which allows later to find original declaration of object.
For every non-primitive value - Ecmascript object - hidden field
__SOURCE_DELCARATION__ will be attached, so in later use original file name & line number can be ealisy retrieved.
Plugin works with ES5-only AST tree, so must be included after any ES6+ and other babel transformation plugins.
Works with latest babel versions, including 6-th and 7-th branch. Tested on
@firstname.lastname@example.org. Note that since 7-th version babel core package moved into dedicated org-namespace.
When develop library or boilerplate starter, like react-scripts or resolve-scripts, there is common problem with reporting error to end user.
If some configuration value is incorrent or omitted, it's very simple to detect it, but impossible to report original source, including file name and line number, where wrong configuration value had been created.
babel-plugin-object-source helps in this use case - just configure development build with including this babel plugin, and every object become have
__SOURCE_DELCARATION__ field, so wrong misconfigurated place can be reported
Every wrapped object has following metainformation in
sourceCode(string) - source code with object declaration
filename(string) - file name in which object has been created
startLine(number) - first line, where object creation had began
startColumn(number) - first column, where object creation had began
endLine(number) - last line, where object creation had end
endColumn(number) - last column, where object creation had end
If babel or webpack have no specific or multi-stage transpile configuraion, all source code, file, line and columns mappings will point to original non-transpiled file.
babel-plugin-object-source ONLY in development mode, i.e. on first cold starts, when end user can make mistakes in configuration file. Plugin causes following effects:
- Plugin generates very long and verbose source code after transpiling. Since every inner object had been wrapped in own helper, growth will be non-linear
- If original code uses Object.getOwnPropertyNames or similar reflaction methods, they can discover
__SOURCE_DELCARATION__field, although it's of cource private and non-enumerable field.
Besides to the proliferation of the source code, the plugin does not cause any side effects. Behaviour of original code will remain unchanged.
Configuration with webpack
const babelPluginObjectSource =ifprocessenvNODE_ENV !== 'production'webpackServerConfigmodulerules;
Examples can be found at resolve-scripts error handling module.