From 1fccb057a9ceacc26c2bdeaaf386001e8d0155f1 Mon Sep 17 00:00:00 2001 From: jardenliu <15915839707@163.com> Date: Wed, 9 Feb 2022 09:39:35 +0800 Subject: [PATCH 1/2] feat: provide source script info for js loader --- src/source/scripts.ts | 23 ++++++++++++----------- typings/global.d.ts | 4 ++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/source/scripts.ts b/src/source/scripts.ts index eebeaa21c..bf8cda2b0 100644 --- a/src/source/scripts.ts +++ b/src/source/scripts.ts @@ -238,7 +238,7 @@ export function runScript ( callback?: moduleCallBack, ): any { try { - const code = bindScope(url, app, info.code, info.module) + const code = bindScope(url, app, info.code, info) if (app.inline || info.module) { const scriptElement = pureCreateElement('script') runCode2InlineScript(url, code, info.module, scriptElement, callback) @@ -296,7 +296,7 @@ export function runDynamicRemoteScript ( app.source.scripts.set(url, info) info.isGlobal && globalScripts.set(url, code) try { - code = bindScope(url, app, code, info.module) + code = bindScope(url, app, code, info) if (app.inline || info.module) { runCode2InlineScript(url, code, info.module, replaceElement as HTMLScriptElement, dispatchScriptOnLoadEvent) } else { @@ -360,19 +360,19 @@ function runCode2Function (code: string, info: sourceScriptInfo) { * @param url script address * @param app app * @param code code - * @param module type='module' of script + * @param info source script info */ function bindScope ( url: string, app: AppInterface, code: string, - module: boolean, + info: sourceScriptInfo, ): string { if (isPlainObject(microApp.plugins)) { - code = usePlugins(url, code, app.name, microApp.plugins!) + code = usePlugins(url, code, app.name, microApp.plugins!, info) } - if (app.sandBox && !module) { + if (app.sandBox && !info.module) { globalEnv.rawWindow.__MICRO_APP_PROXY_WINDOW__ = app.sandBox.proxyWindow return `;(function(proxyWindow){with(proxyWindow.__MICRO_APP_WINDOW__){(function(${globalKeyToBeCached}){;${code}\n}).call(proxyWindow,${globalKeyToBeCached})}})(window.__MICRO_APP_PROXY_WINDOW__);` } @@ -386,21 +386,22 @@ function bindScope ( * @param code code * @param appName app name * @param plugins plugin list + * @param info source script info */ -function usePlugins (url: string, code: string, appName: string, plugins: plugins): string { - const newCode = processCode(plugins.global, code, url) +function usePlugins (url: string, code: string, appName: string, plugins: plugins, info: sourceScriptInfo): string { + const newCode = processCode(plugins.global, code, url, info) - return processCode(plugins.modules?.[appName], newCode, url) + return processCode(plugins.modules?.[appName], newCode, url, info) } -function processCode (configs: plugins['global'], code: string, url: string) { +function processCode (configs: plugins['global'], code: string, url: string, info: sourceScriptInfo) { if (!isArray(configs)) { return code } return configs.reduce((preCode, config) => { if (isPlainObject(config) && isFunction(config.loader)) { - return config.loader!(preCode, url, config.options) + return config.loader!(preCode, url, config.options, info) } return preCode diff --git a/typings/global.d.ts b/typings/global.d.ts index f88415cd8..96c9e30a8 100644 --- a/typings/global.d.ts +++ b/typings/global.d.ts @@ -136,7 +136,7 @@ declare module '@micro-app/types' { // options for plugin as the third parameter of loader options?: unknown // handle function - loader?: (code: string, url: string, options: unknown) => string + loader?: (code: string, url: string, options: unknown, info: sourceScriptInfo) => string }> // plugin for special app @@ -149,7 +149,7 @@ declare module '@micro-app/types' { // options for plugin as the third parameter of loader options?: unknown // handle function - loader?: (code: string, url: string, options: unknown) => string + loader?: (code: string, url: string, options: unknown, info: sourceScriptInfo) => string }> } } From 462b5c39e39838e8e965b2ca4ab688dd7ba930d7 Mon Sep 17 00:00:00 2001 From: jardenliu Date: Sat, 12 Feb 2022 23:38:52 +0800 Subject: [PATCH 2/2] docs: update docs --- docs/zh-cn/api.md | 4 ++-- docs/zh-cn/plugins.md | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/zh-cn/api.md b/docs/zh-cn/api.md index 405783337..bf74110ca 100644 --- a/docs/zh-cn/api.md +++ b/docs/zh-cn/api.md @@ -47,7 +47,7 @@ start (options?: { // 可选,传递给loader的配置项 options?: unknown, // 必填,js处理函数,必须返回code值 - loader?: (code: string, url: string, options: unknown) => string + loader?: (code: string, url: string, options: unknown, info: sourceScriptInfo) => string }> // 子应用插件 @@ -61,7 +61,7 @@ start (options?: { // 可选,传递给loader的配置项 options?: unknown, // 必填,js处理函数,必须返回code值 - loader?: (code: string, url: string, options: unknown) => string + loader?: (code: string, url: string, options: unknown, info: sourceScriptInfo) => string }> } }, diff --git a/docs/zh-cn/plugins.md b/docs/zh-cn/plugins.md index 607519e80..4852bc186 100644 --- a/docs/zh-cn/plugins.md +++ b/docs/zh-cn/plugins.md @@ -20,7 +20,7 @@ microApp.start({ // 可选,传递给loader的配置项 options?: any, // 必填,js处理函数,必须返回code值 - loader?: (code: string, url: string, options: any) => code + loader?: (code: string, url: string, options: any, info: sourceScriptInfo) => code }> // 子应用插件 @@ -34,7 +34,7 @@ microApp.start({ // 可选,传递给loader的配置项 options?: any, // 必填,js处理函数,必须返回code值 - loader?: (code: string, url: string, options: any) => code + loader?: (code: string, url: string, options: any, info: sourceScriptInfo) => code }> } } @@ -52,7 +52,7 @@ microApp.start({ scopeProperties: ['key', 'key', ...], // 可选 escapeProperties: ['key', 'key', ...], // 可选 options: 配置项, // 可选 - loader(code, url, options) { // 必填 + loader(code, url, options, info) { // 必填 console.log('全局插件') return code } @@ -60,7 +60,7 @@ microApp.start({ ], modules: { 'appName1': [{ - loader(code, url, options) { + loader(code, url, options, info) { if (url === 'xxx.js') { code = code.replace('var abc =', 'window.abc =') } @@ -71,7 +71,7 @@ microApp.start({ scopeProperties: ['key', 'key', ...], // 可选 escapeProperties: ['key', 'key', ...], // 可选 options: 配置项, // 可选 - loader(code, url, options) { // 必填 + loader(code, url, options, info) { // 必填 console.log('只适用于appName2的插件') return code }