Plugin System

Wujie's plugin system lets you transform and intercept sub-app code at runtime without modifying the sub-app's source repository. See the plugins API for type definitions.
html-loader
Modifies the sub-app's HTML template string at runtime.
- Example
const plugins = [
{
// Replace "aaa" with "bbb" in the sub-app's HTML template
htmlLoader: (code) => {
return code.replace("aaa", "bbb");
},
},
];
js-excludes
Excludes specific JavaScript files so they are not executed when loading the sub-app.
- Example
const plugins = [
// Scripts matching "http://xxxxx.js" or /test\.js/ will not be executed
{ jsExcludes: ["http://xxxxx.js", /test\.js/] },
];
js-ignores
Allows specific JavaScript files to be loaded natively by the sub-app via <script> tags rather than fetched and proxied by Wujie (useful when external scripts do not support CORS).
- Example
const plugins = [
// Scripts matching "http://xxxxx.js" or /test\.js/ will be loaded directly by the sub-app
{ jsIgnores: ["http://xxxxx.js", /test\.js/] },
];
Warning
Because scripts listed in jsIgnores are loaded directly without location proxying, any assignment to window.location.href inside them must be replaced with window.$wujie.location.href; otherwise, the sub-app's iframe sandbox will be navigated away and destroyed.
js-before-loaders
Runs before all JavaScript files in the sub-app's HTML to:
- Load an external script (
src="http://xxxxx") inside the sub-app. - Run an inline script (
<script>content</script>) inside the sub-app. - Execute a callback function.
- Example
const plugins = [
{
// Executed before all sub-app scripts
jsBeforeLoaders: [
// Inject an external script
{ src: "http://xxxx.js" },
// Inject an inline script
{ content: 'console.log("test")' },
// Execute a callback and log the sub-app name
{
callback(appWindow) {
console.log("js-before-loader-callback", appWindow.__WUJIE.id);
},
},
],
},
];
js-loader
Transforms the source code of sub-app JavaScript files at runtime.
- Example
const plugins = [
{
// Replace "aaa" with "bbb" in aaa.js
// code: script source, url: script URL ("" for inline scripts), base: sub-app base URL
jsLoader: (code, url, base) => {
if (url === "aaa.js") return code.replace("aaa", "bbb");
},
},
];
Warning
- ES module (
type="module") scripts are not processed byjsLoader. - Scripts matched by
jsIgnoresare not processed byjsLoader.
js-after-loaders
Runs after all JavaScript files in the sub-app's HTML to:
- Load an external script (
src="http://xxxxx") inside the sub-app. - Run an inline script (
<script>content</script>) inside the sub-app. - Execute a callback function.
- Example
const plugins = [
{
jsAfterLoaders: [
// Inject an external script
{ src: "http://xxxx.js" },
// Inject an inline script
{ content: 'console.log("test")' },
// Execute a callback and log the sub-app name
{
callback(appWindow) {
console.log("js-after-loader-callback", appWindow.__WUJIE.id);
},
},
],
},
];
css-excludes
Excludes specific CSS stylesheets so they are not loaded by the sub-app.
- Example
const plugins = [
// Stylesheets matching "http://xxxxx.css" or /test\.css/ will not be loaded
{ cssExcludes: ["http://xxxxx.css", /test\.css/] },
];
css-ignores
Allows specific CSS stylesheets to be loaded natively by the sub-app via <link> tags rather than fetched by Wujie (useful when external stylesheets do not support CORS).
- Example
const plugins = [
// Stylesheets matching "http://xxxxx.css" or /test\.css/ will be loaded directly by the sub-app
{ cssIgnores: ["http://xxxxx.css", /test\.css/] },
];
css-before-loaders
Runs before all stylesheets in the sub-app's HTML to:
- Insert an external stylesheet (
src="http://xxxxx"). - Insert an inline
<style>content</style>block.
- Example
const plugins = [
{
// Applied before all sub-app stylesheets
cssBeforeLoaders: [
// Insert an external stylesheet before HTML styles
{ src: "https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" },
// Insert an inline style before HTML styles
{ content: "img{width: 300px}" },
],
},
];
css-loader
Transforms the sub-app's CSS text at runtime.
- Example
const plugins = [
{
// Dynamically transform CSS source
// code: CSS source, url: stylesheet URL ("" for inline styles), base: sub-app base URL
cssLoader: (code, url, base) => {
console.log("css-loader", url, code.slice(0, 50) + "...");
return code;
},
},
];
css-after-loaders
Runs after all stylesheets in the sub-app's HTML to:
- Insert an external stylesheet (
src="http://xxxxx"). - Insert an inline
<style>content</style>block.
- Example
const plugins = [
{
cssAfterLoaders: [
// Insert an external stylesheet after HTML styles
{ src: "https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" },
// Insert an inline style after HTML styles
{ content: "img{height: 300px}" },
],
},
];
windowAddEventListenerHook
Callback invoked whenever the sub-app calls window.addEventListener.
- Example
Because the sub-app's DOM renders inside a Web Component while its JS runs inside an iframe, scrolling often happens on an outer container rather than the iframe window. Listening to window scroll events directly will not fire, so you can forward them to the scrolling container:
const plugins = [
{
windowAddEventListenerHook(iframeWindow, type, handler, options) {
container.addEventListener(type, handler, options);
},
},
];
windowRemoveEventListenerHook
Callback invoked whenever the sub-app calls window.removeEventListener.
- Example
const plugins = [
{
windowAddEventListenerHook(iframeWindow, type, handler, options) {
container.addEventListener(type, handler, options);
},
windowRemoveEventListenerHook(iframeWindow, type, handler, options) {
container.removeEventListener(type, handler, options);
},
},
];
documentAddEventListenerHook
Callback invoked whenever the sub-app calls document.addEventListener.
- Example
Similarly, if the sub-app listens for scroll events on document, you can forward them to the outer scrolling container:
const plugins = [
{
documentAddEventListenerHook(iframeWindow, type, handler, options) {
container.addEventListener(type, handler, options);
},
},
];
documentRemoveEventListenerHook
Callback invoked whenever the sub-app calls document.removeEventListener.
- Example
const plugins = [
{
documentAddEventListenerHook(iframeWindow, type, handler, options) {
container.addEventListener(type, handler, options);
},
documentRemoveEventListenerHook(iframeWindow, type, handler, options) {
container.removeEventListener(type, handler, options);
},
},
];
appendOrInsertElementHook
Callback invoked after the sub-app appends or inserts an element into body or head.
- Example
const plugins = [
{
// element: the element actually inserted, iframeWindow: sub-app window, rawElement: original element
appendOrInsertElementHook(element, iframeWindow, rawElement) {
console.log(element, iframeWindow, rawElement);
},
},
];
patchElementHook
Callback invoked after the sub-app creates a DOM element.
- Example
const plugins = [
{
patchElementHook(element, iframeWindow) {
console.log(element, iframeWindow);
},
},
];
