Bit-Bundler Source Map Extraction Plugin

2.1.4 · maintenance · verified Tue Apr 21

bit-bundler-extractsm is a specialized plugin for the `bit-bundler` ecosystem, designed to manage source maps during the bundling process. Its primary function is to extract inline or generated source maps from bundled JavaScript files and write them to separate, dedicated `.map` files, typically alongside their corresponding JavaScript output. As of version `2.1.4`, it offers stable functionality for `bit-bundler` users. While its release cadence is not strictly defined, it appears to be actively maintained for bug fixes and compatibility, with recent updates addressing specific edge cases. A key differentiator is its deep integration with `bit-bundler`'s plugin architecture, allowing granular control over source map extraction, including the ability to entirely remove source maps from bundles or to manage them differently across various bundle splits. This makes it essential for optimizing production builds by separating source map files, preventing them from being included in the main application bundle.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `bit-bundler-extractsm` with `bit-bundler` and `bit-bundler-minifyjs` to bundle `in.js` into `out.js` and extract its source map into a separate `out.js.map` file.

const Bitbundler = require("bit-bundler");
const path = require("path");
const fs = require("fs");

// Create a dummy in.js file for the example
const inJsContent = `
// in.js
console.log('Hello from in.js');
const add = (a, b) => a + b;
console.log('Sum:', add(1, 2));
// This line is often generated by tools like Babel or TypeScript, or explicitly added by minifiers.
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluLmpzIl0sIm5hbWVzIjpbImNvbnNvbGUiLCJsb2ciLCJhZGQiLCJhIiwiYiJdLCJtYXBwaW5ncyI6IkFBQUE7QUFDQUEsUUFBQUMsR0FBRCxDQUFLLGtCQUFMLENBQUw7QUFDQSxNQUFNQyxHQUFHLENBQUNDLEVBQUQsRUFBU0MsQ0FBVCxDQUFELEdBQWdCRCxDQUFDLEdBQUVDLENBQXJCO0FBQ0FILFFBQUFDLGdBQWdCLENBQUNFLFdBQVdDLENBQUMsQ0FBQyxDQUFFLENBQUMsQ0FBRCxDQUFZLENBQVAsQ0FBckI7QUFDQTtBQUxBIiwic291cmNlc0NvbnRlbnQiOlsiLy8gaW4uanNcbmNvbnNvbGUubG9nKCdIZWxsbyBmcm9tIGluLmpzJyk7XG5jb25zdCBhZGQgPSAocmEsIGIpID0+IGEgKyBiO1xuY29uc29sZS5sb2coJ1N1bTogJywgYWRkKDEsIDIpKTtcbi8vIHRoaXMgaW5saW5lIHNvdXJjZW1hcCBpcyBmb3IgZGVtb25zdHJhdGlvbiBwdXJwb3Nlcywgbm90gCBhIGNhbm9uaWNhbCBzb3VyY2VtYXAgZm9ybWF0XG4jIyMgc291cmNlTWFwZmluZ1VSTA1pbi5qcy5tYXAgYnkgYmFzZTY0IGVuY29kaW5nIGEgZHVubXkgc291cmNlbWFwXG4ifV0sImZpbGUiOiJpbi5qcyJ9
`;
const inJsPath = path.join(__dirname, 'in.js');
fs.writeFileSync(inJsPath, inJsContent.trim());

const outJsPath = path.join(__dirname, 'out.js');
const outMapPath = path.join(__dirname, 'out.js.map');

// Clean up previous outputs if they exist
if (fs.existsSync(outJsPath)) fs.unlinkSync(outJsPath);
if (fs.existsSync(outMapPath)) fs.unlinkSync(outMapPath);

console.log('Starting bundling process...');
Bitbundler.bundle({
  src: inJsPath,
  dest: outJsPath
}, {
  bundler: [
    // bit-bundler-minifyjs is often used to generate the source map that extractsm will then process
    "bit-bundler-minifyjs",
    "bit-bundler-extractsm"
  ]
}).then(() => {
  console.log(`Bundle created at ${outJsPath}`);
  console.log(`Source map extracted to ${outMapPath}`);
  console.log('Verification: out.js should reference out.js.map, and out.js.map should exist.');
  
  // Clean up dummy in.js after bundling
  fs.unlinkSync(inJsPath);
}).catch(err => {
  console.error('Bundling failed:', err);
  // Ensure cleanup even on error
  if (fs.existsSync(inJsPath)) fs.unlinkSync(inJsPath);
  if (fs.existsSync(outJsPath)) fs.unlinkSync(outJsPath);
  if (fs.existsSync(outMapPath)) fs.unlinkSync(outMapPath);
});

// To run this example:
// 1. Ensure you have Node.js installed.
// 2. npm install bit-bundler bit-bundler-minifyjs bit-bundler-extractsm
// 3. Save the code above as 'bundle.js'
// 4. Run: node bundle.js

view raw JSON →