@tiny-codes/vite-plugin-qiankun 2.0.0
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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/es/helper.d.ts +17 -0
- package/es/helper.js +14 -0
- package/es/helper.js.map +1 -0
- package/es/index.d.ts +28 -0
- package/es/index.js +119 -0
- package/es/index.js.map +1 -0
- package/es/utils.d.ts +3 -0
- package/es/utils.js +26 -0
- package/es/utils.js.map +1 -0
- package/lib/helper.d.ts +17 -0
- package/lib/helper.js +44 -0
- package/lib/helper.js.map +7 -0
- package/lib/index.d.ts +28 -0
- package/lib/index.js +155 -0
- package/lib/index.js.map +7 -0
- package/lib/utils.d.ts +3 -0
- package/lib/utils.js +59 -0
- package/lib/utils.js.map +7 -0
- package/package.json +69 -0
- package/readme.md +119 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @tiny-codes/vite-plugin-qiankun
|
|
2
|
+
|
|
3
|
+
## v2.0.0
|
|
4
|
+
|
|
5
|
+
2024-4-1
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- Always respect the `__INJECTED_PUBLIC_PATH_BY_QIANKUN__` to ensure correct resource loading in micro apps.
|
|
10
|
+
- Add `changeScriptOrigin` option to shut down the behavior.
|
|
11
|
+
- Always setup a dev server to enable running micro apps in development mode.
|
|
12
|
+
|
|
13
|
+
## v1.0.15
|
|
14
|
+
|
|
15
|
+
2024-4-1
|
|
16
|
+
|
|
17
|
+
forked from [https://github.com/tengmaoqing/vite-plugin-qiankun](https://github.com/tengmaoqing/vite-plugin-qiankun)
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 TMQ
|
|
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/es/helper.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface QiankunProps {
|
|
2
|
+
container?: HTMLElement;
|
|
3
|
+
[x: string]: unknown;
|
|
4
|
+
}
|
|
5
|
+
export interface QiankunLifeCycle {
|
|
6
|
+
bootstrap: (props?: QiankunProps) => void | Promise<void>;
|
|
7
|
+
mount: (props: QiankunProps) => void | Promise<void>;
|
|
8
|
+
unmount: (props: QiankunProps) => void | Promise<void>;
|
|
9
|
+
update: (props: QiankunProps) => void | Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export interface QiankunWindow {
|
|
12
|
+
__POWERED_BY_QIANKUN__?: boolean;
|
|
13
|
+
[x: string]: any;
|
|
14
|
+
}
|
|
15
|
+
export declare const qiankunWindow: QiankunWindow;
|
|
16
|
+
export declare const renderWithQiankun: (qiankunLifeCycle: QiankunLifeCycle) => void;
|
|
17
|
+
export default renderWithQiankun;
|
package/es/helper.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export var qiankunWindow = typeof window !== 'undefined' ? window.proxy || window : {};
|
|
2
|
+
export var renderWithQiankun = function renderWithQiankun(qiankunLifeCycle) {
|
|
3
|
+
// 函数只有一次执行机会,需要把生命周期赋值给全局
|
|
4
|
+
if (qiankunWindow !== null && qiankunWindow !== void 0 && qiankunWindow.__POWERED_BY_QIANKUN__) {
|
|
5
|
+
if (!window.moudleQiankunAppLifeCycles) {
|
|
6
|
+
window.moudleQiankunAppLifeCycles = {};
|
|
7
|
+
}
|
|
8
|
+
if (qiankunWindow.qiankunName) {
|
|
9
|
+
window.moudleQiankunAppLifeCycles[qiankunWindow.qiankunName] = qiankunLifeCycle;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
export default renderWithQiankun;
|
|
14
|
+
//# sourceMappingURL=helper.js.map
|
package/es/helper.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["qiankunWindow","window","proxy","renderWithQiankun","qiankunLifeCycle","__POWERED_BY_QIANKUN__","moudleQiankunAppLifeCycles","qiankunName"],"sources":["../src/helper.ts"],"sourcesContent":["export interface QiankunProps {\n container?: HTMLElement;\n [x: string]: unknown;\n}\n\nexport interface QiankunLifeCycle {\n bootstrap: (props?: QiankunProps) => void | Promise<void>;\n mount: (props: QiankunProps) => void | Promise<void>;\n unmount: (props: QiankunProps) => void | Promise<void>;\n update: (props: QiankunProps) => void | Promise<void>;\n}\n\nexport interface QiankunWindow {\n __POWERED_BY_QIANKUN__?: boolean;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [x: string]: any;\n}\n\nexport const qiankunWindow: QiankunWindow = typeof window !== 'undefined' ? window.proxy || window : {};\n\nexport const renderWithQiankun = (qiankunLifeCycle: QiankunLifeCycle) => {\n // 函数只有一次执行机会,需要把生命周期赋值给全局\n if (qiankunWindow?.__POWERED_BY_QIANKUN__) {\n if (!window.moudleQiankunAppLifeCycles) {\n window.moudleQiankunAppLifeCycles = {};\n }\n if (qiankunWindow.qiankunName) {\n window.moudleQiankunAppLifeCycles[qiankunWindow.qiankunName] = qiankunLifeCycle;\n }\n }\n};\n\nexport default renderWithQiankun;\n"],"mappings":"AAkBA,OAAO,IAAMA,aAA4B,GAAG,OAAOC,MAAM,KAAK,WAAW,GAAGA,MAAM,CAACC,KAAK,IAAID,MAAM,GAAG,CAAC,CAAC;AAEvG,OAAO,IAAME,iBAAiB,GAAG,SAApBA,iBAAiBA,CAAIC,gBAAkC,EAAK;EACvE;EACA,IAAIJ,aAAa,aAAbA,aAAa,eAAbA,aAAa,CAAEK,sBAAsB,EAAE;IACzC,IAAI,CAACJ,MAAM,CAACK,0BAA0B,EAAE;MACtCL,MAAM,CAACK,0BAA0B,GAAG,CAAC,CAAC;IACxC;IACA,IAAIN,aAAa,CAACO,WAAW,EAAE;MAC7BN,MAAM,CAACK,0BAA0B,CAACN,aAAa,CAACO,WAAW,CAAC,GAAGH,gBAAgB;IACjF;EACF;AACF,CAAC;AAED,eAAeD,iBAAiB"}
|
package/es/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PluginOption } from 'vite';
|
|
2
|
+
export * from './helper';
|
|
3
|
+
export interface MicroOption {
|
|
4
|
+
/**
|
|
5
|
+
* Whether to change the origin of entry script tag for micro frontend. It's useful when the micro
|
|
6
|
+
* frontend is deployed on a different domain or path.
|
|
7
|
+
*
|
|
8
|
+
* > If the origin by the qiankun's default algorithm is not correct for you, please try the
|
|
9
|
+
* > [getPublicPath](https://qiankun.umijs.org/zh/api) option.
|
|
10
|
+
*
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
changeScriptOrigin?: boolean;
|
|
14
|
+
}
|
|
15
|
+
type PluginFn = (appName: string, pluginOptions?: MicroOption) => PluginOption;
|
|
16
|
+
/**
|
|
17
|
+
* Vite plugin for integrating with Qiankun micro frontend.
|
|
18
|
+
*
|
|
19
|
+
* @param appName The name of the Qiankun sub app.
|
|
20
|
+
* @param pluginOptions Options for configuring the micro frontend behavior.
|
|
21
|
+
*
|
|
22
|
+
* > If the micro app is deployed on a different domain or path, you may need to adjust the public
|
|
23
|
+
* > path. Please have a look at the [getPublicPath](https://qiankun.umijs.org/zh/api) option.
|
|
24
|
+
*
|
|
25
|
+
* @returns A Vite plugin option object.
|
|
26
|
+
*/
|
|
27
|
+
declare const qiankunPlugin: PluginFn;
|
|
28
|
+
export default qiankunPlugin;
|
package/es/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { load } from 'cheerio';
|
|
2
|
+
import { detectIndent, space } from "./utils";
|
|
3
|
+
export * from "./helper";
|
|
4
|
+
/**
|
|
5
|
+
* Vite plugin for integrating with Qiankun micro frontend.
|
|
6
|
+
*
|
|
7
|
+
* @param appName The name of the Qiankun sub app.
|
|
8
|
+
* @param pluginOptions Options for configuring the micro frontend behavior.
|
|
9
|
+
*
|
|
10
|
+
* > If the micro app is deployed on a different domain or path, you may need to adjust the public
|
|
11
|
+
* > path. Please have a look at the [getPublicPath](https://qiankun.umijs.org/zh/api) option.
|
|
12
|
+
*
|
|
13
|
+
* @returns A Vite plugin option object.
|
|
14
|
+
*/
|
|
15
|
+
var qiankunPlugin = function qiankunPlugin(appName) {
|
|
16
|
+
var pluginOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
17
|
+
var _pluginOptions$change = pluginOptions.changeScriptOrigin,
|
|
18
|
+
changeScriptOrigin = _pluginOptions$change === void 0 ? true : _pluginOptions$change;
|
|
19
|
+
return {
|
|
20
|
+
name: 'vite-plugin-qiankun',
|
|
21
|
+
transformIndexHtml: function transformIndexHtml(html, context) {
|
|
22
|
+
var _$$get;
|
|
23
|
+
var $ = load(html, {
|
|
24
|
+
sourceCodeLocationInfo: true
|
|
25
|
+
});
|
|
26
|
+
var entryScript = (_$$get = $('script[entry]').get(0)) !== null && _$$get !== void 0 ? _$$get : $('body script[type=module], head script[crossorigin=""]').get(0);
|
|
27
|
+
if (entryScript) {
|
|
28
|
+
var _script$$html;
|
|
29
|
+
var scriptBaseIndent = detectIndent(html, entryScript);
|
|
30
|
+
var S0 = scriptBaseIndent;
|
|
31
|
+
var S1 = scriptBaseIndent + space(2);
|
|
32
|
+
var S2 = S1 + space(2);
|
|
33
|
+
var script$ = module2DynamicImport({
|
|
34
|
+
$: $,
|
|
35
|
+
scriptTag: entryScript,
|
|
36
|
+
changeScriptOrigin: changeScriptOrigin,
|
|
37
|
+
ident: S1
|
|
38
|
+
});
|
|
39
|
+
script$ === null || script$ === void 0 || script$.html("".concat((_script$$html = script$.html()) === null || _script$$html === void 0 ? void 0 : _script$$html.trimEnd(), ".finally(() => {\n").concat(S2).concat(createImportFinallyResolve(appName, {
|
|
40
|
+
indent: S2
|
|
41
|
+
}).trim(), "\n").concat(S1, "});\n").concat(S0));
|
|
42
|
+
var bodyBaseIndent = detectIndent(html, $('body').get(0));
|
|
43
|
+
var B1 = bodyBaseIndent + space(2);
|
|
44
|
+
var B2 = B1 + space(2);
|
|
45
|
+
$('body').append("\n".concat(B1, "<script>\n").concat(B2).concat(createQiankunHelper(appName, {
|
|
46
|
+
indent: B2 + space(2)
|
|
47
|
+
}).trim(), "\n").concat(B1, "</script>\n"));
|
|
48
|
+
var output = $.html();
|
|
49
|
+
return output;
|
|
50
|
+
} else {
|
|
51
|
+
console.warn('\x1b[33m%s\x1b[0m', '⚠️ Patch for qiankun failed, because the entry script was not found.');
|
|
52
|
+
return html;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
configureServer: function configureServer(server) {
|
|
56
|
+
var base = server.config.base;
|
|
57
|
+
return function () {
|
|
58
|
+
var patchFiles = ["".concat(base, "@vite/client")];
|
|
59
|
+
patchFiles.forEach(function (file) {
|
|
60
|
+
server.middlewares.use(file, function (req, res, next) {
|
|
61
|
+
var end = res.end.bind(res);
|
|
62
|
+
res.end = function (chunk) {
|
|
63
|
+
if (typeof chunk === 'string') {
|
|
64
|
+
var $ = load(chunk);
|
|
65
|
+
module2DynamicImport({
|
|
66
|
+
$: $,
|
|
67
|
+
scriptTag: $("script[src=\"".concat(file, "\"]")).get(0),
|
|
68
|
+
changeScriptOrigin: changeScriptOrigin
|
|
69
|
+
});
|
|
70
|
+
chunk = $.html();
|
|
71
|
+
}
|
|
72
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
73
|
+
rest[_key - 1] = arguments[_key];
|
|
74
|
+
}
|
|
75
|
+
end.apply(void 0, [chunk].concat(rest));
|
|
76
|
+
return this;
|
|
77
|
+
};
|
|
78
|
+
next();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
function module2DynamicImport(options) {
|
|
86
|
+
var $ = options.$,
|
|
87
|
+
scriptTag = options.scriptTag,
|
|
88
|
+
changeScriptOrigin = options.changeScriptOrigin,
|
|
89
|
+
_options$ident = options.ident,
|
|
90
|
+
ident = _options$ident === void 0 ? '' : _options$ident;
|
|
91
|
+
if (!scriptTag) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
var script$ = $(scriptTag);
|
|
95
|
+
var moduleSrc = script$.attr('src');
|
|
96
|
+
var appendBase = "''";
|
|
97
|
+
if (changeScriptOrigin) {
|
|
98
|
+
appendBase = "window.proxy ? window.proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || '' : ''";
|
|
99
|
+
}
|
|
100
|
+
script$.removeAttr('src');
|
|
101
|
+
script$.removeAttr('type');
|
|
102
|
+
var space = ident;
|
|
103
|
+
script$.html("\n".concat(space, "const publicUrl = ").concat(appendBase, ";\n").concat(space, "let assetUrl = '").concat(moduleSrc, "';\n").concat(space, "if (!assetUrl.match(/^https?/i)) {\n").concat(space, " assetUrl = publicUrl + assetUrl;\n").concat(space, "}\n").concat(space, "import(assetUrl)"));
|
|
104
|
+
return script$;
|
|
105
|
+
}
|
|
106
|
+
function createImportFinallyResolve(qiankunName, options) {
|
|
107
|
+
var _ref = options !== null && options !== void 0 ? options : {},
|
|
108
|
+
_ref$indent = _ref.indent,
|
|
109
|
+
space = _ref$indent === void 0 ? ' ' : _ref$indent;
|
|
110
|
+
return "\n".concat(space, "const qiankunLifeCycle = window.moudleQiankunAppLifeCycles && window.moudleQiankunAppLifeCycles['").concat(qiankunName, "'];\n").concat(space, "if (qiankunLifeCycle) {\n").concat(space, " window.proxy.vitemount((props) => qiankunLifeCycle.mount(props));\n").concat(space, " window.proxy.viteunmount((props) => qiankunLifeCycle.unmount(props));\n").concat(space, " window.proxy.vitebootstrap(() => qiankunLifeCycle.bootstrap());\n").concat(space, " window.proxy.viteupdate((props) => qiankunLifeCycle.update(props));\n").concat(space, "}\n");
|
|
111
|
+
}
|
|
112
|
+
function createQiankunHelper(qiankunName, options) {
|
|
113
|
+
var _ref2 = options !== null && options !== void 0 ? options : {},
|
|
114
|
+
_ref2$indent = _ref2.indent,
|
|
115
|
+
space = _ref2$indent === void 0 ? ' ' : _ref2$indent;
|
|
116
|
+
return "\n".concat(space, "const createDeffer = (hookName) => {\n").concat(space, " const d = new Promise((resolve, reject) => {\n").concat(space, " window.proxy && (window.proxy[`vite${hookName}`] = resolve)\n").concat(space, " })\n").concat(space, " return props => d.then(fn => fn(props));\n").concat(space, "}\n").concat(space, "const bootstrap = createDeffer('bootstrap');\n").concat(space, "const mount = createDeffer('mount');\n").concat(space, "const unmount = createDeffer('unmount');\n").concat(space, "const update = createDeffer('update');\n\n").concat(space, ";(global => {\n").concat(space, " global.qiankunName = '").concat(qiankunName, "';\n").concat(space, " global['").concat(qiankunName, "'] = {\n").concat(space, " bootstrap,\n").concat(space, " mount,\n").concat(space, " unmount,\n").concat(space, " update\n").concat(space, " };\n").concat(space, "})(window);\n");
|
|
117
|
+
}
|
|
118
|
+
export default qiankunPlugin;
|
|
119
|
+
//# sourceMappingURL=index.js.map
|
package/es/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["load","detectIndent","space","qiankunPlugin","appName","pluginOptions","arguments","length","undefined","_pluginOptions$change","changeScriptOrigin","name","transformIndexHtml","html","context","_$$get","$","sourceCodeLocationInfo","entryScript","get","_script$$html","scriptBaseIndent","S0","S1","S2","script$","module2DynamicImport","scriptTag","ident","concat","trimEnd","createImportFinallyResolve","indent","trim","bodyBaseIndent","B1","B2","append","createQiankunHelper","output","console","warn","configureServer","server","base","config","patchFiles","forEach","file","middlewares","use","req","res","next","end","bind","chunk","_len","rest","Array","_key","apply","options","_options$ident","moduleSrc","attr","appendBase","removeAttr","qiankunName","_ref","_ref$indent","_ref2","_ref2$indent"],"sources":["../src/index.ts"],"sourcesContent":["import type { CheerioAPI } from 'cheerio';\nimport { load } from 'cheerio';\nimport type { Element } from 'domhandler';\nimport type { PluginOption } from 'vite';\nimport { detectIndent, space } from './utils';\n\nexport * from './helper';\n\nexport interface MicroOption {\n /**\n * Whether to change the origin of entry script tag for micro frontend. It's useful when the micro\n * frontend is deployed on a different domain or path.\n *\n * > If the origin by the qiankun's default algorithm is not correct for you, please try the\n * > [getPublicPath](https://qiankun.umijs.org/zh/api) option.\n *\n * @default true\n */\n changeScriptOrigin?: boolean;\n}\ntype PluginFn = (appName: string, pluginOptions?: MicroOption) => PluginOption;\n\n/**\n * Vite plugin for integrating with Qiankun micro frontend.\n *\n * @param appName The name of the Qiankun sub app.\n * @param pluginOptions Options for configuring the micro frontend behavior.\n *\n * > If the micro app is deployed on a different domain or path, you may need to adjust the public\n * > path. Please have a look at the [getPublicPath](https://qiankun.umijs.org/zh/api) option.\n *\n * @returns A Vite plugin option object.\n */\nconst qiankunPlugin: PluginFn = (appName, pluginOptions = {}) => {\n const { changeScriptOrigin = true } = pluginOptions;\n\n return {\n name: 'vite-plugin-qiankun',\n\n transformIndexHtml(html: string, context) {\n const $ = load(html, { sourceCodeLocationInfo: true });\n const entryScript =\n $('script[entry]').get(0) ?? $('body script[type=module], head script[crossorigin=\"\"]').get(0);\n if (entryScript) {\n const scriptBaseIndent = detectIndent(html, entryScript);\n const S0 = scriptBaseIndent;\n const S1 = scriptBaseIndent + space(2);\n const S2 = S1 + space(2);\n const script$ = module2DynamicImport({\n $,\n scriptTag: entryScript,\n changeScriptOrigin,\n ident: S1,\n });\n script$?.html(`${script$.html()?.trimEnd()}.finally(() => {\n${S2}${createImportFinallyResolve(appName, { indent: S2 }).trim()}\n${S1}});\n${S0}`);\n\n const bodyBaseIndent = detectIndent(html, $('body').get(0));\n const B1 = bodyBaseIndent + space(2);\n const B2 = B1 + space(2);\n $('body').append(`\n${B1}<script>\n${B2}${createQiankunHelper(appName, { indent: B2 + space(2) }).trim()}\n${B1}</script>\n`);\n const output = $.html();\n return output;\n } else {\n console.warn('\\x1b[33m%s\\x1b[0m', '⚠️ Patch for qiankun failed, because the entry script was not found.');\n return html;\n }\n },\n configureServer(server) {\n const base = server.config.base;\n return () => {\n const patchFiles = [`${base}@vite/client`];\n patchFiles.forEach((file) => {\n server.middlewares.use(file, (req, res, next) => {\n const end = res.end.bind(res);\n res.end = function (this: typeof res, chunk: unknown, ...rest: any[]) {\n if (typeof chunk === 'string') {\n const $ = load(chunk);\n module2DynamicImport({ $, scriptTag: $(`script[src=\"${file}\"]`).get(0), changeScriptOrigin });\n chunk = $.html();\n }\n end(chunk, ...rest);\n return this;\n } as unknown as typeof res.end;\n next();\n });\n });\n };\n },\n };\n};\n\nfunction module2DynamicImport(\n options: { $: CheerioAPI; scriptTag: Element | undefined; ident?: string } & Pick<MicroOption, 'changeScriptOrigin'>,\n) {\n const { $, scriptTag, changeScriptOrigin, ident = '' } = options;\n if (!scriptTag) {\n return;\n }\n const script$ = $(scriptTag);\n const moduleSrc = script$.attr('src');\n let appendBase = \"''\";\n if (changeScriptOrigin) {\n appendBase = \"window.proxy ? window.proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || '' : ''\";\n }\n script$.removeAttr('src');\n script$.removeAttr('type');\n const space = ident;\n script$.html(`\n${space}const publicUrl = ${appendBase};\n${space}let assetUrl = '${moduleSrc}';\n${space}if (!assetUrl.match(/^https?/i)) {\n${space} assetUrl = publicUrl + assetUrl;\n${space}}\n${space}import(assetUrl)`);\n return script$;\n}\n\nfunction createImportFinallyResolve(qiankunName: string, options?: { indent?: string }) {\n const { indent: space = ' ' } = options ?? {};\n return `\n${space}const qiankunLifeCycle = window.moudleQiankunAppLifeCycles && window.moudleQiankunAppLifeCycles['${qiankunName}'];\n${space}if (qiankunLifeCycle) {\n${space} window.proxy.vitemount((props) => qiankunLifeCycle.mount(props));\n${space} window.proxy.viteunmount((props) => qiankunLifeCycle.unmount(props));\n${space} window.proxy.vitebootstrap(() => qiankunLifeCycle.bootstrap());\n${space} window.proxy.viteupdate((props) => qiankunLifeCycle.update(props));\n${space}}\n`;\n}\n\nfunction createQiankunHelper(qiankunName: string, options?: { indent?: string }) {\n const { indent: space = ' ' } = options ?? {};\n return `\n${space}const createDeffer = (hookName) => {\n${space} const d = new Promise((resolve, reject) => {\n${space} window.proxy && (window.proxy[\\`vite\\${hookName}\\`] = resolve)\n${space} })\n${space} return props => d.then(fn => fn(props));\n${space}}\n${space}const bootstrap = createDeffer('bootstrap');\n${space}const mount = createDeffer('mount');\n${space}const unmount = createDeffer('unmount');\n${space}const update = createDeffer('update');\n\n${space};(global => {\n${space} global.qiankunName = '${qiankunName}';\n${space} global['${qiankunName}'] = {\n${space} bootstrap,\n${space} mount,\n${space} unmount,\n${space} update\n${space} };\n${space}})(window);\n`;\n}\n\nexport default qiankunPlugin;\n"],"mappings":"AACA,SAASA,IAAI,QAAQ,SAAS;AAG9B,SAASC,YAAY,EAAEC,KAAK;AAE5B;AAgBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAMC,aAAuB,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EAAE;EAC/E,IAAAS,KAAA,GAAqCT,OAAO,aAAPA,OAAO,cAAPA,OAAO,GAAI,CAAC,CAAC;IAAAU,YAAA,GAAAD,KAAA,CAA1CvC,MAAM;IAAE9B,KAAK,GAAAsE,YAAA,cAAG,QAAQ,GAAAA,YAAA;EAChC,YAAA3C,MAAA,CACA3B,KAAK,4CAAA2B,MAAA,CACL3B,KAAK,sDAAA2B,MAAA,CACL3B,KAAK,uEAAA2B,MAAA,CACL3B,KAAK,YAAA2B,MAAA,CACL3B,KAAK,kDAAA2B,MAAA,CACL3B,KAAK,SAAA2B,MAAA,CACL3B,KAAK,oDAAA2B,MAAA,CACL3B,KAAK,4CAAA2B,MAAA,CACL3B,KAAK,gDAAA2B,MAAA,CACL3B,KAAK,gDAAA2B,MAAA,CAEL3B,KAAK,qBAAA2B,MAAA,CACL3B,KAAK,8BAAA2B,MAAA,CAA2BuC,WAAW,UAAAvC,MAAA,CAC3C3B,KAAK,gBAAA2B,MAAA,CAAauC,WAAW,cAAAvC,MAAA,CAC7B3B,KAAK,sBAAA2B,MAAA,CACL3B,KAAK,kBAAA2B,MAAA,CACL3B,KAAK,oBAAA2B,MAAA,CACL3B,KAAK,kBAAA2B,MAAA,CACL3B,KAAK,YAAA2B,MAAA,CACL3B,KAAK;AAEP;AAEA,eAAeC,aAAa"}
|
package/es/utils.d.ts
ADDED
package/es/utils.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function detectIndent(html, entryScript) {
|
|
2
|
+
var baseIndent = '';
|
|
3
|
+
if (entryScript !== null && entryScript !== void 0 && entryScript.sourceCodeLocation) {
|
|
4
|
+
var newline = html.includes('\r\n') ? '\r\n' : '\n';
|
|
5
|
+
var lines = html.split(newline);
|
|
6
|
+
var lineStr = lines[entryScript.sourceCodeLocation.startLine - 1];
|
|
7
|
+
if (lineStr) {
|
|
8
|
+
var startTag = '<script';
|
|
9
|
+
var tagNameFromHtml = lineStr.substring(entryScript.sourceCodeLocation.startCol - 1, entryScript.sourceCodeLocation.startCol - 1 + startTag.length);
|
|
10
|
+
if (tagNameFromHtml === startTag) {
|
|
11
|
+
for (var i = 0; i < entryScript.sourceCodeLocation.startCol - 1; i++) {
|
|
12
|
+
if (/\s/.test(lineStr[i])) {
|
|
13
|
+
baseIndent += lineStr[i];
|
|
14
|
+
} else {
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return baseIndent;
|
|
22
|
+
}
|
|
23
|
+
export function space(num) {
|
|
24
|
+
return new Array(num + 1).join(' ');
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=utils.js.map
|
package/es/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["detectIndent","html","entryScript","baseIndent","sourceCodeLocation","newline","includes","lines","split","lineStr","startLine","startTag","tagNameFromHtml","substring","startCol","length","i","test","space","num","Array","join"],"sources":["../src/utils.ts"],"sourcesContent":["import type { Element } from 'domhandler';\n\nexport function detectIndent(html: string, entryScript: Element | undefined) {\n let baseIndent = '';\n if (entryScript?.sourceCodeLocation) {\n const newline = html.includes('\\r\\n') ? '\\r\\n' : '\\n';\n const lines = html.split(newline);\n const lineStr = lines[entryScript.sourceCodeLocation.startLine - 1];\n if (lineStr) {\n const startTag = '<script';\n const tagNameFromHtml = lineStr.substring(\n entryScript.sourceCodeLocation.startCol - 1,\n entryScript.sourceCodeLocation.startCol - 1 + startTag.length,\n );\n if (tagNameFromHtml === startTag) {\n for (let i = 0; i < entryScript.sourceCodeLocation.startCol - 1; i++) {\n if (/\\s/.test(lineStr[i])) {\n baseIndent += lineStr[i];\n } else {\n break;\n }\n }\n }\n }\n }\n return baseIndent;\n}\n\nexport function space(num: number) {\n return new Array(num + 1).join(' ');\n}\n"],"mappings":"AAEA,OAAO,SAASA,YAAYA,CAACC,IAAY,EAAEC,WAAgC,EAAE;EAC3E,IAAIC,UAAU,GAAG,EAAE;EACnB,IAAID,WAAW,aAAXA,WAAW,eAAXA,WAAW,CAAEE,kBAAkB,EAAE;IACnC,IAAMC,OAAO,GAAGJ,IAAI,CAACK,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,IAAI;IACrD,IAAMC,KAAK,GAAGN,IAAI,CAACO,KAAK,CAACH,OAAO,CAAC;IACjC,IAAMI,OAAO,GAAGF,KAAK,CAACL,WAAW,CAACE,kBAAkB,CAACM,SAAS,GAAG,CAAC,CAAC;IACnE,IAAID,OAAO,EAAE;MACX,IAAME,QAAQ,GAAG,SAAS;MAC1B,IAAMC,eAAe,GAAGH,OAAO,CAACI,SAAS,CACvCX,WAAW,CAACE,kBAAkB,CAACU,QAAQ,GAAG,CAAC,EAC3CZ,WAAW,CAACE,kBAAkB,CAACU,QAAQ,GAAG,CAAC,GAAGH,QAAQ,CAACI,MACzD,CAAC;MACD,IAAIH,eAAe,KAAKD,QAAQ,EAAE;QAChC,KAAK,IAAIK,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGd,WAAW,CAACE,kBAAkB,CAACU,QAAQ,GAAG,CAAC,EAAEE,CAAC,EAAE,EAAE;UACpE,IAAI,IAAI,CAACC,IAAI,CAACR,OAAO,CAACO,CAAC,CAAC,CAAC,EAAE;YACzBb,UAAU,IAAIM,OAAO,CAACO,CAAC,CAAC;UAC1B,CAAC,MAAM;YACL;UACF;QACF;MACF;IACF;EACF;EACA,OAAOb,UAAU;AACnB;AAEA,OAAO,SAASe,KAAKA,CAACC,GAAW,EAAE;EACjC,OAAO,IAAIC,KAAK,CAACD,GAAG,GAAG,CAAC,CAAC,CAACE,IAAI,CAAC,GAAG,CAAC;AACrC"}
|
package/lib/helper.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface QiankunProps {
|
|
2
|
+
container?: HTMLElement;
|
|
3
|
+
[x: string]: unknown;
|
|
4
|
+
}
|
|
5
|
+
export interface QiankunLifeCycle {
|
|
6
|
+
bootstrap: (props?: QiankunProps) => void | Promise<void>;
|
|
7
|
+
mount: (props: QiankunProps) => void | Promise<void>;
|
|
8
|
+
unmount: (props: QiankunProps) => void | Promise<void>;
|
|
9
|
+
update: (props: QiankunProps) => void | Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export interface QiankunWindow {
|
|
12
|
+
__POWERED_BY_QIANKUN__?: boolean;
|
|
13
|
+
[x: string]: any;
|
|
14
|
+
}
|
|
15
|
+
export declare const qiankunWindow: QiankunWindow;
|
|
16
|
+
export declare const renderWithQiankun: (qiankunLifeCycle: QiankunLifeCycle) => void;
|
|
17
|
+
export default renderWithQiankun;
|
package/lib/helper.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/helper.ts
|
|
20
|
+
var helper_exports = {};
|
|
21
|
+
__export(helper_exports, {
|
|
22
|
+
default: () => helper_default,
|
|
23
|
+
qiankunWindow: () => qiankunWindow,
|
|
24
|
+
renderWithQiankun: () => renderWithQiankun
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(helper_exports);
|
|
27
|
+
var qiankunWindow = typeof window !== "undefined" ? window.proxy || window : {};
|
|
28
|
+
var renderWithQiankun = (qiankunLifeCycle) => {
|
|
29
|
+
if (qiankunWindow == null ? void 0 : qiankunWindow.__POWERED_BY_QIANKUN__) {
|
|
30
|
+
if (!window.moudleQiankunAppLifeCycles) {
|
|
31
|
+
window.moudleQiankunAppLifeCycles = {};
|
|
32
|
+
}
|
|
33
|
+
if (qiankunWindow.qiankunName) {
|
|
34
|
+
window.moudleQiankunAppLifeCycles[qiankunWindow.qiankunName] = qiankunLifeCycle;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var helper_default = renderWithQiankun;
|
|
39
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
40
|
+
0 && (module.exports = {
|
|
41
|
+
qiankunWindow,
|
|
42
|
+
renderWithQiankun
|
|
43
|
+
});
|
|
44
|
+
//# sourceMappingURL=helper.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/helper.ts"],
|
|
4
|
+
"sourcesContent": ["export interface QiankunProps {\n container?: HTMLElement;\n [x: string]: unknown;\n}\n\nexport interface QiankunLifeCycle {\n bootstrap: (props?: QiankunProps) => void | Promise<void>;\n mount: (props: QiankunProps) => void | Promise<void>;\n unmount: (props: QiankunProps) => void | Promise<void>;\n update: (props: QiankunProps) => void | Promise<void>;\n}\n\nexport interface QiankunWindow {\n __POWERED_BY_QIANKUN__?: boolean;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [x: string]: any;\n}\n\nexport const qiankunWindow: QiankunWindow = typeof window !== 'undefined' ? window.proxy || window : {};\n\nexport const renderWithQiankun = (qiankunLifeCycle: QiankunLifeCycle) => {\n // 函数只有一次执行机会,需要把生命周期赋值给全局\n if (qiankunWindow?.__POWERED_BY_QIANKUN__) {\n if (!window.moudleQiankunAppLifeCycles) {\n window.moudleQiankunAppLifeCycles = {};\n }\n if (qiankunWindow.qiankunName) {\n window.moudleQiankunAppLifeCycles[qiankunWindow.qiankunName] = qiankunLifeCycle;\n }\n }\n};\n\nexport default renderWithQiankun;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBO,IAAM,gBAA+B,OAAO,WAAW,cAAc,OAAO,SAAS,SAAS,CAAC;AAE/F,IAAM,oBAAoB,CAAC,qBAAuC;AAEvE,MAAI,+CAAe,wBAAwB;AACzC,QAAI,CAAC,OAAO,4BAA4B;AACtC,aAAO,6BAA6B,CAAC;AAAA,IACvC;AACA,QAAI,cAAc,aAAa;AAC7B,aAAO,2BAA2B,cAAc,WAAW,IAAI;AAAA,IACjE;AAAA,EACF;AACF;AAEA,IAAO,iBAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PluginOption } from 'vite';
|
|
2
|
+
export * from './helper';
|
|
3
|
+
export interface MicroOption {
|
|
4
|
+
/**
|
|
5
|
+
* Whether to change the origin of entry script tag for micro frontend. It's useful when the micro
|
|
6
|
+
* frontend is deployed on a different domain or path.
|
|
7
|
+
*
|
|
8
|
+
* > If the origin by the qiankun's default algorithm is not correct for you, please try the
|
|
9
|
+
* > [getPublicPath](https://qiankun.umijs.org/zh/api) option.
|
|
10
|
+
*
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
changeScriptOrigin?: boolean;
|
|
14
|
+
}
|
|
15
|
+
type PluginFn = (appName: string, pluginOptions?: MicroOption) => PluginOption;
|
|
16
|
+
/**
|
|
17
|
+
* Vite plugin for integrating with Qiankun micro frontend.
|
|
18
|
+
*
|
|
19
|
+
* @param appName The name of the Qiankun sub app.
|
|
20
|
+
* @param pluginOptions Options for configuring the micro frontend behavior.
|
|
21
|
+
*
|
|
22
|
+
* > If the micro app is deployed on a different domain or path, you may need to adjust the public
|
|
23
|
+
* > path. Please have a look at the [getPublicPath](https://qiankun.umijs.org/zh/api) option.
|
|
24
|
+
*
|
|
25
|
+
* @returns A Vite plugin option object.
|
|
26
|
+
*/
|
|
27
|
+
declare const qiankunPlugin: PluginFn;
|
|
28
|
+
export default qiankunPlugin;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
default: () => src_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var import_cheerio = require("cheerio");
|
|
27
|
+
var import_utils = require("./utils");
|
|
28
|
+
__reExport(src_exports, require("./helper"), module.exports);
|
|
29
|
+
var qiankunPlugin = (appName, pluginOptions = {}) => {
|
|
30
|
+
const { changeScriptOrigin = true } = pluginOptions;
|
|
31
|
+
return {
|
|
32
|
+
name: "vite-plugin-qiankun",
|
|
33
|
+
transformIndexHtml(html, context) {
|
|
34
|
+
var _a;
|
|
35
|
+
const $ = (0, import_cheerio.load)(html, { sourceCodeLocationInfo: true });
|
|
36
|
+
const entryScript = $("script[entry]").get(0) ?? $('body script[type=module], head script[crossorigin=""]').get(0);
|
|
37
|
+
if (entryScript) {
|
|
38
|
+
const scriptBaseIndent = (0, import_utils.detectIndent)(html, entryScript);
|
|
39
|
+
const S0 = scriptBaseIndent;
|
|
40
|
+
const S1 = scriptBaseIndent + (0, import_utils.space)(2);
|
|
41
|
+
const S2 = S1 + (0, import_utils.space)(2);
|
|
42
|
+
const script$ = module2DynamicImport({
|
|
43
|
+
$,
|
|
44
|
+
scriptTag: entryScript,
|
|
45
|
+
changeScriptOrigin,
|
|
46
|
+
ident: S1
|
|
47
|
+
});
|
|
48
|
+
script$ == null ? void 0 : script$.html(`${(_a = script$.html()) == null ? void 0 : _a.trimEnd()}.finally(() => {
|
|
49
|
+
${S2}${createImportFinallyResolve(appName, { indent: S2 }).trim()}
|
|
50
|
+
${S1}});
|
|
51
|
+
${S0}`);
|
|
52
|
+
const bodyBaseIndent = (0, import_utils.detectIndent)(html, $("body").get(0));
|
|
53
|
+
const B1 = bodyBaseIndent + (0, import_utils.space)(2);
|
|
54
|
+
const B2 = B1 + (0, import_utils.space)(2);
|
|
55
|
+
$("body").append(`
|
|
56
|
+
${B1}<script>
|
|
57
|
+
${B2}${createQiankunHelper(appName, { indent: B2 + (0, import_utils.space)(2) }).trim()}
|
|
58
|
+
${B1}</script>
|
|
59
|
+
`);
|
|
60
|
+
const output = $.html();
|
|
61
|
+
return output;
|
|
62
|
+
} else {
|
|
63
|
+
console.warn("\x1B[33m%s\x1B[0m", "⚠️ Patch for qiankun failed, because the entry script was not found.");
|
|
64
|
+
return html;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
configureServer(server) {
|
|
68
|
+
const base = server.config.base;
|
|
69
|
+
return () => {
|
|
70
|
+
const patchFiles = [`${base}@vite/client`];
|
|
71
|
+
patchFiles.forEach((file) => {
|
|
72
|
+
server.middlewares.use(file, (req, res, next) => {
|
|
73
|
+
const end = res.end.bind(res);
|
|
74
|
+
res.end = function(chunk, ...rest) {
|
|
75
|
+
if (typeof chunk === "string") {
|
|
76
|
+
const $ = (0, import_cheerio.load)(chunk);
|
|
77
|
+
module2DynamicImport({ $, scriptTag: $(`script[src="${file}"]`).get(0), changeScriptOrigin });
|
|
78
|
+
chunk = $.html();
|
|
79
|
+
}
|
|
80
|
+
end(chunk, ...rest);
|
|
81
|
+
return this;
|
|
82
|
+
};
|
|
83
|
+
next();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
function module2DynamicImport(options) {
|
|
91
|
+
const { $, scriptTag, changeScriptOrigin, ident = "" } = options;
|
|
92
|
+
if (!scriptTag) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const script$ = $(scriptTag);
|
|
96
|
+
const moduleSrc = script$.attr("src");
|
|
97
|
+
let appendBase = "''";
|
|
98
|
+
if (changeScriptOrigin) {
|
|
99
|
+
appendBase = "window.proxy ? window.proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || '' : ''";
|
|
100
|
+
}
|
|
101
|
+
script$.removeAttr("src");
|
|
102
|
+
script$.removeAttr("type");
|
|
103
|
+
const space2 = ident;
|
|
104
|
+
script$.html(`
|
|
105
|
+
${space2}const publicUrl = ${appendBase};
|
|
106
|
+
${space2}let assetUrl = '${moduleSrc}';
|
|
107
|
+
${space2}if (!assetUrl.match(/^https?/i)) {
|
|
108
|
+
${space2} assetUrl = publicUrl + assetUrl;
|
|
109
|
+
${space2}}
|
|
110
|
+
${space2}import(assetUrl)`);
|
|
111
|
+
return script$;
|
|
112
|
+
}
|
|
113
|
+
function createImportFinallyResolve(qiankunName, options) {
|
|
114
|
+
const { indent: space2 = " " } = options ?? {};
|
|
115
|
+
return `
|
|
116
|
+
${space2}const qiankunLifeCycle = window.moudleQiankunAppLifeCycles && window.moudleQiankunAppLifeCycles['${qiankunName}'];
|
|
117
|
+
${space2}if (qiankunLifeCycle) {
|
|
118
|
+
${space2} window.proxy.vitemount((props) => qiankunLifeCycle.mount(props));
|
|
119
|
+
${space2} window.proxy.viteunmount((props) => qiankunLifeCycle.unmount(props));
|
|
120
|
+
${space2} window.proxy.vitebootstrap(() => qiankunLifeCycle.bootstrap());
|
|
121
|
+
${space2} window.proxy.viteupdate((props) => qiankunLifeCycle.update(props));
|
|
122
|
+
${space2}}
|
|
123
|
+
`;
|
|
124
|
+
}
|
|
125
|
+
function createQiankunHelper(qiankunName, options) {
|
|
126
|
+
const { indent: space2 = " " } = options ?? {};
|
|
127
|
+
return `
|
|
128
|
+
${space2}const createDeffer = (hookName) => {
|
|
129
|
+
${space2} const d = new Promise((resolve, reject) => {
|
|
130
|
+
${space2} window.proxy && (window.proxy[\`vite\${hookName}\`] = resolve)
|
|
131
|
+
${space2} })
|
|
132
|
+
${space2} return props => d.then(fn => fn(props));
|
|
133
|
+
${space2}}
|
|
134
|
+
${space2}const bootstrap = createDeffer('bootstrap');
|
|
135
|
+
${space2}const mount = createDeffer('mount');
|
|
136
|
+
${space2}const unmount = createDeffer('unmount');
|
|
137
|
+
${space2}const update = createDeffer('update');
|
|
138
|
+
|
|
139
|
+
${space2};(global => {
|
|
140
|
+
${space2} global.qiankunName = '${qiankunName}';
|
|
141
|
+
${space2} global['${qiankunName}'] = {
|
|
142
|
+
${space2} bootstrap,
|
|
143
|
+
${space2} mount,
|
|
144
|
+
${space2} unmount,
|
|
145
|
+
${space2} update
|
|
146
|
+
${space2} };
|
|
147
|
+
${space2}})(window);
|
|
148
|
+
`;
|
|
149
|
+
}
|
|
150
|
+
var src_default = qiankunPlugin;
|
|
151
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
152
|
+
0 && (module.exports = {
|
|
153
|
+
...require("./helper")
|
|
154
|
+
});
|
|
155
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["import type { CheerioAPI } from 'cheerio';\nimport { load } from 'cheerio';\nimport type { Element } from 'domhandler';\nimport type { PluginOption } from 'vite';\nimport { detectIndent, space } from './utils';\n\nexport * from './helper';\n\nexport interface MicroOption {\n /**\n * Whether to change the origin of entry script tag for micro frontend. It's useful when the micro\n * frontend is deployed on a different domain or path.\n *\n * > If the origin by the qiankun's default algorithm is not correct for you, please try the\n * > [getPublicPath](https://qiankun.umijs.org/zh/api) option.\n *\n * @default true\n */\n changeScriptOrigin?: boolean;\n}\ntype PluginFn = (appName: string, pluginOptions?: MicroOption) => PluginOption;\n\n/**\n * Vite plugin for integrating with Qiankun micro frontend.\n *\n * @param appName The name of the Qiankun sub app.\n * @param pluginOptions Options for configuring the micro frontend behavior.\n *\n * > If the micro app is deployed on a different domain or path, you may need to adjust the public\n * > path. Please have a look at the [getPublicPath](https://qiankun.umijs.org/zh/api) option.\n *\n * @returns A Vite plugin option object.\n */\nconst qiankunPlugin: PluginFn = (appName, pluginOptions = {}) => {\n const { changeScriptOrigin = true } = pluginOptions;\n\n return {\n name: 'vite-plugin-qiankun',\n\n transformIndexHtml(html: string, context) {\n const $ = load(html, { sourceCodeLocationInfo: true });\n const entryScript =\n $('script[entry]').get(0) ?? $('body script[type=module], head script[crossorigin=\"\"]').get(0);\n if (entryScript) {\n const scriptBaseIndent = detectIndent(html, entryScript);\n const S0 = scriptBaseIndent;\n const S1 = scriptBaseIndent + space(2);\n const S2 = S1 + space(2);\n const script$ = module2DynamicImport({\n $,\n scriptTag: entryScript,\n changeScriptOrigin,\n ident: S1,\n });\n script$?.html(`${script$.html()?.trimEnd()}.finally(() => {\n${S2}${createImportFinallyResolve(appName, { indent: S2 }).trim()}\n${S1}});\n${S0}`);\n\n const bodyBaseIndent = detectIndent(html, $('body').get(0));\n const B1 = bodyBaseIndent + space(2);\n const B2 = B1 + space(2);\n $('body').append(`\n${B1}<script>\n${B2}${createQiankunHelper(appName, { indent: B2 + space(2) }).trim()}\n${B1}</script>\n`);\n const output = $.html();\n return output;\n } else {\n console.warn('\\x1b[33m%s\\x1b[0m', '⚠️ Patch for qiankun failed, because the entry script was not found.');\n return html;\n }\n },\n configureServer(server) {\n const base = server.config.base;\n return () => {\n const patchFiles = [`${base}@vite/client`];\n patchFiles.forEach((file) => {\n server.middlewares.use(file, (req, res, next) => {\n const end = res.end.bind(res);\n res.end = function (this: typeof res, chunk: unknown, ...rest: any[]) {\n if (typeof chunk === 'string') {\n const $ = load(chunk);\n module2DynamicImport({ $, scriptTag: $(`script[src=\"${file}\"]`).get(0), changeScriptOrigin });\n chunk = $.html();\n }\n end(chunk, ...rest);\n return this;\n } as unknown as typeof res.end;\n next();\n });\n });\n };\n },\n };\n};\n\nfunction module2DynamicImport(\n options: { $: CheerioAPI; scriptTag: Element | undefined; ident?: string } & Pick<MicroOption, 'changeScriptOrigin'>,\n) {\n const { $, scriptTag, changeScriptOrigin, ident = '' } = options;\n if (!scriptTag) {\n return;\n }\n const script$ = $(scriptTag);\n const moduleSrc = script$.attr('src');\n let appendBase = \"''\";\n if (changeScriptOrigin) {\n appendBase = \"window.proxy ? window.proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || '' : ''\";\n }\n script$.removeAttr('src');\n script$.removeAttr('type');\n const space = ident;\n script$.html(`\n${space}const publicUrl = ${appendBase};\n${space}let assetUrl = '${moduleSrc}';\n${space}if (!assetUrl.match(/^https?/i)) {\n${space} assetUrl = publicUrl + assetUrl;\n${space}}\n${space}import(assetUrl)`);\n return script$;\n}\n\nfunction createImportFinallyResolve(qiankunName: string, options?: { indent?: string }) {\n const { indent: space = ' ' } = options ?? {};\n return `\n${space}const qiankunLifeCycle = window.moudleQiankunAppLifeCycles && window.moudleQiankunAppLifeCycles['${qiankunName}'];\n${space}if (qiankunLifeCycle) {\n${space} window.proxy.vitemount((props) => qiankunLifeCycle.mount(props));\n${space} window.proxy.viteunmount((props) => qiankunLifeCycle.unmount(props));\n${space} window.proxy.vitebootstrap(() => qiankunLifeCycle.bootstrap());\n${space} window.proxy.viteupdate((props) => qiankunLifeCycle.update(props));\n${space}}\n`;\n}\n\nfunction createQiankunHelper(qiankunName: string, options?: { indent?: string }) {\n const { indent: space = ' ' } = options ?? {};\n return `\n${space}const createDeffer = (hookName) => {\n${space} const d = new Promise((resolve, reject) => {\n${space} window.proxy && (window.proxy[\\`vite\\${hookName}\\`] = resolve)\n${space} })\n${space} return props => d.then(fn => fn(props));\n${space}}\n${space}const bootstrap = createDeffer('bootstrap');\n${space}const mount = createDeffer('mount');\n${space}const unmount = createDeffer('unmount');\n${space}const update = createDeffer('update');\n\n${space};(global => {\n${space} global.qiankunName = '${qiankunName}';\n${space} global['${qiankunName}'] = {\n${space} bootstrap,\n${space} mount,\n${space} unmount,\n${space} update\n${space} };\n${space}})(window);\n`;\n}\n\nexport default qiankunPlugin;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,qBAAqB;AAGrB,mBAAoC;AAEpC,wBAAc,qBANd;AAiCA,IAAM,gBAA0B,CAAC,SAAS,gBAAgB,CAAC,MAAM;AAC/D,QAAM,EAAE,qBAAqB,KAAK,IAAI;AAEtC,SAAO;AAAA,IACL,MAAM;AAAA,IAEN,mBAAmB,MAAc,SAAS;AAvC9C;AAwCM,YAAM,QAAI,qBAAK,MAAM,EAAE,wBAAwB,KAAK,CAAC;AACrD,YAAM,cACJ,EAAE,eAAe,EAAE,IAAI,CAAC,KAAK,EAAE,uDAAuD,EAAE,IAAI,CAAC;AAC/F,UAAI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AEF;AAEA,IAAO,cAAQ;",
|
|
6
|
+
"names": ["space"]
|
|
7
|
+
}
|
package/lib/utils.d.ts
ADDED
package/lib/utils.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/utils.ts
|
|
20
|
+
var utils_exports = {};
|
|
21
|
+
__export(utils_exports, {
|
|
22
|
+
detectIndent: () => detectIndent,
|
|
23
|
+
space: () => space
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(utils_exports);
|
|
26
|
+
function detectIndent(html, entryScript) {
|
|
27
|
+
let baseIndent = "";
|
|
28
|
+
if (entryScript == null ? void 0 : entryScript.sourceCodeLocation) {
|
|
29
|
+
const newline = html.includes("\r\n") ? "\r\n" : "\n";
|
|
30
|
+
const lines = html.split(newline);
|
|
31
|
+
const lineStr = lines[entryScript.sourceCodeLocation.startLine - 1];
|
|
32
|
+
if (lineStr) {
|
|
33
|
+
const startTag = "<script";
|
|
34
|
+
const tagNameFromHtml = lineStr.substring(
|
|
35
|
+
entryScript.sourceCodeLocation.startCol - 1,
|
|
36
|
+
entryScript.sourceCodeLocation.startCol - 1 + startTag.length
|
|
37
|
+
);
|
|
38
|
+
if (tagNameFromHtml === startTag) {
|
|
39
|
+
for (let i = 0; i < entryScript.sourceCodeLocation.startCol - 1; i++) {
|
|
40
|
+
if (/\s/.test(lineStr[i])) {
|
|
41
|
+
baseIndent += lineStr[i];
|
|
42
|
+
} else {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return baseIndent;
|
|
50
|
+
}
|
|
51
|
+
function space(num) {
|
|
52
|
+
return new Array(num + 1).join(" ");
|
|
53
|
+
}
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
detectIndent,
|
|
57
|
+
space
|
|
58
|
+
});
|
|
59
|
+
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/utils.ts"],
|
|
4
|
+
"sourcesContent": ["import type { Element } from 'domhandler';\n\nexport function detectIndent(html: string, entryScript: Element | undefined) {\n let baseIndent = '';\n if (entryScript?.sourceCodeLocation) {\n const newline = html.includes('\\r\\n') ? '\\r\\n' : '\\n';\n const lines = html.split(newline);\n const lineStr = lines[entryScript.sourceCodeLocation.startLine - 1];\n if (lineStr) {\n const startTag = '<script';\n const tagNameFromHtml = lineStr.substring(\n entryScript.sourceCodeLocation.startCol - 1,\n entryScript.sourceCodeLocation.startCol - 1 + startTag.length,\n );\n if (tagNameFromHtml === startTag) {\n for (let i = 0; i < entryScript.sourceCodeLocation.startCol - 1; i++) {\n if (/\\s/.test(lineStr[i])) {\n baseIndent += lineStr[i];\n } else {\n break;\n }\n }\n }\n }\n }\n return baseIndent;\n}\n\nexport function space(num: number) {\n return new Array(num + 1).join(' ');\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,SAAS,aAAa,MAAc,aAAkC;AAC3E,MAAI,aAAa;AACjB,MAAI,2CAAa,oBAAoB;AACnC,UAAM,UAAU,KAAK,SAAS,MAAM,IAAI,SAAS;AACjD,UAAM,QAAQ,KAAK,MAAM,OAAO;AAChC,UAAM,UAAU,MAAM,YAAY,mBAAmB,YAAY,CAAC;AAClE,QAAI,SAAS;AACX,YAAM,WAAW;AACjB,YAAM,kBAAkB,QAAQ;AAAA,QAC9B,YAAY,mBAAmB,WAAW;AAAA,QAC1C,YAAY,mBAAmB,WAAW,IAAI,SAAS;AAAA,MACzD;AACA,UAAI,oBAAoB,UAAU;AAChC,iBAAS,IAAI,GAAG,IAAI,YAAY,mBAAmB,WAAW,GAAG,KAAK;AACpE,cAAI,KAAK,KAAK,QAAQ,CAAC,CAAC,GAAG;AACzB,0BAAc,QAAQ,CAAC;AAAA,UACzB,OAAO;AACL;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,MAAM,KAAa;AACjC,SAAO,IAAI,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG;AACpC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tiny-codes/vite-plugin-qiankun",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "vite for qiankun. This is a forked version of vite-plugin-qiankun.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"qiankun",
|
|
7
|
+
"vite",
|
|
8
|
+
"esm",
|
|
9
|
+
"esModule"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/shijistar/vite-plugin-qiankun",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/shijistar/vite-plugin-qiankun/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/shijistar/vite-plugin-qiankun.git"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "李凤宝(Leo) <shijistar@gmail.com>",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"main": "lib/index.js",
|
|
23
|
+
"module": "es/index.js",
|
|
24
|
+
"types": "lib/index.d.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"lib",
|
|
27
|
+
"es",
|
|
28
|
+
"LICENSE",
|
|
29
|
+
"CHANGELOG.md",
|
|
30
|
+
"README.md",
|
|
31
|
+
"README.zh-CN.md"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "run-s build-task",
|
|
35
|
+
"build-task": "father build",
|
|
36
|
+
"example:install": "cd example/main && npm install && cd ../viteapp && npm install && cd ../vue && npm install && cd ../react18 && npm install && cd ../vue3sub && npm install",
|
|
37
|
+
"example:start": "npm run build && cd example/viteapp && npm run build && cd ../../ && npm-run-all --parallel start:main start:viteapp start:vue start:react18dev start:vue3sub",
|
|
38
|
+
"example:start-vite-dev": "npm run build && npm-run-all --parallel start:main start:vitedev start:vue start:react18dev start:vue3sub",
|
|
39
|
+
"init-husky": "husky",
|
|
40
|
+
"prepare": "run-s init-husky",
|
|
41
|
+
"prepublishOnly": "run-s build",
|
|
42
|
+
"start:main": "cd example/main && npm run start",
|
|
43
|
+
"start:react18dev": "cd example/react18 && npm run dev",
|
|
44
|
+
"start:viteapp": "cd example/viteapp && npm run start",
|
|
45
|
+
"start:vitedev": "cd example/viteapp && npm run dev",
|
|
46
|
+
"start:vue": "cd example/vue && npm run start",
|
|
47
|
+
"start:vue3sub": "cd example/vue3sub && npm run dev",
|
|
48
|
+
"vite:test": "npm run build && cd example/viteapp && npm run dev"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"cheerio": "^1.0.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@tiny-codes/code-style-all-in-one": "^2.0.0",
|
|
55
|
+
"@types/node": "^22.19.15",
|
|
56
|
+
"father": "^4.6.17",
|
|
57
|
+
"npm-run-all2": "^8.0.4",
|
|
58
|
+
"qiankun": "^2.10.16",
|
|
59
|
+
"tsx": "^4.21.0",
|
|
60
|
+
"typescript": "^5.9.3",
|
|
61
|
+
"vite": "^2.9.9"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"vite": "^2.0.0 | ^3.0.0 | ^4.0.0 | ^5.0.0 | ^6.0.0 | ^7.0.0 | ^8.0.0"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"registry": "https://registry.npmjs.org/"
|
|
68
|
+
}
|
|
69
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#### 简介
|
|
2
|
+
|
|
3
|
+
forked from [vite-plugin-qiankun](https://github.com/tengmaoqing/vite-plugin-qiankun)
|
|
4
|
+
|
|
5
|
+
> vite-plugin-qiankun: 帮助应用快速接入乾坤的vite插件
|
|
6
|
+
|
|
7
|
+
- 保留vite构建es模块的优势
|
|
8
|
+
- 一键配置,不影响已有的vite配置
|
|
9
|
+
- 支持vite开发环境
|
|
10
|
+
|
|
11
|
+
#### 快速开始
|
|
12
|
+
|
|
13
|
+
###### 1、在 `vite.config.ts` 中安装插件
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// vite.config.ts
|
|
17
|
+
import qiankun from 'vite-plugin-qiankun';
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
// 这里的 'myMicroAppName' 是子应用名,主应用注册时AppName需保持一致
|
|
21
|
+
plugins: [qiankun('myMicroAppName')],
|
|
22
|
+
// 生产环境需要指定运行域名作为base
|
|
23
|
+
base: 'http://xxx.com/',
|
|
24
|
+
};
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
###### 2、在入口文件里面写入乾坤的生命周期配置
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// main.ts
|
|
31
|
+
import { qiankunWindow, renderWithQiankun } from 'vite-plugin-qiankun';
|
|
32
|
+
|
|
33
|
+
// some code
|
|
34
|
+
renderWithQiankun({
|
|
35
|
+
mount(props) {
|
|
36
|
+
console.log('mount');
|
|
37
|
+
render(props);
|
|
38
|
+
},
|
|
39
|
+
bootstrap() {
|
|
40
|
+
console.log('bootstrap');
|
|
41
|
+
},
|
|
42
|
+
unmount(props: any) {
|
|
43
|
+
console.log('unmount');
|
|
44
|
+
const { container } = props;
|
|
45
|
+
const mountRoot = container?.querySelector('#root');
|
|
46
|
+
ReactDOM.unmountComponentAtNode(mountRoot || document.querySelector('#root'));
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
|
|
51
|
+
render({});
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
###### 3、dev下作为子应用调试
|
|
56
|
+
|
|
57
|
+
> 因为开发环境作为子应用时与热更新插件(可能与其他修改html的插件也会存在冲突)有冲突,所以需要额外的调试配置
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// useDevMode 开启时与热更新插件冲突,使用变量切换
|
|
61
|
+
const useDevMode = true;
|
|
62
|
+
|
|
63
|
+
const baseConfig: UserConfig = {
|
|
64
|
+
plugins: [
|
|
65
|
+
...(useDevMode ? [] : [reactRefresh()]),
|
|
66
|
+
qiankun('viteapp', {
|
|
67
|
+
useDevMode,
|
|
68
|
+
}),
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
上面例子中 `useDevMode = true` 则不使用热更新插件,`useDevMode = false` 则能使用热更新,但无法作为子应用加载。
|
|
74
|
+
|
|
75
|
+
###### 4、子应用部署的域名不同
|
|
76
|
+
|
|
77
|
+
如果子应用与主应用部署的域名不同,并且子应用的`base`使用了相对路径,例如`/sub-app`,此时可能会导致子应用入口script加载失败。在子应用的页面里,入口script可能会是`/sub-app/index.js`,qiankun会尝试加载该路径,但由于当前域名是主应用,就会导致浏览器尝试从主应用的域名下去加载资源,肯定会失败。
|
|
78
|
+
|
|
79
|
+
要解决这个问题,可以使用 [getPublicPath](https://qiankun.umijs.org/zh/api) 选项,返回子应用的域名。
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
loadMicroApp(
|
|
83
|
+
{
|
|
84
|
+
name: 'sub-app',
|
|
85
|
+
entry: `https://www.sub-app.com/path/to/page`,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
getPublicPath: () => `https://www.sub-app.com`,
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
###### 5、其它使用注意点 `qiankunWindow`
|
|
94
|
+
|
|
95
|
+
因为es模块加载与`qiankun`的实现方式有些冲突,所以使用本插件实现的`qiankun`微应用里面没有运行在js沙盒中。所以在不可避免需要设置window上的属性时,尽量显示的操作js沙盒,否则可能会对其它子应用产生副作用。qiankun沙盒使用方式
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { qiankunWindow } from 'vite-plugin-qiankun';
|
|
99
|
+
|
|
100
|
+
qiankunWindow.customxxx = 'ssss';
|
|
101
|
+
|
|
102
|
+
if (qiankunWindow.__POWERED_BY_QIANKUN__) {
|
|
103
|
+
console.log('我正在作为子应用运行');
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### 例子
|
|
108
|
+
|
|
109
|
+
详细的信息可以参考例子里面的使用方式
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
git clone xx
|
|
113
|
+
npm install
|
|
114
|
+
npm run example:install
|
|
115
|
+
# 生产环境调试demo
|
|
116
|
+
npm run example:start
|
|
117
|
+
# vite开发环境demo, demo中热更新已经关闭
|
|
118
|
+
npm run example:start-vite-dev
|
|
119
|
+
```
|