Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/micro-zoe/micro-app into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Feb 14, 2022
2 parents 4850198 + 815b541 commit dd41912
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docs/zh-cn/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}>

// 子应用插件
Expand All @@ -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
}>
}
},
Expand Down
10 changes: 5 additions & 5 deletions docs/zh-cn/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}>

// 子应用插件
Expand All @@ -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
}>
}
}
Expand All @@ -52,15 +52,15 @@ microApp.start({
scopeProperties: ['key', 'key', ...], // 可选
escapeProperties: ['key', 'key', ...], // 可选
options: 配置项, // 可选
loader(code, url, options) { // 必填
loader(code, url, options, info) { // 必填
console.log('全局插件')
return code
}
}
],
modules: {
'appName1': [{
loader(code, url, options) {
loader(code, url, options, info) {
if (url === 'xxx.js') {
code = code.replace('var abc =', 'window.abc =')
}
Expand All @@ -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
}
Expand Down
23 changes: 12 additions & 11 deletions src/source/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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__);`
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}>
}
}
Expand Down

0 comments on commit dd41912

Please sign in to comment.