@tiny-codes/vite-plugin-qiankun 2.0.0 → 2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @tiny-codes/vite-plugin-qiankun
2
2
 
3
+ ## v2.0.1
4
+
5
+ 2024-4-2
6
+
7
+ ### Features
8
+
9
+ - Support `development` mode by default without any configuration.
10
+
3
11
  ## v2.0.0
4
12
 
5
13
  2024-4-1
package/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { load } from 'cheerio';
2
- import { detectIndent, space } from "./utils";
2
+ import { detectIndent, normalizeUrl, space } from "./utils";
3
3
  export * from "./helper";
4
4
  /**
5
5
  * Vite plugin for integrating with Qiankun micro frontend.
@@ -23,9 +23,10 @@ var qiankunPlugin = function qiankunPlugin(appName) {
23
23
  var $ = load(html, {
24
24
  sourceCodeLocationInfo: true
25
25
  });
26
+ // Transform entry script
26
27
  var entryScript = (_$$get = $('script[entry]').get(0)) !== null && _$$get !== void 0 ? _$$get : $('body script[type=module], head script[crossorigin=""]').get(0);
27
28
  if (entryScript) {
28
- var _script$$html;
29
+ var _script$$html, _context$server;
29
30
  var scriptBaseIndent = detectIndent(html, entryScript);
30
31
  var S0 = scriptBaseIndent;
31
32
  var S1 = scriptBaseIndent + space(2);
@@ -39,12 +40,41 @@ var qiankunPlugin = function qiankunPlugin(appName) {
39
40
  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
41
  indent: S2
41
42
  }).trim(), "\n").concat(S1, "});\n").concat(S0));
43
+
44
+ // Transform @react-refresh script
45
+ if (((_context$server = context.server) === null || _context$server === void 0 ? void 0 : _context$server.config.command) === 'serve') {
46
+ var scripts = $('head script[type=module]').toArray();
47
+ var refreshScript = scripts.find(function (s) {
48
+ var _$$html;
49
+ return /@react-refresh";$/m.test((_$$html = $(s).html()) !== null && _$$html !== void 0 ? _$$html : '');
50
+ });
51
+ if (refreshScript) {
52
+ var refreshScript$ = $(refreshScript);
53
+ var R1 = detectIndent(html, refreshScript) + space(2);
54
+ var content = refreshScript$.html();
55
+ var regExp = /import\s*{\s*injectIntoGlobalHook\s*}\s*from\s*"([^"]*@react-refresh)";/m;
56
+ var match = content === null || content === void 0 ? void 0 : content.match(regExp);
57
+ if (content && match) {
58
+ var sentence = match[0];
59
+ var from = match[1];
60
+ var rest = content.replace(sentence, '');
61
+ refreshScript$.removeAttr('type');
62
+ refreshScript$.html("\n".concat(R1, "import(").concat(normalizeUrl(from, {
63
+ changeScriptOrigin: changeScriptOrigin
64
+ }), ").then(({ injectIntoGlobalHook }) => {\n").concat(R1, " ").concat(rest.split('\n').map(function (s) {
65
+ return s.trim();
66
+ }).filter(Boolean).join("\n".concat(R1, " ")), "\n").concat(R1, "});"));
67
+ }
68
+ }
69
+ }
70
+
71
+ // Add extra script to export lifecycles
42
72
  var bodyBaseIndent = detectIndent(html, $('body').get(0));
43
73
  var B1 = bodyBaseIndent + space(2);
44
74
  var B2 = B1 + space(2);
45
75
  $('body').append("\n".concat(B1, "<script>\n").concat(B2).concat(createQiankunHelper(appName, {
46
- indent: B2 + space(2)
47
- }).trim(), "\n").concat(B1, "</script>\n"));
76
+ indent: B2
77
+ }), "\n").concat(B1, "</script>\n"));
48
78
  var output = $.html();
49
79
  return output;
50
80
  } else {
@@ -55,28 +85,26 @@ var qiankunPlugin = function qiankunPlugin(appName) {
55
85
  configureServer: function configureServer(server) {
56
86
  var base = server.config.base;
57
87
  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
- });
88
+ // Only apply to / i.e. the mount point of the app
89
+ server.middlewares.use(function (req, res, next) {
90
+ var end = res.end.bind(res);
91
+ res.end = function (chunk) {
92
+ if (typeof chunk === 'string') {
93
+ var $ = load(chunk);
94
+ module2DynamicImport({
95
+ $: $,
96
+ scriptTag: $("script[src=\"".concat(base, "@vite/client\"]")).get(0),
97
+ changeScriptOrigin: changeScriptOrigin
98
+ });
99
+ chunk = $.html();
100
+ }
101
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
102
+ rest[_key - 1] = arguments[_key];
103
+ }
104
+ end.apply(void 0, [chunk].concat(rest));
105
+ return this;
106
+ };
107
+ next();
80
108
  });
81
109
  };
82
110
  }
@@ -93,14 +121,12 @@ function module2DynamicImport(options) {
93
121
  }
94
122
  var script$ = $(scriptTag);
95
123
  var moduleSrc = script$.attr('src');
96
- var appendBase = "''";
97
- if (changeScriptOrigin) {
98
- appendBase = "window.proxy ? window.proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || '' : ''";
99
- }
100
124
  script$.removeAttr('src');
101
125
  script$.removeAttr('type');
102
126
  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)"));
127
+ script$.html("\n".concat(space, "import(").concat(normalizeUrl(moduleSrc, {
128
+ changeScriptOrigin: changeScriptOrigin
129
+ }), ")"));
104
130
  return script$;
105
131
  }
106
132
  function createImportFinallyResolve(qiankunName, options) {
@@ -113,7 +139,7 @@ function createQiankunHelper(qiankunName, options) {
113
139
  var _ref2 = options !== null && options !== void 0 ? options : {},
114
140
  _ref2$indent = _ref2.indent,
115
141
  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");
142
+ 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);").trimStart();
117
143
  }
118
144
  export default qiankunPlugin;
119
145
  //# sourceMappingURL=index.js.map
package/es/index.js.map CHANGED
@@ -1 +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,GAAG,SAA1BA,aAAuBA,CAAIC,OAAO,EAAyB;EAAA,IAAvBC,aAAa,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAC1D,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,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"}
1
+ {"version":3,"names":["load","detectIndent","normalizeUrl","space","qiankunPlugin","appName","pluginOptions","arguments","length","undefined","_pluginOptions$change","changeScriptOrigin","name","transformIndexHtml","html","context","_$$get","$","sourceCodeLocationInfo","entryScript","get","_script$$html","_context$server","scriptBaseIndent","S0","S1","S2","script$","module2DynamicImport","scriptTag","ident","concat","trimEnd","createImportFinallyResolve","indent","trim","server","config","command","scripts","toArray","refreshScript","find","s","_$$html","test","refreshScript$","R1","content","regExp","match","sentence","from","rest","replace","removeAttr","split","map","filter","Boolean","join","bodyBaseIndent","B1","B2","append","createQiankunHelper","output","console","warn","configureServer","base","middlewares","use","req","res","next","end","bind","chunk","_len","Array","_key","apply","options","_options$ident","moduleSrc","attr","qiankunName","_ref","_ref$indent","_ref2","_ref2$indent","trimStart"],"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, normalizeUrl, 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 // Transform entry script\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 // Transform @react-refresh script\n if (context.server?.config.command === 'serve') {\n const scripts = $('head script[type=module]').toArray();\n const refreshScript = scripts.find((s) => /@react-refresh\";$/m.test($(s).html() ?? ''));\n if (refreshScript) {\n const refreshScript$ = $(refreshScript);\n const R1 = detectIndent(html, refreshScript) + space(2);\n const content = refreshScript$.html();\n const regExp = /import\\s*{\\s*injectIntoGlobalHook\\s*}\\s*from\\s*\"([^\"]*@react-refresh)\";/m;\n const match = content?.match(regExp);\n if (content && match) {\n const sentence = match[0];\n const from = match[1];\n const rest = content.replace(sentence, '');\n refreshScript$.removeAttr('type');\n refreshScript$.html(`\n${R1}import(${normalizeUrl(from, { changeScriptOrigin })}).then(({ injectIntoGlobalHook }) => {\n${R1} ${rest\n .split('\\n')\n .map((s) => s.trim())\n .filter(Boolean)\n .join(`\\n${R1} `)}\n${R1}});`);\n }\n }\n }\n\n // Add extra script to export lifecycles\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 })}\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 // Only apply to / i.e. the mount point of the app\n server.middlewares.use((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=\"${base}@vite/client\"]`).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\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 script$.removeAttr('src');\n script$.removeAttr('type');\n const space = ident;\n script$.html(`\n${space}import(${normalizeUrl(moduleSrc, { changeScriptOrigin })})`);\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);`.trimStart();\n}\n\nexport default qiankunPlugin;\n"],"mappings":"AACA,SAASA,IAAI,QAAQ,SAAS;AAG9B,SAASC,YAAY,EAAEC,YAAY,EAAEC,KAAK;AAE1C;AAgBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAMC,aAAuB,GAAG,SAA1BA,aAAuBA,CAAIC,OAAO,EAAyB;EAAA,IAAvBC,aAAa,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAC1D,IAAAG,qBAAA,GAAsCJ,aAAa,CAA3CK,kBAAkB;IAAlBA,kBAAkB,GAAAD,qBAAA,cAAG,I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B,MAAA,CACL5B,KAAK,YAAA4B,MAAA,CACL5B,KAAK,kDAAA4B,MAAA,CACL5B,KAAK,UAAA4B,MAAA,CACL5B,KAAK,oDAAA4B,MAAA,CACL5B,KAAK,4CAAA4B,MAAA,CACL5B,KAAK,gDAAA4B,MAAA,CACL5B,KAAK,gDAAA4B,MAAA,CAEL5B,KAAK,oBAAA4B,MAAA,CACL5B,KAAK,8BAAA4B,MAAA,CAA2BwD,WAAW,UAAAxD,MAAA,CAC3C5B,KAAK,gBAAA4B,MAAA,CAAawD,WAAW,cAAAxD,MAAA,CAC7B5B,KAAK,sBAAA4B,MAAA,CACL5B,KAAK,kBAAA4B,MAAA,CACL5B,KAAK,oBAAA4B,MAAA,CACL5B,KAAK,kBAAA4B,MAAA,CACL5B,KAAK,YAAA4B,MAAA,CACL5B,KAAK,iBAAcyF,SAAS,CAAC,CAAC;AAChC;AAEA,eAAexF,aAAa"}
package/es/utils.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import type { Element } from 'domhandler';
2
2
  export declare function detectIndent(html: string, entryScript: Element | undefined): string;
3
3
  export declare function space(num: number): string;
4
+ export declare function normalizeUrl(url: string | undefined, options?: {
5
+ changeScriptOrigin?: boolean;
6
+ }): string;
package/es/utils.js CHANGED
@@ -23,4 +23,14 @@ export function detectIndent(html, entryScript) {
23
23
  export function space(num) {
24
24
  return new Array(num + 1).join(' ');
25
25
  }
26
+ export function normalizeUrl(url, options) {
27
+ var _ref = options !== null && options !== void 0 ? options : {},
28
+ _ref$changeScriptOrig = _ref.changeScriptOrigin,
29
+ changeScriptOrigin = _ref$changeScriptOrig === void 0 ? true : _ref$changeScriptOrig;
30
+ var appendBase = "''";
31
+ if (changeScriptOrigin) {
32
+ appendBase = "window.proxy?.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || ''";
33
+ }
34
+ return url !== null && url !== void 0 && url.match(/^https?/i) ? "'".concat(url, "'") : "(".concat(appendBase, ") + '").concat(url, "'");
35
+ }
26
36
  //# sourceMappingURL=utils.js.map
package/es/utils.js.map CHANGED
@@ -1 +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"}
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","normalizeUrl","url","options","_ref","_ref$changeScriptOrig","changeScriptOrigin","appendBase","match","concat"],"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\nexport function normalizeUrl(url: string | undefined, options?: { changeScriptOrigin?: boolean }) {\n const { changeScriptOrigin = true } = options ?? {};\n let appendBase = \"''\";\n if (changeScriptOrigin) {\n appendBase = \"window.proxy?.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || ''\";\n }\n return url?.match(/^https?/i) ? `'${url}'` : `(${appendBase}) + '${url}'`;\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;AAEA,OAAO,SAASC,YAAYA,CAACC,GAAuB,EAAEC,OAA0C,EAAE;EAChG,IAAAC,IAAA,GAAsCD,OAAO,aAAPA,OAAO,cAAPA,OAAO,GAAI,CAAC,CAAC;IAAAE,qBAAA,GAAAD,IAAA,CAA3CE,kBAAkB;IAAlBA,kBAAkB,GAAAD,qBAAA,cAAG,IAAI,GAAAA,qBAAA;EACjC,IAAIE,UAAU,GAAG,IAAI;EACrB,IAAID,kBAAkB,EAAE;IACtBC,UAAU,GAAG,yDAAyD;EACxE;EACA,OAAOL,GAAG,aAAHA,GAAG,eAAHA,GAAG,CAAEM,KAAK,CAAC,UAAU,CAAC,OAAAC,MAAA,CAAOP,GAAG,aAAAO,MAAA,CAAUF,UAAU,WAAAE,MAAA,CAAQP,GAAG,MAAG;AAC3E"}
package/lib/index.js CHANGED
@@ -31,7 +31,7 @@ var qiankunPlugin = (appName, pluginOptions = {}) => {
31
31
  return {
32
32
  name: "vite-plugin-qiankun",
33
33
  transformIndexHtml(html, context) {
34
- var _a;
34
+ var _a, _b;
35
35
  const $ = (0, import_cheerio.load)(html, { sourceCodeLocationInfo: true });
36
36
  const entryScript = $("script[entry]").get(0) ?? $('body script[type=module], head script[crossorigin=""]').get(0);
37
37
  if (entryScript) {
@@ -49,12 +49,34 @@ var qiankunPlugin = (appName, pluginOptions = {}) => {
49
49
  ${S2}${createImportFinallyResolve(appName, { indent: S2 }).trim()}
50
50
  ${S1}});
51
51
  ${S0}`);
52
+ if (((_b = context.server) == null ? void 0 : _b.config.command) === "serve") {
53
+ const scripts = $("head script[type=module]").toArray();
54
+ const refreshScript = scripts.find((s) => /@react-refresh";$/m.test($(s).html() ?? ""));
55
+ if (refreshScript) {
56
+ const refreshScript$ = $(refreshScript);
57
+ const R1 = (0, import_utils.detectIndent)(html, refreshScript) + (0, import_utils.space)(2);
58
+ const content = refreshScript$.html();
59
+ const regExp = /import\s*{\s*injectIntoGlobalHook\s*}\s*from\s*"([^"]*@react-refresh)";/m;
60
+ const match = content == null ? void 0 : content.match(regExp);
61
+ if (content && match) {
62
+ const sentence = match[0];
63
+ const from = match[1];
64
+ const rest = content.replace(sentence, "");
65
+ refreshScript$.removeAttr("type");
66
+ refreshScript$.html(`
67
+ ${R1}import(${(0, import_utils.normalizeUrl)(from, { changeScriptOrigin })}).then(({ injectIntoGlobalHook }) => {
68
+ ${R1} ${rest.split("\n").map((s) => s.trim()).filter(Boolean).join(`
69
+ ${R1} `)}
70
+ ${R1}});`);
71
+ }
72
+ }
73
+ }
52
74
  const bodyBaseIndent = (0, import_utils.detectIndent)(html, $("body").get(0));
53
75
  const B1 = bodyBaseIndent + (0, import_utils.space)(2);
54
76
  const B2 = B1 + (0, import_utils.space)(2);
55
77
  $("body").append(`
56
78
  ${B1}<script>
57
- ${B2}${createQiankunHelper(appName, { indent: B2 + (0, import_utils.space)(2) }).trim()}
79
+ ${B2}${createQiankunHelper(appName, { indent: B2 })}
58
80
  ${B1}</script>
59
81
  `);
60
82
  const output = $.html();
@@ -67,21 +89,18 @@ ${B1}</script>
67
89
  configureServer(server) {
68
90
  const base = server.config.base;
69
91
  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
- });
92
+ server.middlewares.use((req, res, next) => {
93
+ const end = res.end.bind(res);
94
+ res.end = function(chunk, ...rest) {
95
+ if (typeof chunk === "string") {
96
+ const $ = (0, import_cheerio.load)(chunk);
97
+ module2DynamicImport({ $, scriptTag: $(`script[src="${base}@vite/client"]`).get(0), changeScriptOrigin });
98
+ chunk = $.html();
99
+ }
100
+ end(chunk, ...rest);
101
+ return this;
102
+ };
103
+ next();
85
104
  });
86
105
  };
87
106
  }
@@ -94,20 +113,11 @@ function module2DynamicImport(options) {
94
113
  }
95
114
  const script$ = $(scriptTag);
96
115
  const moduleSrc = script$.attr("src");
97
- let appendBase = "''";
98
- if (changeScriptOrigin) {
99
- appendBase = "window.proxy ? window.proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || '' : ''";
100
- }
101
116
  script$.removeAttr("src");
102
117
  script$.removeAttr("type");
103
118
  const space2 = ident;
104
119
  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)`);
120
+ ${space2}import(${(0, import_utils.normalizeUrl)(moduleSrc, { changeScriptOrigin })})`);
111
121
  return script$;
112
122
  }
113
123
  function createImportFinallyResolve(qiankunName, options) {
@@ -130,13 +140,13 @@ ${space2} const d = new Promise((resolve, reject) => {
130
140
  ${space2} window.proxy && (window.proxy[\`vite\${hookName}\`] = resolve)
131
141
  ${space2} })
132
142
  ${space2} return props => d.then(fn => fn(props));
133
- ${space2}}
143
+ ${space2}};
134
144
  ${space2}const bootstrap = createDeffer('bootstrap');
135
145
  ${space2}const mount = createDeffer('mount');
136
146
  ${space2}const unmount = createDeffer('unmount');
137
147
  ${space2}const update = createDeffer('update');
138
148
 
139
- ${space2};(global => {
149
+ ${space2}(global => {
140
150
  ${space2} global.qiankunName = '${qiankunName}';
141
151
  ${space2} global['${qiankunName}'] = {
142
152
  ${space2} bootstrap,
@@ -144,8 +154,7 @@ ${space2} mount,
144
154
  ${space2} unmount,
145
155
  ${space2} update
146
156
  ${space2} };
147
- ${space2}})(window);
148
- `;
157
+ ${space2}})(window);`.trimStart();
149
158
  }
150
159
  var src_default = qiankunPlugin;
151
160
  // Annotate the CommonJS export names for ESM import in node:
package/lib/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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,uDAA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A;AAAA,EACAA;AAAA,EACAA;AAAA;AAEF;AAEA,IAAO,cAAQ;",
4
+ "sourcesContent": ["import type { CheerioAPI } from 'cheerio';\nimport { load } from 'cheerio';\nimport type { Element } from 'domhandler';\nimport type { PluginOption } from 'vite';\nimport { detectIndent, normalizeUrl, 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 // Transform entry script\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 // Transform @react-refresh script\n if (context.server?.config.command === 'serve') {\n const scripts = $('head script[type=module]').toArray();\n const refreshScript = scripts.find((s) => /@react-refresh\";$/m.test($(s).html() ?? ''));\n if (refreshScript) {\n const refreshScript$ = $(refreshScript);\n const R1 = detectIndent(html, refreshScript) + space(2);\n const content = refreshScript$.html();\n const regExp = /import\\s*{\\s*injectIntoGlobalHook\\s*}\\s*from\\s*\"([^\"]*@react-refresh)\";/m;\n const match = content?.match(regExp);\n if (content && match) {\n const sentence = match[0];\n const from = match[1];\n const rest = content.replace(sentence, '');\n refreshScript$.removeAttr('type');\n refreshScript$.html(`\n${R1}import(${normalizeUrl(from, { changeScriptOrigin })}).then(({ injectIntoGlobalHook }) => {\n${R1} ${rest\n .split('\\n')\n .map((s) => s.trim())\n .filter(Boolean)\n .join(`\\n${R1} `)}\n${R1}});`);\n }\n }\n }\n\n // Add extra script to export lifecycles\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 })}\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 // Only apply to / i.e. the mount point of the app\n server.middlewares.use((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=\"${base}@vite/client\"]`).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\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 script$.removeAttr('src');\n script$.removeAttr('type');\n const space = ident;\n script$.html(`\n${space}import(${normalizeUrl(moduleSrc, { changeScriptOrigin })})`);\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);`.trimStart();\n}\n\nexport default qiankunPlugin;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,qBAAqB;AAGrB,mBAAkD;AAElD,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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KAAK;AACxB,UAAQ,WAAW,MAAM;AACzB,QAAMA,SAAQ;AACd,UAAQ,KAAK;AAAA,EACbA,oBAAe,2BAAa,WAAW,EAAE,mBAAmB,CAAC,IAAI;AACjE,SAAO;AACT;AAEA,SAAS,2BAA2B,aAAqB,SAA+B;AACtF,QAAM,EAAE,QAAQA,SAAQ,SAAS,IAAI,WAAW,CAAC;AACjD,SAAO;AAAA,EACPA,0GAAyG;AAAA,EACzGA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA;AAEF;AAEA,SAAS,oBAAoB,aAAqB,SAA+B;AAC/E,QAAM,EAAE,QAAQA,SAAQ,SAAS,IAAI,WAAW,CAAC;AACjD,SAAO;AAAA,EACPA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA;AAAA,EAEAA;AAAA,EACAA,iCAAgC;AAAA,EAChCA,mBAAkB;AAAA,EAClBA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA;AAAA,EACAA,oBAAmB,UAAU;AAC/B;AAEA,IAAO,cAAQ;",
6
6
  "names": ["space"]
7
7
  }
package/lib/utils.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import type { Element } from 'domhandler';
2
2
  export declare function detectIndent(html: string, entryScript: Element | undefined): string;
3
3
  export declare function space(num: number): string;
4
+ export declare function normalizeUrl(url: string | undefined, options?: {
5
+ changeScriptOrigin?: boolean;
6
+ }): string;
package/lib/utils.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var utils_exports = {};
21
21
  __export(utils_exports, {
22
22
  detectIndent: () => detectIndent,
23
+ normalizeUrl: () => normalizeUrl,
23
24
  space: () => space
24
25
  });
25
26
  module.exports = __toCommonJS(utils_exports);
@@ -51,9 +52,18 @@ function detectIndent(html, entryScript) {
51
52
  function space(num) {
52
53
  return new Array(num + 1).join(" ");
53
54
  }
55
+ function normalizeUrl(url, options) {
56
+ const { changeScriptOrigin = true } = options ?? {};
57
+ let appendBase = "''";
58
+ if (changeScriptOrigin) {
59
+ appendBase = "window.proxy?.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || ''";
60
+ }
61
+ return (url == null ? void 0 : url.match(/^https?/i)) ? `'${url}'` : `(${appendBase}) + '${url}'`;
62
+ }
54
63
  // Annotate the CommonJS export names for ESM import in node:
55
64
  0 && (module.exports = {
56
65
  detectIndent,
66
+ normalizeUrl,
57
67
  space
58
68
  });
59
69
  //# sourceMappingURL=utils.js.map
package/lib/utils.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;",
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\nexport function normalizeUrl(url: string | undefined, options?: { changeScriptOrigin?: boolean }) {\n const { changeScriptOrigin = true } = options ?? {};\n let appendBase = \"''\";\n if (changeScriptOrigin) {\n appendBase = \"window.proxy?.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || ''\";\n }\n return url?.match(/^https?/i) ? `'${url}'` : `(${appendBase}) + '${url}'`;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;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;AAEO,SAAS,aAAa,KAAyB,SAA4C;AAChG,QAAM,EAAE,qBAAqB,KAAK,IAAI,WAAW,CAAC;AAClD,MAAI,aAAa;AACjB,MAAI,oBAAoB;AACtB,iBAAa;AAAA,EACf;AACA,UAAO,2BAAK,MAAM,eAAc,IAAI,SAAS,IAAI,kBAAkB;AACrE;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiny-codes/vite-plugin-qiankun",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "vite for qiankun. This is a forked version of vite-plugin-qiankun.",
5
5
  "keywords": [
6
6
  "qiankun",
@@ -61,7 +61,8 @@
61
61
  "vite": "^2.9.9"
62
62
  },
63
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"
64
+ "rolldown": "^1.0.0-rc.9",
65
+ "vite": ">=8.0.0"
65
66
  },
66
67
  "publishConfig": {
67
68
  "registry": "https://registry.npmjs.org/"
package/readme.md CHANGED
@@ -1,34 +1,35 @@
1
- #### 简介
1
+ ## 简介
2
2
 
3
- forked from [vite-plugin-qiankun](https://github.com/tengmaoqing/vite-plugin-qiankun)
4
-
5
- > vite-plugin-qiankun: 帮助应用快速接入乾坤的vite插件
3
+ > `@tiny-codes/vite-plugin-qiankun`: 帮助应用快速接入乾坤的vite插件
6
4
 
7
5
  - 保留vite构建es模块的优势
8
6
  - 一键配置,不影响已有的vite配置
9
7
  - 支持vite开发环境
10
8
 
11
- #### 快速开始
9
+ forked from [vite-plugin-qiankun](https://github.com/tengmaoqing/vite-plugin-qiankun)
12
10
 
13
- ###### 1、在 `vite.config.ts` 中安装插件
11
+ ## 快速开始
12
+
13
+ ### 1、在 `vite.config.ts` 中安装插件
14
14
 
15
15
  ```typescript
16
16
  // vite.config.ts
17
- import qiankun from 'vite-plugin-qiankun';
17
+ import qiankun from '@tiny-codes/vite-plugin-qiankun';
18
18
 
19
19
  export default {
20
20
  // 这里的 'myMicroAppName' 是子应用名,主应用注册时AppName需保持一致
21
- plugins: [qiankun('myMicroAppName')],
22
- // 生产环境需要指定运行域名作为base
23
- base: 'http://xxx.com/',
21
+ plugins: [
22
+ // ...
23
+ qiankun('subApp'),
24
+ ],
24
25
  };
25
26
  ```
26
27
 
27
- ###### 2、在入口文件里面写入乾坤的生命周期配置
28
+ ### 2、在入口文件里面写入乾坤的生命周期配置
28
29
 
29
30
  ```typescript
30
31
  // main.ts
31
- import { qiankunWindow, renderWithQiankun } from 'vite-plugin-qiankun';
32
+ import { qiankunWindow, renderWithQiankun } from '@tiny-codes/vite-plugin-qiankun';
32
33
 
33
34
  // some code
34
35
  renderWithQiankun({
@@ -52,33 +53,15 @@ if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
52
53
  }
53
54
  ```
54
55
 
55
- ###### 3、dev下作为子应用调试
56
+ ### 3、子应用部署到不同的域名
56
57
 
57
- > 因为开发环境作为子应用时与热更新插件(可能与其他修改html的插件也会存在冲突)有冲突,所以需要额外的调试配置
58
+ 如果子应用与主应用部署的域名不同,并且子应用的`base`是相对路径(例如`/sub-app/`),此时很可能子应用会加载失败。子应用的入口 script 标签可能类似于 `/sub-app/index.js`,qiankun会尝试在主应用域名下加载该资源,当然会失败。
58
59
 
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) 选项,返回子应用的域名。
60
+ 要解决这个问题,可以使用 [getPublicPath](https://qiankun.umijs.org/zh/api) 选项,返回子应用的域名,注意末尾不要包含`/`。
80
61
 
81
62
  ```tsx
63
+ import { loadMicroApp } from 'qiankun';
64
+
82
65
  loadMicroApp(
83
66
  {
84
67
  name: 'sub-app',
@@ -90,12 +73,16 @@ loadMicroApp(
90
73
  );
91
74
  ```
92
75
 
93
- ###### 5、其它使用注意点 `qiankunWindow`
76
+ ### 4、显式指定入口文件
77
+
78
+ qiankun会默认取最后一个 `script` 标签作为子应用的入口文件,但如果由于特殊原因不能把入口文件放到最后,也可以显式指定入口文件,在script标签上添加 `entry` 属性即可,详情参考[官方文档](https://qiankun.umijs.org/zh/faq)。
79
+
80
+ ### 5、其它使用注意点 `qiankunWindow`
94
81
 
95
- 因为es模块加载与`qiankun`的实现方式有些冲突,所以使用本插件实现的`qiankun`微应用里面没有运行在js沙盒中。所以在不可避免需要设置window上的属性时,尽量显示的操作js沙盒,否则可能会对其它子应用产生副作用。qiankun沙盒使用方式
82
+ 因为 esm 模块加载与`qiankun`的实现方式有些冲突,所以使用本插件实现的`qiankun`微应用里面没有运行在js沙盒中。所以在不可避免需要设置window上的属性时,尽量显式地操作js沙盒,否则可能会对其它子应用产生副作用。qiankun沙盒对象的使用方式:
96
83
 
97
84
  ```typescript
98
- import { qiankunWindow } from 'vite-plugin-qiankun';
85
+ import { qiankunWindow } from '@tiny-codes/vite-plugin-qiankun';
99
86
 
100
87
  qiankunWindow.customxxx = 'ssss';
101
88
 
@@ -104,7 +91,7 @@ if (qiankunWindow.__POWERED_BY_QIANKUN__) {
104
91
  }
105
92
  ```
106
93
 
107
- #### 例子
94
+ ## 例子
108
95
 
109
96
  详细的信息可以参考例子里面的使用方式
110
97