npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-object-source

v1.0.4

Published

Babel plugin for verbose any object creation stack, file and line expansion for development purposes

Downloads

14

Readme

babel-plugin-object-source

Summary

Plugin babel-plugin-object-source for 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 babel-core@^6.26.0 and @babel/[email protected]. Note that since 7-th version babel core package moved into dedicated org-namespace.

Use case

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.
Plugin 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

Provided information

Every wrapped object has following metainformation in __SOURCE_DELCARATION__ field:

  • 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.

Usage limits

Use 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:

  1. 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
  2. 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 = require('babel-plugin-object-source')

if(process.env.NODE_ENV !== 'production') {
    webpackServerConfig.module.rules.forEach(rule =>
        rule.loaders.filter(({ loader }) => loader === 'babel-loader').forEach(
            loader =>
                (loader.query.presets = [
                    {
                        plugins: [babelPluginObjectSource]
                    }
                ].concat(Array.isArray(loader.query.presets) ? loader.query.presets : []))
        )
    );
}

Examples

Examples can be found at resolve-scripts error handling module.

import config from 'SOME_USER_CONFIG_FILE';
import { raiseError } from 'error_handling';

if(isNotGood(config.value)) {
   raiseError('Config `value` is not good', config.value)
}