Skip to content

Commit 02b35f1

Browse files
committed
feat: add code
1 parent 7567789 commit 02b35f1

File tree

7 files changed

+23
-15
lines changed

7 files changed

+23
-15
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ function injector(current: Function, methodName: string) {
1919
const el = methodName === 'insertAdjacentElement'
2020
? arguments[1]
2121
: arguments[0];
22-
const sandbox = el && sandboxMap.get(el);
22+
const sandbox = sandboxMap.get(el);
2323
const originProcess = () => current.apply(this, arguments);
2424

25-
if (this?.tagName?.toLowerCase() === 'style') {
25+
if (el && this?.tagName?.toLowerCase() === 'style') {
2626
const { baseUrl, namespace } = sandbox?.options || {};
2727
const manager = new StyleManager(el.textContent);
28-
manager.setScope(namespace);
2928
manager.correctPath(baseUrl);
29+
manager.setAppName(namespace);
3030
el.textContent = manager.transformCode(manager.styleCode);
3131
return originProcess();
3232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ export class DynamicNodeProcessor {
246246
else if (this.is('style')) {
247247
parentNode = this.findParentNodeInApp(context, 'head');
248248
const manager = new StyleManager(this.el.textContent);
249-
manager.setScope(namespace);
250249
manager.correctPath(baseUrl);
250+
manager.setAppName(namespace);
251251
this.el.textContent = manager.transformCode(manager.styleCode);
252252
convertedNode = this.el;
253253
this.sandbox.dynamicStyleSheetElementSet.add(this.el);

packages/core/src/module/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ export class App {
552552
const text = node.children[0] as Text;
553553
if (text) {
554554
const styleManager = new StyleManager(text.content, baseUrl);
555-
styleManager.setScope(this.name);
555+
styleManager.setAppName(this.name);
556556
return entryManager.ignoreChildNodesCreation(
557557
styleManager.renderAsStyleElement(),
558558
);

packages/css-scope/src/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ export interface Options {
88
}
99

1010
export function GarfishCssScope(options: Options = {}) {
11+
const protoCache = new Set<StyleManager>();
12+
const codeCache = new Map<string, string>();
13+
1114
const disable = (appName: string) => {
1215
const { excludes } = options;
1316
if (Array.isArray(excludes)) return excludes.includes(appName);
1417
if (typeof excludes === 'function') return excludes(appName);
1518
return false;
1619
};
1720

18-
const protoCache = new Set();
19-
2021
return function (Garfish: interfaces.Garfish): interfaces.Plugin {
2122
return {
2223
name: 'css-scope',
@@ -28,10 +29,17 @@ export function GarfishCssScope(options: Options = {}) {
2829
protoCache.add(proto);
2930

3031
proto.transformCode = function (code: string) {
31-
if (!code || !this.scope || disable(this.scope)) {
32+
if (!code || !this.appName || disable(this.appName)) {
3233
return code;
34+
} else {
35+
const astNode = parse(code, { source: this.url });
36+
const newCode = stringify(
37+
astNode,
38+
`div[id^=garfish_app_for_${this.appName}_]`,
39+
);
40+
codeCache.set(code, newCode);
41+
return newCode;
3342
}
34-
return '';
3543
};
3644
}
3745
},

packages/css-scope/src/stringify.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Compiler {
6666
: s === 'head'
6767
? '[__GarfishMockHead__]'
6868
: s;
69-
return `#${this.prefix} ${s}`;
69+
return `${this.prefix} ${s}`;
7070
});
7171
}
7272

packages/loader/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class Loader {
173173

174174
// Set css scope
175175
if (fileType === FileTypes.css) {
176-
(resourceManager as StyleManager).setScope(scope);
176+
(resourceManager as StyleManager).setAppName(scope);
177177
}
178178

179179
// The results will be cached this time.

packages/loader/src/managers/style.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const MATCH_CSS_URL = /url\(['"]?([^\)]+?)['"]?\)/g;
66
export class StyleManager {
77
public styleCode: string;
88
public url: string | null;
9-
public scope: string | null;
9+
public appName: string | null;
1010

1111
private depsStack = new Set();
1212

1313
constructor(styleCode: string, url?: string) {
14-
this.scope = null;
14+
this.appName = null;
1515
this.url = url || null;
1616
this.styleCode = styleCode;
1717
}
@@ -37,8 +37,8 @@ export class StyleManager {
3737
this.depsStack.add(node);
3838
}
3939

40-
setScope(scope: string) {
41-
this.scope = scope;
40+
setAppName(appName: string) {
41+
this.appName = appName;
4242
}
4343

4444
isSameOrigin(node: Node) {

0 commit comments

Comments
 (0)