vite-plugin-garfish-mf 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +56 -0
- package/dist/index.cjs +89 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.mjs +89 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mokeykingblack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
## vite-plugin-garfish-mf
|
|
2
|
+
|
|
3
|
+
A Vite plugin that helps you run a Vite sub-application inside a micro-frontend architecture powered by [Garfish](https://www.garfishjs.org).
|
|
4
|
+
|
|
5
|
+
This plugin adapts Vite-based apps to work smoothly as sub-applications, handling base URL configuration and sandbox compatibility.
|
|
6
|
+
|
|
7
|
+
### Installation
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install --save-dev vite-plugin-garfish-mf
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Getting Started
|
|
14
|
+
|
|
15
|
+
Add the plugin to sub-application's Vite configuration shown below (make sure the plugin is placed after the **react**/**vue** plugin, if present):
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
// vite.config.js
|
|
19
|
+
|
|
20
|
+
import { defineConfig } from "vite";
|
|
21
|
+
import react from "@vitejs/plugin-react";
|
|
22
|
+
import garfish from "vite-plugin-garfish-mf";
|
|
23
|
+
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
plugins: [react(), garfish({
|
|
26
|
+
base: "http://localhost:3000",
|
|
27
|
+
sandbox: true
|
|
28
|
+
})
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
| Property | Prequire | Type | Default | Description |
|
|
34
|
+
| :---------: | :------: | :-----: | :-----------------------: | :--------------------------------------------------------------------------------------------------------------------------- |
|
|
35
|
+
| **base** | [x] | string | **http://localhost:5173** | Base origin where the sub-application is served. Overrides server.port and server.origin |
|
|
36
|
+
| **sandbox** | [ ] | boolean | **true** | Enable this when running the sub-application inside a [Sandbox](https://www.garfishjs.org/api/run.html#sandbox) environment |
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### How It Works
|
|
40
|
+
|
|
41
|
+
- Overrides Vite’s dev server origin so the sub-app can be correctly loaded by Garfish
|
|
42
|
+
- Ensures assets and HMR work as expected in a micro-frontend context
|
|
43
|
+
- Supports sandboxed execution to avoid global scope conflicts
|
|
44
|
+
|
|
45
|
+
### Inspiration
|
|
46
|
+
|
|
47
|
+
This plugin is inspired by the following projects and communities:
|
|
48
|
+
|
|
49
|
+
- [vite-plugin-qiankun](https://github.com/tengmaoqing/vite-plugin-qiankun)
|
|
50
|
+
- [vite-plugin-qiankun-lite](https://github.com/kotarella1110/vite-plugin-qiankun-lite)
|
|
51
|
+
|
|
52
|
+
Their ideas and implementations were invaluable references when building this plugin.
|
|
53
|
+
|
|
54
|
+
### License
|
|
55
|
+
|
|
56
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
let cheerio = require("cheerio");
|
|
2
|
+
|
|
3
|
+
//#region src/utils.ts
|
|
4
|
+
function scriptTransform(script$, publicPath) {
|
|
5
|
+
let src = script$.attr("src");
|
|
6
|
+
if (!src) return;
|
|
7
|
+
script$.removeAttr("src").removeAttr("type").html(`import("${publicPath + src}")`);
|
|
8
|
+
return script$;
|
|
9
|
+
}
|
|
10
|
+
function reactHRMScriptTransfrom(script$, publicPath) {
|
|
11
|
+
script$.removeAttr("type").html(`
|
|
12
|
+
((window) => {
|
|
13
|
+
import("${publicPath}/@react-refresh").then(({default: RefreshRuntime}) => {
|
|
14
|
+
RefreshRuntime.injectIntoGlobalHook(window);
|
|
15
|
+
window.$RefreshReg$ = () => {};
|
|
16
|
+
window.$RefreshSig$ = () => (type) => type;
|
|
17
|
+
window.__vite_plugin_react_preamble_installed__ = true;
|
|
18
|
+
})
|
|
19
|
+
})(new Function("return this")());
|
|
20
|
+
`);
|
|
21
|
+
}
|
|
22
|
+
function injectGarfishProvider(script$, publicPath) {
|
|
23
|
+
let src = script$.attr("src");
|
|
24
|
+
if (!src) return;
|
|
25
|
+
script$.removeAttr("src").removeAttr("type").html(`
|
|
26
|
+
if (window.__GARFISH__ && typeof __GARFISH_EXPORTS__ === "object") {
|
|
27
|
+
let provider = new Promise((resolve) => {
|
|
28
|
+
import("${publicPath + src}").then(({provider}) => resolve(provider))
|
|
29
|
+
})
|
|
30
|
+
if (__GARFISH_EXPORTS__.registerProvider) {
|
|
31
|
+
__GARFISH_EXPORTS__.registerProvider(provider);
|
|
32
|
+
} else {
|
|
33
|
+
__GARFISH_EXPORTS__.provider = provider;
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
import("${publicPath + src}")
|
|
37
|
+
}
|
|
38
|
+
`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/plugin.ts
|
|
43
|
+
const vitePluginGarfish = ({ base = "http://localhost:5173", sandbox = true }) => {
|
|
44
|
+
let config;
|
|
45
|
+
let isUseWithReact;
|
|
46
|
+
return [{
|
|
47
|
+
name: "vite-plugin-garfish-fm:resolve-config",
|
|
48
|
+
enforce: "post",
|
|
49
|
+
config() {
|
|
50
|
+
let url = new URL(base);
|
|
51
|
+
return {
|
|
52
|
+
base,
|
|
53
|
+
server: {
|
|
54
|
+
port: Number(url.port),
|
|
55
|
+
origin: url.origin
|
|
56
|
+
},
|
|
57
|
+
preview: { port: Number(url.port) }
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
configResolved(resolvedConfig) {
|
|
61
|
+
config = resolvedConfig;
|
|
62
|
+
isUseWithReact = config.plugins.findIndex((plugin) => plugin.name === "vite:react-swc" || plugin.name === "vite:react-babel") !== -1;
|
|
63
|
+
}
|
|
64
|
+
}, {
|
|
65
|
+
name: "vite-plugin-garfish-fm:html-transfrom",
|
|
66
|
+
enforce: "post",
|
|
67
|
+
apply: "serve",
|
|
68
|
+
transformIndexHtml(html) {
|
|
69
|
+
const $ = (0, cheerio.load)(html);
|
|
70
|
+
const moduleScripts$ = $("body script[src][type], head script[src]");
|
|
71
|
+
if (sandbox) injectGarfishProvider(moduleScripts$.last(), base);
|
|
72
|
+
moduleScripts$.each((_, script$) => void scriptTransform($(script$), base));
|
|
73
|
+
if (isUseWithReact) {
|
|
74
|
+
const reactHMRScript = $("script:not([src])[type=module]").filter((_, el) => {
|
|
75
|
+
return ($(el).html() || "").includes("/@react-refresh");
|
|
76
|
+
});
|
|
77
|
+
if (reactHMRScript.length > 0) reactHRMScriptTransfrom($(reactHMRScript[0]), base);
|
|
78
|
+
}
|
|
79
|
+
return $.html();
|
|
80
|
+
}
|
|
81
|
+
}];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/index.ts
|
|
86
|
+
var src_default = vitePluginGarfish;
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
module.exports = src_default;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PluginOption } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/plugin.d.ts
|
|
4
|
+
type Options = {
|
|
5
|
+
base: string;
|
|
6
|
+
sandbox?: boolean;
|
|
7
|
+
};
|
|
8
|
+
declare const vitePluginGarfish: ({
|
|
9
|
+
base,
|
|
10
|
+
sandbox
|
|
11
|
+
}: Options) => PluginOption;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { type Options, vitePluginGarfish as default };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PluginOption } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/plugin.d.ts
|
|
4
|
+
type Options = {
|
|
5
|
+
base: string;
|
|
6
|
+
sandbox?: boolean;
|
|
7
|
+
};
|
|
8
|
+
declare const vitePluginGarfish: ({
|
|
9
|
+
base,
|
|
10
|
+
sandbox
|
|
11
|
+
}: Options) => PluginOption;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { type Options, vitePluginGarfish as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { load } from "cheerio";
|
|
2
|
+
|
|
3
|
+
//#region src/utils.ts
|
|
4
|
+
function scriptTransform(script$, publicPath) {
|
|
5
|
+
let src = script$.attr("src");
|
|
6
|
+
if (!src) return;
|
|
7
|
+
script$.removeAttr("src").removeAttr("type").html(`import("${publicPath + src}")`);
|
|
8
|
+
return script$;
|
|
9
|
+
}
|
|
10
|
+
function reactHRMScriptTransfrom(script$, publicPath) {
|
|
11
|
+
script$.removeAttr("type").html(`
|
|
12
|
+
((window) => {
|
|
13
|
+
import("${publicPath}/@react-refresh").then(({default: RefreshRuntime}) => {
|
|
14
|
+
RefreshRuntime.injectIntoGlobalHook(window);
|
|
15
|
+
window.$RefreshReg$ = () => {};
|
|
16
|
+
window.$RefreshSig$ = () => (type) => type;
|
|
17
|
+
window.__vite_plugin_react_preamble_installed__ = true;
|
|
18
|
+
})
|
|
19
|
+
})(new Function("return this")());
|
|
20
|
+
`);
|
|
21
|
+
}
|
|
22
|
+
function injectGarfishProvider(script$, publicPath) {
|
|
23
|
+
let src = script$.attr("src");
|
|
24
|
+
if (!src) return;
|
|
25
|
+
script$.removeAttr("src").removeAttr("type").html(`
|
|
26
|
+
if (window.__GARFISH__ && typeof __GARFISH_EXPORTS__ === "object") {
|
|
27
|
+
let provider = new Promise((resolve) => {
|
|
28
|
+
import("${publicPath + src}").then(({provider}) => resolve(provider))
|
|
29
|
+
})
|
|
30
|
+
if (__GARFISH_EXPORTS__.registerProvider) {
|
|
31
|
+
__GARFISH_EXPORTS__.registerProvider(provider);
|
|
32
|
+
} else {
|
|
33
|
+
__GARFISH_EXPORTS__.provider = provider;
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
import("${publicPath + src}")
|
|
37
|
+
}
|
|
38
|
+
`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/plugin.ts
|
|
43
|
+
const vitePluginGarfish = ({ base = "http://localhost:5173", sandbox = true }) => {
|
|
44
|
+
let config;
|
|
45
|
+
let isUseWithReact;
|
|
46
|
+
return [{
|
|
47
|
+
name: "vite-plugin-garfish-fm:resolve-config",
|
|
48
|
+
enforce: "post",
|
|
49
|
+
config() {
|
|
50
|
+
let url = new URL(base);
|
|
51
|
+
return {
|
|
52
|
+
base,
|
|
53
|
+
server: {
|
|
54
|
+
port: Number(url.port),
|
|
55
|
+
origin: url.origin
|
|
56
|
+
},
|
|
57
|
+
preview: { port: Number(url.port) }
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
configResolved(resolvedConfig) {
|
|
61
|
+
config = resolvedConfig;
|
|
62
|
+
isUseWithReact = config.plugins.findIndex((plugin) => plugin.name === "vite:react-swc" || plugin.name === "vite:react-babel") !== -1;
|
|
63
|
+
}
|
|
64
|
+
}, {
|
|
65
|
+
name: "vite-plugin-garfish-fm:html-transfrom",
|
|
66
|
+
enforce: "post",
|
|
67
|
+
apply: "serve",
|
|
68
|
+
transformIndexHtml(html) {
|
|
69
|
+
const $ = load(html);
|
|
70
|
+
const moduleScripts$ = $("body script[src][type], head script[src]");
|
|
71
|
+
if (sandbox) injectGarfishProvider(moduleScripts$.last(), base);
|
|
72
|
+
moduleScripts$.each((_, script$) => void scriptTransform($(script$), base));
|
|
73
|
+
if (isUseWithReact) {
|
|
74
|
+
const reactHMRScript = $("script:not([src])[type=module]").filter((_, el) => {
|
|
75
|
+
return ($(el).html() || "").includes("/@react-refresh");
|
|
76
|
+
});
|
|
77
|
+
if (reactHMRScript.length > 0) reactHRMScriptTransfrom($(reactHMRScript[0]), base);
|
|
78
|
+
}
|
|
79
|
+
return $.html();
|
|
80
|
+
}
|
|
81
|
+
}];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/index.ts
|
|
86
|
+
var src_default = vitePluginGarfish;
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
export { src_default as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-garfish-mf",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "A vite plugin for running Micro frontend applications using garfish",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"@garfish/react-bridge",
|
|
7
|
+
"garfish",
|
|
8
|
+
"micro frontend",
|
|
9
|
+
"micro frontends",
|
|
10
|
+
"micro-frontend",
|
|
11
|
+
"micro-frontends",
|
|
12
|
+
"microfrontend",
|
|
13
|
+
"microfrontends",
|
|
14
|
+
"react",
|
|
15
|
+
"vite",
|
|
16
|
+
"vite-plugin",
|
|
17
|
+
"vite-plugin-garfish"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/monkeykingblack/vite-plugin-garfish.git#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/monkeykingblack/vite-plugin-garfish/issues"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "monkeykingblack",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/monkeykingblack/vite-plugin-garfish.git"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"./dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"type": "module",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.mts",
|
|
39
|
+
"default": "./dist/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"cheerio": "^1.2.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"domhandler": "^5.0.3",
|
|
52
|
+
"tsdown": "^0.20.3"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"vite": ">= 4 < 8"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsdown"
|
|
59
|
+
}
|
|
60
|
+
}
|