mini-star
mini-star is a robust frontend micro-kernel framework, bundler, and loader designed to help projects implement a plugin-based architecture. It facilitates the independent development and deployment of features as separate plugins. The library, currently at version 2.1.8, supports TypeScript natively and is framework-agnostic, meaning it can be integrated with React, Vue, Angular, jQuery, or other modern web frameworks. Key differentiators include its bundler for building AMD-based plugins, automatic processing of plugin and dependency paths, dynamic asynchronous module loading to optimize project size, and a powerful mechanism for sharing common dependencies across all plugins, reducing boilerplate and bundle size. Unlike `requirejs`, `mini-star` operates at a module level, aligning with modern frontend practices, and it distinguishes itself from micro-frontend solutions like `qiankun` by prioritizing dependency reuse and an isomorphic technology stack rather than isolation for heterogeneous applications. It offers a complete toolchain encompassing CLI, runtime, and bundler functionalities. The project appears to have an active development and maintenance cadence, with recent documentation updates indicating ongoing support.
Common errors
-
Uncaught ReferenceError: React is not defined
cause A plugin attempted to use `React` but it was not globally available or registered as a shared dependency with `mini-star`.fixEnsure `React` (and other global dependencies) are properly imported and then registered using `ministar.registerShared('react', React);` in the host application before any plugins are loaded. -
Error: Plugin 'my-plugin-name' not found or failed to load. (Network error, or script parsing error)
cause The specified plugin script could not be fetched from the `base` URL or parsed correctly due to network issues, incorrect path, or syntax errors in the plugin bundle.fixVerify the `base` path in `MiniStar` configuration. Check the network tab in browser dev tools for 404s on the plugin URL. Ensure the plugin's bundle is correctly built and accessible at the expected location. -
TypeScript Error: Property 'render' does not exist on type 'Plugin'
cause The loaded plugin object does not match the expected interface, often because the plugin itself does not correctly expose the `render` method or the TypeScript type assertion is incorrect.fixEnsure your plugin's entry file explicitly exports the `render` function (or whatever methods you expect). If using TypeScript, cast the loaded plugin to its specific interface: `await ministar.loadPlugin<MyPluginInterface>('my-plugin');`
Warnings
- gotcha Ensure all shared dependencies are registered with `ministar.registerShared()` *before* attempting to load any plugins that rely on those dependencies. Failing to do so will result in runtime errors within the plugin due to missing globals.
- gotcha Plugin path resolution can be tricky, especially when moving between development and production environments. Relative paths or incorrect `base` configuration in `MiniStar` can lead to 'Plugin not found' errors.
- breaking `mini-star` has moved towards a modern module approach. Older CommonJS `require()` patterns for the main `mini-star` library itself might not work as expected or could lead to compatibility issues in projects configured for ESM.
- gotcha When a plugin fails to load or execute, `mini-star`'s default error handling might be generic. Implementing a custom `errorHandler` in `MiniStarConfig` is crucial for better diagnostics in production.
Install
-
npm install mini-star -
yarn add mini-star -
pnpm add mini-star
Imports
- MiniStar
const MiniStar = require('mini-star');import { MiniStar } from 'mini-star'; - MiniStarConfig
import type { MiniStarConfig } from 'mini-star'; - Plugin
import type { Plugin } from 'mini-star';
Quickstart
import { MiniStar } from 'mini-star';
interface MyPluginInstance {
render: (selector: string) => void;
// ... other methods/properties exposed by the plugin
}
async function initializeMiniStar() {
const ministar = new MiniStar({
// Optional: Configure base path for plugins, e.g., for local development or CDN
base: '/plugins/',
// Optional: Global error handler for plugin loading failures
errorHandler: (error, pluginName) => {
console.error(`Failed to load plugin ${pluginName}:`, error);
}
});
// Register shared dependencies that plugins might use, preventing duplication
// This should be done before loading plugins that depend on them.
ministar.registerShared('react', window.React || require('react'));
ministar.registerShared('react-dom', window.ReactDOM || require('react-dom'));
console.log('Shared dependencies registered.');
try {
// Load a plugin by its registered name or path
const myPlugin = await ministar.loadPlugin<MyPluginInstance>('my-plugin-entry');
console.log('Plugin loaded successfully:', myPlugin);
// Assume the plugin exports a render function
if (myPlugin && typeof myPlugin.render === 'function') {
myPlugin.render('#app-root');
console.log('Plugin rendered to #app-root');
}
} catch (error) {
console.error('Error during plugin loading or rendering:', error);
}
}
// Ensure React and ReactDOM are available globally for demonstration if not bundled
// In a real app, these would be proper imports in your host application
(window as any).React = require('react');
(window as any).ReactDOM = require('react-dom');
initializeMiniStar();