babel-deps

raw JSON →
2.1.0 verified Sat Apr 25 auth: no javascript abandoned

Compiles JavaScript files along with all their imported dependencies using Babel, automatically resolving and compiling transitive dependencies that are not explicitly listed. Version 2.1.0 (latest) with no recent updates. Key differentiators: unlike direct Babel usage, babel-deps discovers and compiles the entire dependency graph, caches results for performance, and allows custom module resolution. Supports Node >=0.12.0 (legacy).

error TypeError: Cannot read property 'filename' of undefined
cause Missing `options` or `filename` in a file entry.
fix
Ensure each file object has contents and options: { filename: 'path/to/file.js' }.
error Error: Module 'some-package' not found
cause Module source is not a valid file path and no `resolveModuleToPath` provided.
fix
Provide a resolveModuleToPath function that converts module names to file paths.
deprecated Package is no longer maintained. Last release 2.1.0 on 2016-06-13. Babel versions have changed drastically since.
fix Consider using a modern tool like Webpack, Babel CLI alone, or Rollup for dependency compilation.
gotcha Each file object must have a `filename` in `options`. If missing, compilation will fail silently or throw.
fix Always provide `options.filename` for every file in the array.
gotcha Default module resolution assumes module sources are already absolute or relative paths. For non-path sources, provide `resolveModuleToPath`.
fix Implement `resolveModuleToPath` function for packages that are not paths.
npm install babel-deps
yarn add babel-deps
pnpm add babel-deps

Demonstrates compiling a file and its dependencies with Babel, using cache and custom Babel options.

import babelDeps from 'babel-deps';
import fs from 'fs';

const files = [
  {
    contents: fs.readFileSync('src/index.js', 'utf8'),
    options: { filename: 'src/index.js' }
  }
];

const results = babelDeps(files, {
  babel: { presets: ['@babel/preset-env'] },
  cache: true
});

for (const result of results) {
  console.log(result.filename, result.code);
}