Skip to content

Commit 9259248

Browse files
committed
feat: add css scope plugin
1 parent 1a07036 commit 9259248

File tree

8 files changed

+52
-40
lines changed

8 files changed

+52
-40
lines changed

packages/runtime/core/src/interface/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export namespace interfaces {
3838
name: string;
3939
entry: string;
4040
cache?: boolean; // Whether the cache
41-
cssScope?: boolean;
4241
activeWhen?: string | ((path: string) => boolean);
4342
hooks?: Hooks;
4443
}
@@ -85,7 +84,10 @@ export namespace interfaces {
8584

8685
export interface Config {
8786
appID?: string;
87+
nested?: boolean;
8888
basename?: string;
89+
cssScope?: boolean;
90+
domGetter?: DomGetter;
8991
apps?: Array<AppInfo>;
9092
sandbox?: SandboxConfig;
9193
plugins?: Array<(context: Garfish) => Plugin>;
@@ -94,8 +96,6 @@ export namespace interfaces {
9496
disablePreloadApp?: boolean;
9597
// onNotMatchRouter?: (path: string) => Promise<void> | void;
9698
// autoRefreshApp?: boolean;
97-
domGetter?: DomGetter;
98-
nested?: boolean;
9999
}
100100

101101
export declare type MountLifeCycleFn = (

packages/runtime/core/src/module/app.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ export class App {
475475
const text = node.children[0] as Text;
476476
if (text) {
477477
const styleManager = new StyleManager(text.content, baseUrl);
478-
styleManager.setScope(this.name);
479478
return styleManager.renderAsStyleElement();
480479
}
481480
return DOMApis.createElement(node);

packages/runtime/core/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export const fetchStaticResources = (
100100
.load<StyleManager>(appName, fetchUrl)
101101
.then(({ resourceManager: styleManager }) => {
102102
styleManager.setDep(node);
103-
styleManager.setScope(appName);
104103
return styleManager;
105104
});
106105
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1+
import { interfaces } from '@garfish/core';
2+
import { StyleManager } from '@garfish/loader';
13
import { parse } from './parser';
24
import { stringify } from './stringify';
35

46
export { parse, stringify };
7+
8+
export default function CssScope() {
9+
return function (Garfish: interfaces.Garfish): interfaces.Plugin {
10+
return {
11+
name: 'css-scope',
12+
version: __VERSION__,
13+
14+
beforeBootstrap() {
15+
Garfish.loader.lifecycle.loaded.add((data) => {
16+
if (data.value.fileType === 'css') {
17+
const manager = data.value.resourceManager as StyleManager;
18+
console.log(manager);
19+
}
20+
return data;
21+
});
22+
},
23+
};
24+
};
25+
}

packages/runtime/css-scope/src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface CssParserOptions {
1010
silent?: boolean;
1111
}
1212

13-
// 1M 文本耗时约 150ms
13+
// 1M text takes about 150ms
1414
export function parse(css: string, options?: CssParserOptions) {
1515
options = options || {};
1616
let line = 1;

packages/runtime/garfish/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Garfish } from '@garfish/core';
22
import GarfishRouter from '@garfish/router';
3+
import GarfishCssScope from '@garfish/css-scope';
34
import GarfishBrowserVm from '@garfish/browser-vm';
45
import GarfishBrowserSnapshot from '@garfish/browser-snapshot';
56
import {
@@ -51,7 +52,12 @@ export function createContext(): Garfish {
5152
errorMountApp: (err) => error(err),
5253
errorUnmountApp: (err) => error(err),
5354
onNotMatchRouter: () => {},
54-
plugins: [GarfishRouter(), GarfishBrowserVm(), GarfishBrowserSnapshot()],
55+
plugins: [
56+
GarfishRouter(),
57+
GarfishCssScope(),
58+
GarfishBrowserVm(),
59+
GarfishBrowserSnapshot(),
60+
],
5561
});
5662

5763
type globalValue = boolean | Garfish | Record<string, unknown>;

packages/runtime/loader/src/managers/style.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import { warn, Node, isAbsolute, transformUrl } from '@garfish/utils';
44
// Match url in css
55
const MATCH_CSS_URL = /url\(['"]?([^\)]+?)['"]?\)/g;
66

7+
const correctPath = (manager) => {
8+
const { url, styleCode } = manager;
9+
if (url && typeof styleCode === 'string') {
10+
// The relative path is converted to an absolute path according to the path of the css file
11+
manager.styleCode = styleCode.replace(MATCH_CSS_URL, (k1, k2) => {
12+
if (isAbsolute(k2)) return k1;
13+
return `url("${transformUrl(url, k2)}")`;
14+
});
15+
}
16+
};
17+
718
export class StyleManager {
819
public url: string | null;
920
public styleCode: string;
@@ -15,32 +26,7 @@ export class StyleManager {
1526
this.url = url || null;
1627
this.scopeString = '';
1728
this.styleCode = styleCode;
18-
this.correctPath();
19-
}
20-
21-
private correctPath() {
22-
const { url, styleCode } = this;
23-
if (url && typeof styleCode === 'string') {
24-
// The relative path is converted to an absolute path according to the path of the css file
25-
this.styleCode = styleCode.replace(MATCH_CSS_URL, (k1, k2) => {
26-
if (isAbsolute(k2)) return k1;
27-
return `url("${transformUrl(url, k2)}")`;
28-
});
29-
}
30-
}
31-
32-
setScope(_scope: string) {
33-
// if (scope !== this.scopeString) {
34-
// try {
35-
// const astTree = parse(this.styleCode, {
36-
// source: `Css parser: ${this.url}`,
37-
// });
38-
// this.styleCode = stringify(astTree, scope);
39-
// this.scopeString = scope;
40-
// } catch (err) {
41-
// warn(err);
42-
// }
43-
// }
29+
correctPath(this);
4430
}
4531

4632
setDep(node: Node) {
@@ -51,15 +37,16 @@ export class StyleManager {
5137
return this.depsStack.has(node);
5238
}
5339

40+
getStyleCode() {
41+
return this.styleCode;
42+
}
43+
5444
renderAsStyleElement(extraCode = '') {
45+
const styleCode = this.getStyleCode();
5546
const node = document.createElement('style');
5647
node.setAttribute('type', 'text/css');
57-
// prettier-ignore
58-
node.textContent = extraCode + (
59-
this.styleCode
60-
? this.styleCode
61-
: '/**empty style**/'
62-
);
48+
node.textContent =
49+
extraCode + (styleCode ? styleCode : '/**empty style**/');
6350
return node;
6451
}
6552

packages/runtime/loader/src/pluginSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class PluginManager<T> {
4848
const tempResult = fn(result as any);
4949
if (fn._onceFlag === ONCE_FLAG) this.remove(fn);
5050
for (const key in result) {
51-
if (!hasOwn(key, tempResult)) {
51+
if (!hasOwn(tempResult, key)) {
5252
illegalResult = true;
5353
break;
5454
}

0 commit comments

Comments
 (0)