diff --git a/.changeset/wicked-icons-type.md b/.changeset/wicked-icons-type.md new file mode 100644 index 000000000..9240c3bbb --- /dev/null +++ b/.changeset/wicked-icons-type.md @@ -0,0 +1,8 @@ +--- +"@qiankunjs/loader": patch +"qiankun": patch +"@qiankunjs/sandbox": patch +"@qiankunjs/shared": patch +--- + +feat: extract NodeTransformer type to shared package diff --git a/packages/loader/src/index.ts b/packages/loader/src/index.ts index 209ad974e..0bfc0e51c 100644 --- a/packages/loader/src/index.ts +++ b/packages/loader/src/index.ts @@ -1,7 +1,7 @@ import type { Sandbox } from '@qiankunjs/sandbox'; import { qiankunHeadTagName } from '@qiankunjs/sandbox'; -import type { BaseTranspilerOpts } from '@qiankunjs/shared'; -import { Deferred, moduleResolver as defaultModuleResolver, QiankunError, transpileAssets } from '@qiankunjs/shared'; +import type { BaseTranspilerOpts, NodeTransformer } from '@qiankunjs/shared'; +import { Deferred, QiankunError } from '@qiankunjs/shared'; import { createTagTransformStream } from './TagTransformStream'; import { isUrlHasOwnProtocol } from './utils'; import WritableDOMStream from './writable-dom'; @@ -19,8 +19,8 @@ type Entry = HTMLEntry; // export type LoaderOpts = { streamTransformer?: () => TransformStream; - nodeTransformer?: typeof transpileAssets; -} & BaseTranspilerOpts & { sandbox?: Sandbox }; + nodeTransformer?: NodeTransformer; +} & Omit & { sandbox?: Sandbox }; /** * @param entry @@ -28,15 +28,7 @@ export type LoaderOpts = { * @param opts */ export async function loadEntry(entry: Entry, container: HTMLElement, opts: LoaderOpts): Promise { - const { - fetch, - nodeTransformer = transpileAssets, - streamTransformer, - sandbox, - moduleResolver = (url: string) => { - return defaultModuleResolver(url, container, document.head); - }, - } = opts; + const { fetch, streamTransformer, sandbox, nodeTransformer } = opts; const res = isUrlHasOwnProtocol(entry) ? await fetch(entry) : new Response(entry, { status: 200, statusText: 'OK' }); if (res.body) { @@ -77,12 +69,13 @@ export async function loadEntry(entry: Entry, container: HTMLElement, opts: L ) .pipeTo( new WritableDOMStream(container, null, (clone, node) => { - const transformedNode = nodeTransformer(clone, entry, { - fetch, - sandbox, - moduleResolver, - rawNode: node as unknown as Node, - }); + const transformedNode = nodeTransformer + ? nodeTransformer(clone, entry, { + fetch, + sandbox, + rawNode: node as unknown as Node, + }) + : clone; const script = transformedNode as unknown as HTMLScriptElement; diff --git a/packages/loader/tsconfig.json b/packages/loader/tsconfig.json deleted file mode 100644 index 4082f16a5..000000000 --- a/packages/loader/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../tsconfig.json" -} diff --git a/packages/qiankun/.fatherrc.js b/packages/qiankun/.fatherrc.js index e0d6022e9..d624659c2 100644 --- a/packages/qiankun/.fatherrc.js +++ b/packages/qiankun/.fatherrc.js @@ -8,6 +8,6 @@ const versionFilePath = join(__dirname, './src/version.ts'); writeFileSync(versionFilePath, `export const version = '${version}';`); export default { - umd: {}, + // umd: {}, ...cfg, }; diff --git a/packages/qiankun/src/core/loadApp.ts b/packages/qiankun/src/core/loadApp.ts index 1ac5e29d5..e4f3c2185 100644 --- a/packages/qiankun/src/core/loadApp.ts +++ b/packages/qiankun/src/core/loadApp.ts @@ -6,7 +6,7 @@ import type { LoaderOpts } from '@qiankunjs/loader'; import { loadEntry } from '@qiankunjs/loader'; import type { Sandbox } from '@qiankunjs/sandbox'; import { createSandboxContainer } from '@qiankunjs/sandbox'; -import { wrapFetchWithCache } from '@qiankunjs/shared'; +import { moduleResolver as defaultModuleResolver, transpileAssets, wrapFetchWithCache } from '@qiankunjs/shared'; import { concat, isFunction, mergeWith } from 'lodash'; import type { ParcelConfigObject } from 'single-spa'; import getAddOns from '../addons'; @@ -29,7 +29,18 @@ export default async function loadApp( lifeCycles?: LifeCycles, ): Promise { const { name: appName, entry, container } = app; - const { fetch = window.fetch, sandbox, globalContext = window, ...restConfiguration } = configuration || {}; + const defaultNodeTransformer: AppConfiguration['nodeTransformer'] = (node, baseURI, opts) => { + const moduleResolver = (url: string) => defaultModuleResolver(url, sandboxMicroAppContainer, document.head); + return transpileAssets(node, baseURI, { ...opts, moduleResolver }); + }; + const { + fetch = window.fetch, + sandbox, + globalContext = window, + nodeTransformer = defaultNodeTransformer, + ...restConfiguration + } = configuration || {}; + const fetchWithLruCache = wrapFetchWithCache(fetch); const markName = `[qiankun] App ${appName} Loading`; @@ -50,7 +61,7 @@ export default async function loadApp( globalContext, extraGlobals: {}, fetch: fetchWithLruCache, - nodeTransformer: restConfiguration.nodeTransformer, + nodeTransformer, }); sandboxInstance = sandboxContainer.instance; @@ -60,7 +71,12 @@ export default async function loadApp( unmountSandbox = () => sandboxContainer.unmount(); } - const containerOpts: LoaderOpts = { fetch: fetchWithLruCache, sandbox: sandboxInstance, ...restConfiguration }; + const containerOpts: LoaderOpts = { + fetch: fetchWithLruCache, + sandbox: sandboxInstance, + nodeTransformer, + ...restConfiguration, + }; const lifecyclesPromise = loadEntry(entry, sandboxMicroAppContainer, containerOpts); diff --git a/packages/qiankun/tsconfig.json b/packages/qiankun/tsconfig.json deleted file mode 100644 index 4082f16a5..000000000 --- a/packages/qiankun/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../tsconfig.json" -} diff --git a/packages/sandbox/src/patchers/dynamicAppend/common.ts b/packages/sandbox/src/patchers/dynamicAppend/common.ts index e5cecadeb..155bd808d 100644 --- a/packages/sandbox/src/patchers/dynamicAppend/common.ts +++ b/packages/sandbox/src/patchers/dynamicAppend/common.ts @@ -159,13 +159,11 @@ export function getOverwrittenAppendChildOrInsertBefore( } const { sandbox, nodeTransformer, fetch } = sandboxConfig; - const transpiledStyleSheetElement = nodeTransformer - ? nodeTransformer(stylesheetElement, location.href, { - fetch, - sandbox, - rawNode: stylesheetElement, - }) - : stylesheetElement; + const transpiledStyleSheetElement = nodeTransformer(stylesheetElement, location.href, { + fetch, + sandbox, + rawNode: stylesheetElement, + }); const result = appendChild.call(this, transpiledStyleSheetElement, referenceNode); @@ -200,15 +198,13 @@ export function getOverwrittenAppendChildOrInsertBefore( scriptTranspiledDeferred = new Deferred(); } - const transpiledScriptElement = nodeTransformer - ? nodeTransformer(scriptElement, location.href, { - fetch, - sandbox, - rawNode: scriptElement, - prevScriptTranspiledDeferred, - scriptTranspiledDeferred, - } as ScriptTranspilerOpts) - : scriptElement; + const transpiledScriptElement = nodeTransformer(scriptElement, location.href, { + fetch, + sandbox, + rawNode: scriptElement, + prevScriptTranspiledDeferred, + scriptTranspiledDeferred, + } as ScriptTranspilerOpts); const result = appendChild.call(this, transpiledScriptElement, refChild) as T; diff --git a/packages/sandbox/src/patchers/dynamicAppend/types.ts b/packages/sandbox/src/patchers/dynamicAppend/types.ts index 09e67cb2e..5919f33a2 100644 --- a/packages/sandbox/src/patchers/dynamicAppend/types.ts +++ b/packages/sandbox/src/patchers/dynamicAppend/types.ts @@ -2,7 +2,7 @@ * @author Kuitos * @since 2023-05-04 */ -import type { BaseLoaderOpts, transpileAssets } from '@qiankunjs/shared'; +import type { BaseLoaderOpts, NodeTransformer } from '@qiankunjs/shared'; import type { Sandbox } from '../../core/sandbox'; export type SandboxConfig = { @@ -10,4 +10,5 @@ export type SandboxConfig = { sandbox: Sandbox; dynamicStyleSheetElements: Array; dynamicExternalSyncScriptElements: HTMLScriptElement[]; -} & BaseLoaderOpts & { nodeTransformer?: typeof transpileAssets }; + nodeTransformer: NodeTransformer; +} & BaseLoaderOpts; diff --git a/packages/sandbox/tsconfig.json b/packages/sandbox/tsconfig.json deleted file mode 100644 index 4082f16a5..000000000 --- a/packages/sandbox/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../tsconfig.json" -} diff --git a/packages/shared/src/assets-transpilers/types.ts b/packages/shared/src/assets-transpilers/types.ts index 8fbdd3122..6d7aad7e2 100644 --- a/packages/shared/src/assets-transpilers/types.ts +++ b/packages/shared/src/assets-transpilers/types.ts @@ -15,6 +15,12 @@ export type BaseTranspilerOpts = BaseLoaderOpts & { export type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node }; +export type NodeTransformer = ( + node: T, + baseURI: string, + opts: Omit, +) => T; + export type ScriptTranspilerOpts = AssetsTranspilerOpts & ( | { prevScriptTranspiledDeferred: Deferred; scriptTranspiledDeferred: Deferred } diff --git a/packages/shared/tsconfig.json b/packages/shared/tsconfig.json deleted file mode 100644 index 4082f16a5..000000000 --- a/packages/shared/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../tsconfig.json" -} diff --git a/packages/ui-bindings/react/tsconfig.json b/packages/ui-bindings/react/tsconfig.json index 014cc6750..d792c6a6a 100644 --- a/packages/ui-bindings/react/tsconfig.json +++ b/packages/ui-bindings/react/tsconfig.json @@ -1,6 +1,9 @@ { "extends": "../../../tsconfig.json", "compilerOptions": { - "jsx": "react" + "jsx": "react", + "paths": { + "qiankun": ["packages/qiankun/src"] + } } } diff --git a/packages/webpack-plugin/plugin.test.ts b/packages/webpack-plugin/tests/plugin.test.ts similarity index 100% rename from packages/webpack-plugin/plugin.test.ts rename to packages/webpack-plugin/tests/plugin.test.ts diff --git a/packages/webpack-plugin/tsconfig.json b/packages/webpack-plugin/tsconfig.json deleted file mode 100644 index 4082f16a5..000000000 --- a/packages/webpack-plugin/tsconfig.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../tsconfig.json" -} diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json index 7f39f5c13..64b9a595c 100644 --- a/tsconfig.eslint.json +++ b/tsconfig.eslint.json @@ -7,7 +7,9 @@ }, "include": [ // whatever paths you intend to lint - "src", + "packages/*/src", + "packages/webpack-plugin/tests", + "packages/ui-bindings/*/src", "vitest.workspace.ts", "vitest.config.ts", ".fatherrc.js" diff --git a/tsconfig.json b/tsconfig.json index 4367f8ffd..f9208ae60 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "baseUrl": "./", "target": "esnext", "module": "esnext", "lib": ["es2018", "dom"], @@ -24,7 +25,8 @@ "esModuleInterop": true, "types": ["vitest/globals", "node"], "paths": { - "@@/*": ["./.dumi/tmp/*"] + "@@/*": ["./.dumi/tmp/*"], + "@qiankunjs/*": ["packages/*/src"] } }, "exclude": ["node_modules", "examples", "packages/**/dist"]