Skip to content

Commit

Permalink
feat(sandbox): add disableLinkTransformToStyle sandbox config (#668)
Browse files Browse the repository at this point in the history
* feat(sandbox): add disableLinkTransformToStyle sandbox config

* chore: fix unit test

* chore: fix unit test

* chore: fix unit test

* chore: fix e2e test

* chore: fix e2e test
  • Loading branch information
zhoushaw authored Jun 19, 2024
1 parent 01cf465 commit c2bebd5
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 55 deletions.
6 changes: 3 additions & 3 deletions cypress/e2e/1-the-whole-process/visit-sub-app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('whole process app render', () => {
},
sandbox: {
snapshot: false,
}
},
},
});

Expand Down Expand Up @@ -148,8 +148,8 @@ describe('whole process app render', () => {
cy.wait(4000);
cy.contains('[data-test=title]', HomeTitle)
.then(() => {
cy.get('[data-test=vite-count-btn]').dblclick();
cy.contains('button', 'count is: 2');
cy.get('[data-test=vite-count-btn]').click({ force: true });
cy.contains('button', 'count is: 1');
})
.then(() => {
win.Garfish.router.push({ path: '/vue2/home' });
Expand Down
1 change: 1 addition & 0 deletions dev/app-main/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const localApps: AppInfo = [
activeWhen: '/react17',
sandbox: {
fixStaticResourceBaseUrl: false,
disableLinkTransformToStyle: false,
},
// 子应用的入口地址,可以为 HTML 地址和 JS 地址
// 注意:entry 地址不可以与主应用+子应用激活地址相同,否则刷新时将会直接返回子应用内容
Expand Down
3 changes: 3 additions & 0 deletions dev/app-react-17/public/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hello {
color: red;
}
3 changes: 3 additions & 0 deletions dev/app-react-17/public/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.world {
color: blue;
}
8 changes: 8 additions & 0 deletions dev/app-react-17/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
<head>
<meta charset="UTF-8">
<title>app react v17</title>
<link rel="stylesheet" href="http://localhost:8091/a.css" />
<script>
let link = document.createElement('link');
link.rel = 'stylesheet';
link.setAttribute('rel','stylesheet');
link.setAttribute('href','http://localhost:8091/b.css');
document.head.appendChild(link);
</script>
</head>
<body>
<img data-test="img-pre-fix-app-17" src="/img" />
Expand Down
8 changes: 4 additions & 4 deletions packages/browser-vm/__tests__/dynamic-link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Sandbox: dynamic link', () => {
[linkOrder3]: {
'Content-Type': styleType,
timeConsuming: 300,
}
},
},
});

Expand Down Expand Up @@ -90,7 +90,7 @@ describe('Sandbox: dynamic link', () => {
go(`
const dynamicLink = document.createElement('link');
dynamicLink.href = "${withSuffix}";
dynamicLink.rel = 'stylesheet';
dynamicLink.setAttribute('rel', 'stylesheet');
dynamicLink.onload = function () {
expect(document.body).toMatchSnapshot();
jestDone();
Expand All @@ -106,7 +106,7 @@ describe('Sandbox: dynamic link', () => {
go(`
const dynamicLink = document.createElement('link');
dynamicLink.href = "${withoutSuffix}";
dynamicLink.rel = 'stylesheet';
dynamicLink.setAttribute('rel', 'stylesheet');
dynamicLink.onload = function () {
expect(document.body).toMatchSnapshot();
jestDone();
Expand All @@ -122,7 +122,7 @@ describe('Sandbox: dynamic link', () => {
go(`
const dynamicLink = document.createElement('link');
dynamicLink.href = "${withoutSuffixAndContentType}";
dynamicLink.rel = 'stylesheet';
dynamicLink.setAttribute('rel', 'stylesheet');
dynamicLink.onload = function () {
expect(document.body).toMatchSnapshot();
jestDone();
Expand Down
7 changes: 5 additions & 2 deletions packages/browser-vm/src/dynamicNode/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,12 @@ export class DynamicNodeProcessor {
this.monitorChangesOfStyle();
}
// The link node of the request css needs to be changed to style node
else if (this.is('link')) {
else if (
this.is('link') &&
this.sandbox.options.disableLinkTransformToStyle !== true
) {
parentNode = this.findParentNodeInApp(context, 'head');
if (this.el.rel === 'stylesheet' && this.el.href) {
if (this.el.getAttribute('rel') === 'stylesheet' && this.el.href) {
convertedNode = this.addDynamicLinkNode((styleNode) => {
this.nativeAppend.call(parentNode, styleNode);
});
Expand Down
3 changes: 3 additions & 0 deletions packages/browser-vm/src/pluginify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ function createOptions(Garfish: interfaces.Garfish) {
fixOwnerDocument: Boolean(appInfo.sandbox?.fixOwnerDocument),
disableWith: Boolean(appInfo.sandbox?.disableWith),
disableElementtiming: Boolean(appInfo.sandbox?.disableElementtiming),
disableLinkTransformToStyle: Boolean(
appInfo.sandbox?.disableLinkTransformToStyle,
),
strictIsolation: Boolean(appInfo.sandbox?.strictIsolation),
excludeAssetFilter: appInfo.sandbox?.excludeAssetFilter,
// 缓存模式,不收集副作用
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class Sandbox {
fixStaticResourceBaseUrl: true,
disableWith: false,
strictIsolation: false,
disableLinkTransformToStyle: false,
disableCollect: false,
el: () => null,
styleScopeId: () => '',
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface SandboxOptions {
disableWith?: boolean;
strictIsolation?: boolean;
disableElementtiming?: boolean;
disableLinkTransformToStyle?: boolean;
disableCollect?: boolean;
modules?: Array<Module>;
excludeAssetFilter?: (url: string) => boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const createDefaultOptions = () => {
disableWith: false,
strictIsolation: false,
disableElementtiming: false,
disableLinkTransformToStyle: false,
fixOwnerDocument: false,
},
// global hooks
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export namespace interfaces {
disableWith?: boolean;
strictIsolation?: boolean;
disableElementtiming?: boolean;
disableLinkTransformToStyle?: boolean;
fixOwnerDocument?: boolean;
excludeAssetFilter?: (url: string) => boolean;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/module/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,14 @@ export class App {
},

link: (node) => {
if (
this.appInfo.sandbox &&
typeof this.appInfo.sandbox === 'object' &&
this.appInfo.sandbox.disableLinkTransformToStyle
) {
return DOMApis.createElement(node);
}

if (DOMApis.isCssLinkNode(node)) {
const styleManager = this.resources.link.find((manager) =>
manager.isSameOrigin(node),
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/module/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { interfaces } from '../interface';

// Fetch `script`, `link` and `module meta` elements
function fetchStaticResources(
appName: string,
appInfo: AppInfo,
loader: Loader,
entryManager: TemplateManager,
sandboxConfig: false | interfaces.SandboxConfig | undefined,
Expand Down Expand Up @@ -50,7 +50,7 @@ function fetchStaticResources(
// we have a preload mechanism, so we don’t need to deal with it.
return loader
.load<JavaScriptManager>({
scope: appName,
scope: appInfo.name,
url: fetchUrl,
crossOrigin,
defaultContentType: type,
Expand All @@ -63,7 +63,7 @@ function fetchStaticResources(
jsManager.setDefferAttribute(toBoolean(defer));
return jsManager;
} else {
warn(`[${appName}] Failed to load script: ${fetchUrl}`);
warn(`[${appInfo.name}] Failed to load script: ${fetchUrl}`);
}
})
.catch(() => null);
Expand All @@ -86,20 +86,26 @@ function fetchStaticResources(
.findAllLinkNodes()
.map((node) => {
if (!entryManager.DOMApis.isCssLinkNode(node)) return;
if (
appInfo.sandbox &&
typeof appInfo.sandbox === 'object' &&
appInfo.sandbox.disableLinkTransformToStyle
)
return;
const href = entryManager.findAttributeValue(node, 'href');
if (href) {
const fetchUrl = entryManager.url
? transformUrl(entryManager.url, href)
: href;
return loader
.load<StyleManager>({ scope: appName, url: fetchUrl })
.load<StyleManager>({ scope: appInfo.name, url: fetchUrl })
.then(({ resourceManager: styleManager }) => {
if (styleManager) {
styleManager.setDep(node);
styleManager?.correctPath();
return styleManager;
} else {
warn(`${appName} Failed to load link: ${fetchUrl}`);
warn(`${appInfo.name} Failed to load link: ${fetchUrl}`);
}
})
.catch(() => null);
Expand Down Expand Up @@ -155,7 +161,7 @@ export async function processAppResources(loader: Loader, appInfo: AppInfo) {
if (entryManager instanceof loader.TemplateManager) {
isHtmlMode = true;
const [js, link, modules] = await fetchStaticResources(
appInfo.name,
appInfo,
loader,
entryManager,
appInfo.sandbox,
Expand Down
Loading

0 comments on commit c2bebd5

Please sign in to comment.