Bit-Bundler Source Map Extraction Plugin
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
-
Sourcemap files named 'null.map' generated in current working directory.
cause The `bit-bundler` bundle operation was executed without a `dest` (destination) file path specified, causing `bit-bundler-extractsm` to misinterpret the output target.fixEnsure that `Bitbundler.bundle` is always called with a valid `dest` option, like `dest: 'out.js'`. For `bit-bundler-extractsm` versions prior to `2.1.2`, this was a known bug; upgrading to `2.1.2` or newer resolves the underlying issue. -
Source map file not generated or extracted (e.g., 'out.js.map' is missing).
cause Either no upstream `bit-bundler` plugin (e.g., `bit-bundler-minifyjs`, Babel plugin) is configured to produce source maps, or `bit-bundler-extractsm` is explicitly disabled in the bundler configuration.fixConfirm that your bundling pipeline includes a plugin that generates source maps and that `bit-bundler-extractsm` is placed after it in the `bundler` array. Also, ensure `bit-bundler-extractsm` is not configured to `false` for the affected bundle or split.
Warnings
- gotcha When `bit-bundler-extractsm` is used without a `dest` option for the main bundle, it might incorrectly generate sourcemap files named `null.map` in `process.cwd()`.
- gotcha `bit-bundler-extractsm` requires another bundler plugin (like `bit-bundler-minifyjs`) to actually generate source maps in the first place. If no source maps are being produced by prior steps in the pipeline, `extractsm` will have nothing to extract.
- gotcha To remove sourcemaps from a bundle or a specific bundle split, `bit-bundler-extractsm` must be configured with `false` or an object specifying `false` for named splits. Misconfiguration can lead to unexpected source map generation or removal.
Install
-
npm install bit-bundler-extractsm -
yarn add bit-bundler-extractsm -
pnpm add bit-bundler-extractsm
Imports
- Bitbundler
import Bitbundler from 'bit-bundler';
var Bitbundler = require("bit-bundler"); - Plugin Registration String
import { extractsm } from 'bit-bundler-extractsm';bundler: ["bit-bundler-extractsm"]
- Direct Module Reference (Rare)
import * as extractsmModule from 'bit-bundler-extractsm';
const extractsmModule = require('bit-bundler-extractsm');
Quickstart
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