Skip to content

Commit

Permalink
Merge branch 'next' into feat/new-document-appendchild
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Oct 19, 2023
2 parents 8170933 + 408af41 commit 002dba7
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 32 deletions.
3 changes: 2 additions & 1 deletion packages/loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Sandbox } from '@qiankunjs/sandbox';
import { qiankunHeadTagName } from '@qiankunjs/sandbox';
import type { BaseTranspilerOpts } from '@qiankunjs/shared';
import {
Expand Down Expand Up @@ -25,7 +26,7 @@ type Entry = HTMLEntry;
export type ImportOpts = {
decoder?: (chunk: string) => string;
nodeTransformer?: typeof transpileAssets;
} & BaseTranspilerOpts;
} & BaseTranspilerOpts & { sandbox?: Sandbox };

/**
* @param entry
Expand Down
4 changes: 0 additions & 4 deletions packages/qiankun/src/core/loadApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ export default async function loadApp<T extends ObjectType>(
}

function initContainer(container: HTMLElement, appName: string, sandboxCfg: AppConfiguration['sandbox']): void {
if (!container) {
throw new QiankunError('container is not existed');
}

while (container.firstChild) {
container.removeChild(container.firstChild);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sandbox/src/core/compartment/globalProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function shouldSkipProperty(global: WindowProxy, p: string | number): boolean {
if (isIE11()) {
// https://github.com/kuitos/import-html-entry/pull/32,最小化 try 范围
try {
return global[p as number] && typeof window !== 'undefined' && global[p as number].parent === window;
return !!global[p as keyof WindowProxy] && typeof window !== 'undefined' && global[p as number].parent === window;
} catch (err) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sandbox/src/core/compartment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare global {
interface Window {
__compartment_window__?: Window;

[p: CompartmentGlobalId]: WindowProxy;
[p: CompartmentGlobalId]: WindowProxy | undefined;
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/sandbox/src/core/sandbox/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// generated from https://github.com/sindresorhus/globals/blob/main/globals.json es2015 part
// only init its values while Proxy is supported
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
export const globals = window.Proxy
? [
'Array',
Expand Down Expand Up @@ -60,4 +61,4 @@ export const globals = window.Proxy
'WeakMap',
'WeakSet',
].filter((p) => /* just keep the available properties in current window context */ p in window)
: [];
: [];
1 change: 1 addition & 0 deletions packages/sandbox/src/core/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function createSandboxContainer(
) {
const { globalContext, extraGlobals = {} } = opts;
let sandbox: Sandbox;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (window.Proxy) {
sandbox = new StandardSandbox(appName, extraGlobals, globalContext);
} else {
Expand Down
7 changes: 2 additions & 5 deletions packages/sandbox/src/patchers/dynamicAppend/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ export function isHijackingTag(tagName?: string) {
* Such as the style element generated by styled-components and emotion.
* @param element
*/
export function isStyledComponentsLike(element: HTMLStyleElement) {
return (
!element.textContent &&
((element.sheet as CSSStyleSheet).cssRules.length || getStyledElementCSSRules(element)?.length)
);
export function isStyledComponentsLike(element: HTMLStyleElement): boolean {
return !element.textContent && (element.sheet?.cssRules.length || getStyledElementCSSRules(element)?.length);
}

const appsCounterMap = new Map<string, { bootstrappingPatchCount: number; mountingPatchCount: number }>();
Expand Down
4 changes: 2 additions & 2 deletions packages/sandbox/src/patchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function patchAtBootstrapping(appName: string, getContainer: () => HTMLEl
[SandboxType.Snapshot]: [],
} as const;

return patchersInSandbox[sandbox.type]?.map((patch) => patch());
return patchersInSandbox[sandbox.type].map((patch) => patch());
}

export function patchAtMounting(appName: string, getContainer: () => HTMLElement, sandbox: Sandbox): Free[] {
Expand All @@ -35,5 +35,5 @@ export function patchAtMounting(appName: string, getContainer: () => HTMLElement
[SandboxType.Snapshot]: basePatchers,
};

return patchersInSandbox[sandbox.type]?.map((patch) => patch());
return patchersInSandbox[sandbox.type].map((patch) => patch());
}
21 changes: 9 additions & 12 deletions packages/sandbox/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ export function isConstructable(fn: CallableFunction): fn is CallableFunction {

if (hasPrototypeMethods) return true;

const cachedValue = fnRegexCheckCacheMap.get(fn);
if (typeof cachedValue !== 'undefined') {
return cachedValue;
const cachedResult = fnRegexCheckCacheMap.get(fn);
if (typeof cachedResult !== 'undefined') {
return cachedResult;
}

let constructable: boolean = hasPrototypeMethods;
if (!constructable) {
// fn.toString has a significant performance overhead, if hasPrototypeMethods check not passed, we will check the function string with regex
const fnString = fn.toString();
const constructableFunctionRegex = /^function\b\s[A-Z].*/;
const classRegex = /^class\b/;
constructable = constructableFunctionRegex.test(fnString) || classRegex.test(fnString);
}
// fn.toString has a significant performance overhead, if hasPrototypeMethods check not passed, we will check the function string with regex
const fnString = fn.toString();
const constructableFunctionRegex = /^function\b\s[A-Z].*/;
const classRegex = /^class\b/;
const constructable = constructableFunctionRegex.test(fnString) || classRegex.test(fnString);

fnRegexCheckCacheMap.set(fn, constructable);
return constructable;
Expand Down Expand Up @@ -56,7 +53,7 @@ export function isCallable(fn: unknown): fn is CallableFunction {
const frozenPropertyCacheMap = new WeakMap<object, Record<PropertyKey, boolean>>();

export function isPropertyFrozen(target: object, p?: PropertyKey): boolean {
if (!target || !p) {
if (!p) {
return false;
}

Expand Down
8 changes: 3 additions & 5 deletions packages/shared/src/assets-transpilers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
* @author Kuitos
* @since 2023-08-26
*/
// import type { Sandbox } from '@qiankunjs/sandbox';
import type { BaseLoaderOpts } from '../common';

import type { MatchResult } from '../module-resolver';

export type BaseTranspilerOpts = BaseLoaderOpts & {
moduleResolver?: (url: string) => MatchResult | undefined;
// TODO: 先把 sandbox 类型设置如下,解除和 @qiankunjs/loader 相互依赖的问题
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sandbox?: Record<string, any>;
sandbox?: {
makeEvaluateFactory(source: string, sourceURL?: string): string;
};
};

export type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };
Expand Down

0 comments on commit 002dba7

Please sign in to comment.