Skip to content

Commit c400d69

Browse files
authored
fix: 全部从 loader 上获取 managers,避免业务打包失误有多份构造函数 (#608)
* fix: get the resource manager from the same entry * fix(core): get the resource manager from the same entry
1 parent 0213eee commit c400d69

File tree

8 files changed

+33
-23
lines changed

8 files changed

+33
-23
lines changed

packages/browser-vm/src/dynamicNode/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { makeMap, safeWrapper, warn } from '@garfish/utils';
2-
import { StyleManager } from '@garfish/loader';
32
import { __domWrapper__ } from '../symbolTypes';
43
import { injectHandlerParams } from './processParams';
54
import { DynamicNodeProcessor, rawElementMethods } from './processor';
@@ -33,7 +32,8 @@ function injector(current: Function, methodName: string) {
3332

3433
if (sandbox) {
3534
if (el && this?.tagName?.toLowerCase() === 'style') {
36-
const manager = new StyleManager(el.textContent);
35+
// We take it from the loader, avoid having multiple manager constructors
36+
const manager = new sandbox.loader.StyleManager(el.textContent);
3737
const { baseUrl, namespace, styleScopeId } = sandbox.options;
3838
manager.correctPath(baseUrl);
3939
manager.setScope({
@@ -117,7 +117,7 @@ export function makeElInjector(sandboxConfig: SandboxOptions) {
117117

118118
if (typeof window.Element === 'function') {
119119
// iframe can read html container this can't point to proxyDocument has Illegal invocation error
120-
if (sandboxConfig.fixBaseUrl) safeWrapper(()=> handleOwnerDocument());
120+
if (sandboxConfig.fixBaseUrl) safeWrapper(() => handleOwnerDocument());
121121
const rewrite = (
122122
methods: Array<string>,
123123
builder: typeof injector | typeof injectorRemoveChild,

packages/browser-vm/src/dynamicNode/processor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StyleManager, JavaScriptManager } from '@garfish/loader';
1+
import type { StyleManager, JavaScriptManager } from '@garfish/loader';
22
import {
33
def,
44
warn,
@@ -235,7 +235,7 @@ export class DynamicNodeProcessor {
235235

236236
const modifyStyleCode = (styleCode: string | null) => {
237237
if (styleCode) {
238-
const manager = new StyleManager(styleCode);
238+
const manager = new this.sandbox.loader.StyleManager(styleCode);
239239
manager.correctPath(baseUrl);
240240
if (rootElId) {
241241
manager.setScope({
@@ -325,7 +325,8 @@ export class DynamicNodeProcessor {
325325
// The style node needs to be placed in the sandbox root container
326326
else if (this.is('style')) {
327327
parentNode = this.findParentNodeInApp(context, 'head');
328-
const manager = new StyleManager(this.el.textContent);
328+
// We take it from the loader, avoid having multiple manager constructors
329+
const manager = new this.sandbox.loader.StyleManager(this.el.textContent);
329330
manager.correctPath(baseUrl);
330331
if (styleScopeId) {
331332
manager.setScope({

packages/core/src/module/app.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StyleManager, TemplateManager } from '@garfish/loader';
1+
import type { TemplateManager } from '@garfish/loader';
22
import {
33
Text,
44
Node,
@@ -89,7 +89,9 @@ export class App {
8989
public customLoader?: CustomerLoader;
9090
public childGarfishConfig: interfaces.ChildGarfishConfig = {};
9191
public asyncProviderTimeout: number;
92-
private asyncProvider?: interfaces.Provider | ((...args: any[]) => interfaces.Provider);
92+
private asyncProvider?:
93+
| interfaces.Provider
94+
| ((...args: any[]) => interfaces.Provider);
9395
private resolveAsyncProvider: () => void | undefined;
9496
// private
9597
private active = false;
@@ -215,7 +217,9 @@ export class App {
215217

216218
if (asyncProviderTimeout) {
217219
// just inject 'registerProvider' function for async provider registration
218-
customExports.registerProvider = (provider: typeof this.asyncProvider) => {
220+
customExports.registerProvider = (
221+
provider: typeof this.asyncProvider,
222+
) => {
219223
this.asyncProvider = provider;
220224
// resolve it immediately
221225
this.resolveAsyncProvider?.();
@@ -224,7 +228,7 @@ export class App {
224228
}
225229

226230
awaitAsyncProviderRegistration() {
227-
return new Promise<typeof this.asyncProvider>(resolve => {
231+
return new Promise<typeof this.asyncProvider>((resolve) => {
228232
if (this.asyncProvider) {
229233
resolve(this.asyncProvider);
230234
return;
@@ -697,7 +701,10 @@ export class App {
697701
style: (node) => {
698702
const text = node.children[0] as Text;
699703
if (text) {
700-
const styleManager = new StyleManager(text.content, baseUrl);
704+
const styleManager = new this.context.loader.StyleManager(
705+
text.content,
706+
baseUrl,
707+
);
701708
styleManager.setScope({
702709
appName: this.name,
703710
rootElId: this.appContainer.id,

packages/core/src/module/resource.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { warn, error, Text, transformUrl, assert } from '@garfish/utils';
2-
import {
2+
import type {
33
Loader,
44
StyleManager,
55
TemplateManager,
@@ -59,7 +59,7 @@ function fetchStaticResources(
5959
} else if (node.children.length > 0) {
6060
const code = (node.children[0] as Text).content;
6161
if (code) {
62-
const jsManager = new JavaScriptManager(code, '');
62+
const jsManager = new loader.JavaScriptManager(code, '');
6363
jsManager.setDep(node);
6464
type && jsManager.setMimeType(type);
6565
return jsManager;
@@ -141,7 +141,7 @@ export async function processAppResources(loader: Loader, appInfo: AppInfo) {
141141
});
142142

143143
// Html entry
144-
if (entryManager instanceof TemplateManager) {
144+
if (entryManager instanceof loader.TemplateManager) {
145145
isHtmlMode = true;
146146
const [js, link, modules] = await fetchStaticResources(
147147
appInfo.name,
@@ -151,11 +151,14 @@ export async function processAppResources(loader: Loader, appInfo: AppInfo) {
151151
resources.js = js;
152152
resources.link = link;
153153
resources.modules = modules;
154-
} else if (entryManager instanceof JavaScriptManager) {
154+
} else if (entryManager instanceof loader.JavaScriptManager) {
155155
// Js entry
156156
isHtmlMode = false;
157157
const mockTemplateCode = `<script src="${entryManager.url}"></script>`;
158-
fakeEntryManager = new TemplateManager(mockTemplateCode, entryManager.url);
158+
fakeEntryManager = new loader.TemplateManager(
159+
mockTemplateCode,
160+
entryManager.url,
161+
);
159162
entryManager.setDep(fakeEntryManager.findAllJsNodes()[0]);
160163
resources.js = [entryManager];
161164
} else {

packages/loader/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,29 +212,29 @@ export class Loader {
212212

213213
if (isRemoteModule) {
214214
fileType = FileTypes.module;
215-
managerCtor = ModuleManager;
215+
managerCtor = this.ModuleManager;
216216
} else if (
217217
isHtmlType({ type, src: result.url }) ||
218218
isHtmlType({
219219
type: defaultContentType,
220220
})
221221
) {
222222
fileType = FileTypes.template;
223-
managerCtor = TemplateManager;
223+
managerCtor = this.TemplateManager;
224224
} else if (
225225
isJsType({ type: defaultContentType }) ||
226226
isJsType({ type, src: result.url })
227227
) {
228228
fileType = FileTypes.js;
229-
managerCtor = JavaScriptManager;
229+
managerCtor = this.JavaScriptManager;
230230
} else if (
231231
isCssType({ src: result.url, type }) ||
232232
isCssType({
233233
type: defaultContentType,
234234
})
235235
) {
236236
fileType = FileTypes.css;
237-
managerCtor = StyleManager;
237+
managerCtor = this.StyleManager;
238238
}
239239

240240
// Use result.url, resources may be redirected

packages/remote-module/src/actuator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { evalWithEnv } from '@garfish/utils';
2-
import { ModuleManager } from '@garfish/loader';
2+
import type { ModuleManager } from '@garfish/loader';
33
import { hooks } from './hooks';
44
import { currentApp, moduleConfig } from './common';
55

packages/remote-module/src/apis/loadModuleSync.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { error, assert, isPromise, isAbsolute } from '@garfish/utils';
22
import {
3-
ModuleInfo,
43
cacheModules,
54
purifyOptions,
65
prettifyError,

packages/remote-module/src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ safeWrapper(() => {
5555
currentApp = app;
5656
}
5757
if (isObject(externals)) {
58-
Object.assign(moduleConfig.externals, externals);
58+
Object.assign(moduleConfig.externals!, externals);
5959
}
6060
if (Array.isArray(remoteModulesCode)) {
6161
resourcesStore = resourcesStore.concat(remoteModulesCode);

0 commit comments

Comments
 (0)