babel-plugin-show-source

raw JSON →
0.2.1 verified Sat Apr 25 auth: no javascript

A Babel plugin that transforms functions to include their source code via Function.prototype.toString() for runtime introspection. Current stable version is 0.2.1 (pre-1.0.0, still experimental). The release cadence is low; last update was 2023. It is a niche tool intended for debugging, serialization, or educational demos where preserving function text is needed. Note the pre-release versioning and possible breaking changes in minor versions.

error Error: Cannot find module 'babel-plugin-show-source'
cause Missing dependency; not installed in node_modules.
fix
Run npm install --save-dev babel-plugin-show-source or yarn add -D babel-plugin-show-source.
error TypeError: plugin is not a function
cause Incorrect import; attempting to use default import as a function.
fix
Use import showSource from 'babel-plugin-show-source' in ESM or ensure proper default import resolution in CommonJS.
gotcha Plugin may break if Babel options like 'compact' or 'retainLines' are used.
fix Avoid using conflicting options; test with your Babel configuration.
breaking Version 0.2.0 changed the output format; source string may differ in whitespace from original.
fix Check updated output, adjust any string comparisons if needed.
deprecated The 'show-source' option in Babel config is deprecated in favor of direct plugin usage.
fix Use 'plugins' array directly instead of 'env' or 'overrides' options.
npm install babel-plugin-show-source
yarn add babel-plugin-show-source
pnpm add babel-plugin-show-source

Shows how to add the plugin to Babel config and demonstrates the transformation: after building, greet.toString() will return the original source code instead of '[Function]'.

// babel.config.js
module.exports = {
  plugins: [
    'babel-plugin-show-source',
    // or with options:
    // ['babel-plugin-show-source', { /* options */ }]
  ]
};

// Input file (source.js)
function greet(name) {
  return `Hello, ${name}!`;
}
console.log(greet.toString());

// After transformation, greet.toString() returns the full source code
// Output: 'function greet(name) { return `Hello, ${name}!`; }'