Skip to content

Commit

Permalink
feat: add config excludeAssetFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Aug 19, 2022
1 parent f6b52f8 commit 8cfce97
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
10 changes: 10 additions & 0 deletions dev/children/react16/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,13 @@ if (window.__MICRO_APP_ENVIRONMENT__) {
unBoundDom2.innerHTML = 'unBoundDom2'
document.body.appendChild(unBoundDom2)
}

// test excludeAssetFilter
const dynamicScript2 = document.createElement('script')
dynamicScript2.setAttribute('src', 'http://127.0.0.1:8080/js/defer.js')
dynamicScript2.setAttribute('defer', 'true')
document.body.appendChild(dynamicScript2)

const link1 = document.createElement('link')
link1.setAttribute('href', 'http://127.0.0.1:8080/facefont.css')
document.head.appendChild(link1)
8 changes: 8 additions & 0 deletions dev/main-react16/src/global.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ microApp.start({
return res.text()
})
},
excludeAssetFilter (assetUrl) {
if (assetUrl === 'http://127.0.0.1:8080/js/defer.js') {
return true
} else if (assetUrl === 'http://127.0.0.1:8080/facefont.css') {
return true
}
return false
}
})

// ----------------------分割线--测试全局方法--------------------- //
Expand Down
2 changes: 2 additions & 0 deletions docs/zh-cn/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ start (options?: {
js?: string[], // js地址
css?: string[], // css地址
},
// 指定部分特殊的动态加载的微应用资源(css/js) 不被 micro-app 劫持处理
excludeAssetFilter?: (assetUrl: string) => boolean
})
```

Expand Down
20 changes: 19 additions & 1 deletion docs/zh-cn/static-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,25 @@ microApp.start({
```

## 资源过滤
当子应用不需要加载某个js或css,可以通过在link、script、style设置exclude属性过滤这些资源,当micro-app遇到带有exclude属性的元素会进行删除。
#### 方式一:excludeAssetFilter
在start中注册excludeAssetFilter过滤函数,可以指定部分特殊的动态加载的微应用资源(css/js) 不被 micro-app 劫持处理。

```js
// index.js
import microApp from '@micro-zoe/micro-app'

microApp.start({
excludeAssetFilter (assetUrl) {
if (assetUrl === 'xxx') {
return true // 返回true则micro-app不会劫持处理当前文件
}
return false
}
})
```

#### 方式二:配置 exclude 属性
在link、script、style等元素上设置exclude属性过滤这些资源,当micro-app遇到带有exclude属性的元素会进行删除。

**使用方式**
```html
Expand Down
3 changes: 3 additions & 0 deletions src/micro_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class MicroApp extends EventCenterForBaseApp implements MicroAppConfigTyp
plugins?: plugins
fetch?: fetchType
preFetch = preFetch
excludeAssetFilter?: (assetUrl: string) => boolean
start (options?: OptionsType): void {
if (!isBrowser || !window.customElements) {
return logError('micro-app is not supported in this environment')
Expand Down Expand Up @@ -168,6 +169,8 @@ export class MicroApp extends EventCenterForBaseApp implements MicroAppConfigTyp
// load global assets when browser is idle
options.globalAssets && getGlobalAssets(options.globalAssets)

isFunction(options.excludeAssetFilter) && (this.excludeAssetFilter = options.excludeAssetFilter)

if (isPlainObject(options.plugins)) {
const modules = options.plugins!.modules
if (isPlainObject(modules)) {
Expand Down
19 changes: 18 additions & 1 deletion src/source/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isString,
isInvalidQuerySelectorKey,
isUniqueElement,
isFunction,
} from '../libs/utils'
import scopedCSS from './scoped_css'
import { extractLinkFromHtml, formatDynamicLink } from './links'
Expand Down Expand Up @@ -41,7 +42,15 @@ function handleNewNode (parent: Node, child: Node, app: AppInterface): Node {
const linkReplaceComment = document.createComment('link element with exclude attribute ignored by micro-app')
dynamicElementInMicroAppMap.set(child, linkReplaceComment)
return linkReplaceComment
} else if (child.hasAttribute('ignore') || checkIgnoreUrl(child.getAttribute('href'), app.name)) {
} else if (
child.hasAttribute('ignore') ||
checkIgnoreUrl(child.getAttribute('href'), app.name) ||
(
child.href &&
isFunction(microApp.excludeAssetFilter) &&
microApp.excludeAssetFilter!(child.href)
)
) {
return child
}

Expand All @@ -65,6 +74,14 @@ function handleNewNode (parent: Node, child: Node, app: AppInterface): Node {

return child
} else if (child instanceof HTMLScriptElement) {
if (
child.src &&
isFunction(microApp.excludeAssetFilter) &&
microApp.excludeAssetFilter!(child.src)
) {
return child
}

const { replaceComment, url, info } = extractScriptElement(
child,
parent,
Expand Down
2 changes: 2 additions & 0 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ declare module '@micro-app/types' {
plugins?: plugins
fetch?: fetchType
globalAssets?: globalAssetsType,
excludeAssetFilter?: (assetUrl: string) => boolean
}

// MicroApp config
Expand All @@ -203,6 +204,7 @@ declare module '@micro-app/types' {
plugins?: plugins
fetch?: fetchType
preFetch(apps: prefetchParamList): void
excludeAssetFilter?: (assetUrl: string) => boolean
start(options?: OptionsType): void
}

Expand Down

0 comments on commit 8cfce97

Please sign in to comment.