Bit-Loader Shimmer Plugin
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
-
ReferenceError: $ is not defined
cause A shimmed module (e.g., Bootstrap) expects jQuery's '$' global to be available, but the jQuery shim either wasn't processed or didn't correctly expose '$' to the global scope or to subsequent shims.fixEnsure the shim for jQuery (or similar dependency) is loaded before the dependent module and that its `exports` configuration correctly includes `global: ['$']` (and potentially `name: 'window.$'`) to make the variable available. -
Module not found: Can't resolve 'my-library/dist/my-file.js'
cause The `path` or `name` property within a shim configuration points to an incorrect or non-existent file or an unresolvable module identifier.fixVerify the `path` property contains the correct, absolute, or relative-to-config-file path to the shimmed file. If using `name`, ensure the module is installed and its main entry point or sub-path is correctly specified according to Node.js resolution rules. -
Error: Cannot read properties of undefined (reading 'Modal')
cause A downstream module attempts to import a named export (e.g., `Modal`) from a shimmed module, but the shim's `exports` configuration did not correctly define and expose that specific property.fixReview the `exports` array for the shimmed module, ensuring that the `as`, `name`, and `global` properties correctly map the internal variable or global object property to the desired export name.
Warnings
- gotcha Incorrectly configured `imports` or `exports` can lead to runtime errors, undefined globals, or modules not receiving their expected dependencies. Complex interactions with global objects require careful setup.
- gotcha Shimming large, monolithic libraries with many internal dependencies can be brittle and difficult to maintain. The shim configuration might not fully capture the library's internal loading mechanisms.
- gotcha Relative paths specified in the `path` option are resolved relative to the bit-bundler configuration file, not necessarily the current working directory or the shimmed module itself.
Install
-
npm install bit-loader-shimmer -
yarn add bit-loader-shimmer -
pnpm add bit-loader-shimmer
Imports
- shim
import { shim } from 'bit-loader-shimmer'import shim from 'bit-loader-shimmer'
- shim
const shim = require('bit-loader-shimmer') - shim
import * as shim from 'bit-loader-shimmer'
Quickstart
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!'));