Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加disableBlob全局或实例选项,可选使用blob #273

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/__tests__/create_app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,24 @@ describe('create_app', () => {
})
})

// 关闭Blob
test('disableBlob in this app', async () => {
const microAppElement2 = document.createElement('micro-app')
microAppElement2.setAttribute('name', 'test-app21')
microAppElement2.setAttribute('url', `http://127.0.0.1:${ports.create_app}/dynamic/`)
microAppElement2.setAttribute('disableBlob', 'true')

appCon.appendChild(microAppElement2)
await new Promise((resolve) => {
microAppElement2.addEventListener('mounted', () => {
expect(appInstanceMap.get('test-app21')!.useBlob).toBeFalsy()
resolve(true)
}, false)
})

appCon.removeChild(microAppElement2)
})

// 测试getActiveApps方法
test('test getActiveApps method', async () => {
// 因为上面已经执行unmountAllApps卸载了所有应用,所以这里长度为0
Expand Down
4 changes: 4 additions & 0 deletions src/create_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface CreateAppParam {
url: string
ssrUrl?: string
scopecss: boolean
useBlob?: boolean
useSandbox: boolean
inline?: boolean
baseroute?: string
Expand All @@ -55,6 +56,7 @@ export default class CreateApp implements AppInterface {
container: HTMLElement | ShadowRoot | null = null
inline: boolean
scopecss: boolean
useBlob: boolean
useSandbox: boolean
baseroute = ''
source: sourceType
Expand All @@ -67,6 +69,7 @@ export default class CreateApp implements AppInterface {
container,
inline,
scopecss,
useBlob,
useSandbox,
baseroute,
}: CreateAppParam) {
Expand All @@ -78,6 +81,7 @@ export default class CreateApp implements AppInterface {
this.name = name
this.url = url
this.useSandbox = useSandbox
this.useBlob = useBlob ?? true
this.scopecss = this.useSandbox && scopecss
this.source = {
links: new Map<string, sourceLinkInfo>(),
Expand Down
2 changes: 2 additions & 0 deletions src/micro_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class MicroApp extends EventCenterForBaseApp implements MicroAppConfigTyp
inline?: boolean
disableScopecss?: boolean
disableSandbox?: boolean
disableBlob?: boolean
ssr?: boolean
lifeCycles?: lifeCyclesType
plugins?: plugins
Expand Down Expand Up @@ -155,6 +156,7 @@ export class MicroApp extends EventCenterForBaseApp implements MicroAppConfigTyp
// @ts-ignore
this.destory = options.destory
this.inline = options.inline
this.disableBlob = options.disableBlob
this.disableScopecss = options.disableScopecss
this.disableSandbox = options.disableSandbox
this.ssr = options.ssr
Expand Down
1 change: 1 addition & 0 deletions src/micro_app_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export function defineElement (tagName: string): void {
container: this.shadowRoot ?? this,
inline: this.getDisposeResult('inline'),
scopecss: !(this.getDisposeResult('disableScopecss') || this.getDisposeResult('shadowDOM')),
useBlob: !this.getDisposeResult('disableBlob'),
useSandbox: !this.getDisposeResult('disableSandbox'),
baseroute: this.getBaseRouteCompatible(),
})
Expand Down
13 changes: 9 additions & 4 deletions src/source/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function runScript (
const code = bindScope(url, app, info.code, info.module)
if (app.inline || info.module) {
const scriptElement = pureCreateElement('script')
runCode2InlineScript(url, code, info.module, scriptElement, callback)
runCode2InlineScript(url, code, info.module, app.useBlob, scriptElement, callback)
if (isDynamic) return scriptElement
// TEST IGNORE
app.container?.querySelector('micro-app-body')!.appendChild(scriptElement)
Expand Down Expand Up @@ -298,7 +298,7 @@ export function runDynamicRemoteScript (
try {
code = bindScope(url, app, code, info.module)
if (app.inline || info.module) {
runCode2InlineScript(url, code, info.module, replaceElement as HTMLScriptElement, dispatchScriptOnLoadEvent)
runCode2InlineScript(url, code, info.module, app.useBlob, replaceElement as HTMLScriptElement, dispatchScriptOnLoadEvent)
} else {
runCode2Function(code, info)
}
Expand Down Expand Up @@ -326,13 +326,18 @@ function runCode2InlineScript (
url: string,
code: string,
module: boolean,
useBlob: boolean,
scriptElement: HTMLScriptElement,
callback?: moduleCallBack,
): void {
if (module) {
// module script is async, transform it to a blob for subsequent operations
const blob = new Blob([code], { type: 'text/javascript' })
scriptElement.src = URL.createObjectURL(blob)
if (useBlob) {
const blob = new Blob([code], { type: 'text/javascript' })
scriptElement.src = URL.createObjectURL(blob)
} else {
scriptElement.src = url
}
scriptElement.setAttribute('type', 'module')
if (callback) {
callback.moduleCount && callback.moduleCount--
Expand Down
2 changes: 2 additions & 0 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ declare module '@micro-app/types' {
container: HTMLElement | ShadowRoot | null // container maybe null, micro-app, shadowRoot, DIV(keep-alive)
inline: boolean // whether js runs in inline script mode, default is false
scopecss: boolean // whether use css scoped, default is true
useBlob: boolean // whether use blob to load js, default is true
useSandbox: boolean // whether use js sandbox, default is true
baseroute: string // route prefix, default is ''
source: sourceType // sources of css, js, html
Expand Down Expand Up @@ -166,6 +167,7 @@ declare module '@micro-app/types' {
shadowDOM?: boolean
destroy?: boolean
inline?: boolean
disableBlob?: boolean
disableScopecss?: boolean
disableSandbox?: boolean
ssr?: boolean
Expand Down