Skip to content

Commit d85cdd4

Browse files
committed
feat: add setScope method on styleManager
1 parent 2aada81 commit d85cdd4

File tree

9 files changed

+64
-30
lines changed

9 files changed

+64
-30
lines changed

dev/app-main/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dependencies": {
55
"garfish": "workspace:*",
66
"@garfish/es-module": "workspace:*",
7+
"@garfish/css-scope": "workspace:*",
78
"babel-runtime": "^6.26.0",
89
"history": "^5.1.0",
910
"mobx": "^6.3.6",

dev/app-main/src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import GarfishInstance from 'garfish';
22
import { GarfishEsModule } from '@garfish/es-module';
3+
import { GarfishCssScope } from '@garfish/css-scope';
34
import { store } from './store';
45
import { basename, localApps } from './constant';
56

@@ -57,7 +58,7 @@ let defaultConfig: RunInfo = {
5758
},
5859

5960
// 插件列表
60-
plugins: [GarfishEsModule()],
61+
plugins: [GarfishEsModule(), GarfishCssScope()],
6162

6263
// 开始加载子应用前触发该函数,支持异步函数,可以在该函数中执行异步操作,
6364
// 当返回 false 时表示中断子应用的加载以及后续流程,所有子应用加载都会触发该函数的调用

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ function injector(current: Function, methodName: string) {
2323
const originProcess = () => current.apply(this, arguments);
2424

2525
if (this?.tagName?.toLowerCase() === 'style') {
26-
const baseUrl = sandbox && sandbox.options.baseUrl;
26+
const { baseUrl, namespace } = sandbox?.options || {};
2727
if (baseUrl) {
2828
const manager = new StyleManager(el.textContent);
29+
manager.setScope(namespace);
2930
manager.correctPath(baseUrl);
3031
this.textContent = manager.styleCode;
3132
return originProcess();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export class DynamicNodeProcessor {
230230
append(context: Element, args: IArguments, originProcess: Function) {
231231
let convertedNode;
232232
let parentNode = context;
233-
const { baseUrl } = this.sandbox.options;
233+
const { baseUrl, namespace } = this.sandbox.options;
234234

235235
// Deal with some static resource nodes
236236
if (sourceListTags.includes(this.tagName)) {
@@ -247,6 +247,7 @@ export class DynamicNodeProcessor {
247247
parentNode = this.findParentNodeInApp(context, 'head');
248248
if (baseUrl) {
249249
const manager = new StyleManager(this.el.textContent);
250+
manager.setScope(namespace);
250251
manager.correctPath(baseUrl);
251252
this.el.textContent = manager.styleCode;
252253
}

packages/core/src/module/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +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);
555556
return entryManager.ignoreChildNodesCreation(
556557
styleManager.renderAsStyleElement(),
557558
);

packages/css-scope/src/index.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
1-
import { warn } from '@garfish/utils';
2-
import { interfaces } from '@garfish/core';
1+
import type { interfaces } from '@garfish/core';
2+
import type { StyleManager } from '@garfish/loader';
33
import { parse } from './parser';
44
import { stringify } from './stringify';
55

6-
export { parse, stringify };
6+
export interface Options {
7+
excludes?: Array<string> | ((name: string) => boolean);
8+
}
9+
10+
const pluginName = 'css-scope';
11+
12+
export function GarfishCssScope(options: Options = {}) {
13+
return function (Garfish: interfaces.Garfish): interfaces.Plugin {
14+
const disable = (appName: string) => {
15+
const { excludes } = options;
16+
if (Array.isArray(excludes)) return excludes.includes(appName);
17+
if (typeof excludes === 'function') return excludes(appName);
18+
return true;
19+
};
720

8-
export default function CssScope() {
9-
return function (_Garfish: interfaces.Garfish): interfaces.Plugin {
10-
let changed = false;
1121
return {
12-
name: 'css-scope',
22+
name: pluginName,
1323
version: __VERSION__,
1424

1525
beforeBootstrap() {
16-
if (changed) return;
17-
changed = true;
18-
// const proto = Garfish.loader.StyleManager.prototype;
19-
20-
// proto.parseStyleCode = function() {
21-
// try {
22-
// this.styleCode = parse(this.styleCode);
23-
// } catch(e) {
24-
// warn(e);
25-
// }
26-
// }
26+
const loader = Garfish.loader;
27+
loader.hooks.usePlugin({
28+
name: pluginName,
2729

28-
// proto.getStyleCode = function() {
29-
// return ''
30-
// }
30+
loaded(data) {
31+
const { scope, fileType } = data.value;
32+
console.log(scope, fileType);
33+
if (fileType === 'css' && !disable(scope)) {
34+
const manager = data.value.resourceManager as StyleManager;
35+
console.log(manager);
36+
}
37+
return data;
38+
},
39+
});
3140
},
3241
};
3342
};

packages/loader/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface CacheValue<T extends Manager> {
3434
url: string;
3535
code: string;
3636
size: number;
37+
scope: string;
3738
fileType: FileTypes | '';
3839
resourceManager: T | null;
3940
}
@@ -167,15 +168,21 @@ export class Loader {
167168

168169
// Use result.url, resources may be redirected
169170
const resourceManager: Manager | null = managerCtor
170-
? new managerCtor(code, result.url)
171+
? new managerCtor(code, result.url, scope)
171172
: null;
172173

174+
// Set css scope
175+
if (fileType === FileTypes.css) {
176+
(resourceManager as StyleManager).setScope(scope);
177+
}
178+
173179
// The results will be cached this time.
174180
// So, you can transform the request result.
175181
const data = this.hooks.lifecycle.loaded.emit({
176182
result,
177183
value: {
178184
url,
185+
scope,
179186
resourceManager,
180187
fileType: fileType || '',
181188
// For performance reasons, take an approximation

packages/loader/src/managers/style.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { Node, isAbsolute, transformUrl } from '@garfish/utils';
44
const MATCH_CSS_URL = /url\(['"]?([^\)]+?)['"]?\)/g;
55

66
export class StyleManager {
7-
public url: string | null;
87
public styleCode: string;
8+
public url: string | null;
9+
public scope: string | null;
910

1011
private depsStack = new Set();
1112

1213
constructor(styleCode: string, url?: string) {
14+
this.scope = null;
1315
this.url = url || null;
1416
this.styleCode = styleCode;
1517
}
@@ -26,27 +28,33 @@ export class StyleManager {
2628
}
2729
}
2830

29-
setScope(_scope: string) {
30-
// Process css cope
31+
// Provided to plugins to override this method
32+
transformCode(code: string) {
33+
return code;
3134
}
3235

3336
setDep(node: Node) {
3437
this.depsStack.add(node);
3538
}
3639

40+
setScope(scope: string) {
41+
this.scope = scope;
42+
}
43+
3744
isSameOrigin(node: Node) {
3845
return this.depsStack.has(node);
3946
}
4047

4148
renderAsStyleElement(extraCode = '') {
4249
const node = document.createElement('style');
43-
node.setAttribute('type', 'text/css');
4450
// prettier-ignore
45-
node.textContent = extraCode + (
51+
const code = extraCode + (
4652
this.styleCode
4753
? this.styleCode
4854
: '/**empty style**/'
4955
);
56+
node.setAttribute('type', 'text/css');
57+
node.textContent = this.transformCode(code);
5058
return node;
5159
}
5260

pnpm-lock.yaml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)