Bit-Loader Shimmer Plugin

1.1.0 · maintenance · verified Tue Apr 21

The `bit-loader-shimmer` package provides a plugin for `bit-bundler`, enabling detailed control over how non-modular or legacy JavaScript modules are integrated into a modern bundler environment. Currently at version 1.1.0, this package allows developers to define 'shims' for specific modules, dictating their file paths, dependencies (imports), and what they expose to other modules or the global scope (exports). This is particularly useful for handling libraries that expect global variables, require a specific load order, or do not conform to CommonJS or ES module patterns. It offers granular configuration options, such as aliasing imports and exports, linking to global objects, and resolving modules by name or direct path. While a specific release cadence isn't published, the 1.x versioning suggests stability. Its key differentiator lies in its comprehensive API for managing the module environment for legacy scripts, bridging the gap between traditional script inclusion and modular bundling, especially within the `bit-bundler` ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `bit-loader-shimmer` to handle legacy libraries like jQuery and Bootstrap, managing their imports, exports, and global interactions within a `bit-bundler` setup.

const shim = require('bit-loader-shimmer');
const path = require('path');

const shimConfig = shim({
  // Shim configuration for jQuery, exposing it globally and as an export
  jquery: {
    // Use 'name' for module resolution by bit-bundler
    name: 'jquery/dist/jquery.js',
    // Define how jQuery exports itself or is made available
    exports: [{
      as: '$',
      name: 'window.$',
      global: ['$'] // Expose as global $ variable
    }, {
      as: 'jQuery',
      name: 'window.jQuery',
      global: ['jQuery'] // Expose as global jQuery variable
    }]
  },
  // Shim configuration for Bootstrap, which depends on jQuery
  bootstrap: {
    // Specify the direct path to the Bootstrap JavaScript file
    path: path.resolve('./node_modules/bootstrap/dist/js/bootstrap.js'),
    // Bootstrap expects jQuery to be available, so we import it
    imports: [{
      as: 'jQuery', // Create a local 'jQuery' variable for Bootstrap's scope
      name: 'jquery' // Refers to the 'jquery' shim defined above
    }],
    // If Bootstrap exposes a specific component, e.g., Modal, export it
    exports: [{
      as: 'Modal',
      name: 'window.bootstrap.Modal', // Assuming it attaches to window.bootstrap
      global: 'bootstrap.Modal'
    }]
  },
  // Example for a simple legacy script that might modify the global scope
  myLegacyScript: {
    path: './src/legacy-global-modifier.js'
    // This script runs, and can be ensured to load at a specific time.
  }
});

console.log("Generated shim configuration:\n", JSON.stringify(shimConfig, null, 2));
console.log("\nThis configuration object should be passed to bit-bundler's plugins array.");
// Example of how it would be used within bit-bundler (not runnable without bit-bundler setup):
// const BitBundler = require('bit-bundler');
// const bundler = new BitBundler({
//   src: ['./entry.js'],
//   dest: './bundle.js',
//   plugins: [shimConfig]
// });
// bundler.bundle().then(() => console.log('Bundled with shims!'));

view raw JSON →