MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / CSSSource

Class CSSSource

packages/core/ui/styling/style-scope.ts:91–282  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89}
90
91class CSSSource {
92 private _selectors: RuleSet[] = [];
93 private _keyframes: Keyframes[] = [];
94
95 private constructor(
96 private _ast: ReworkCSS.SyntaxTree,
97 private _url: string,
98 private _file: string,
99 private _source: string,
100 ) {
101 this.parse();
102 }
103
104 public static fromDetect(cssOrAst: any, fileName?: string): CSSSource {
105 if (typeof cssOrAst === 'string') {
106 // raw-loader
107 return CSSSource.fromSource(cssOrAst, fileName);
108 } else if (typeof cssOrAst === 'object') {
109 if (cssOrAst.default) {
110 cssOrAst = cssOrAst.default;
111 }
112
113 if (cssOrAst.type === 'stylesheet' && cssOrAst.stylesheet && cssOrAst.stylesheet.rules) {
114 // css-loader
115 return CSSSource.fromAST(cssOrAst, fileName);
116 }
117 }
118
119 // css2json-loader
120 return CSSSource.fromSource(cssOrAst.toString(), fileName);
121 }
122
123 public static fromURI(uri: string): CSSSource {
124 // webpack modules require all file paths to be relative to /app folder
125 const appRelativeUri = CSSSource.pathRelativeToApp(uri);
126 const sanitizedModuleName = sanitizeModuleName(appRelativeUri);
127 const resolvedModuleName = resolveModuleName(sanitizedModuleName, 'css');
128
129 try {
130 const cssOrAst = global.loadModule(resolvedModuleName, true);
131 if (cssOrAst) {
132 return CSSSource.fromDetect(cssOrAst, resolvedModuleName);
133 }
134 } catch (e) {
135 if (Trace.isEnabled()) {
136 Trace.write(`Could not load CSS from ${uri}: ${e}`, Trace.categories.Error, Trace.messageType.warn);
137 }
138 }
139
140 return CSSSource.fromFile(appRelativeUri);
141 }
142
143 private static pathRelativeToApp(uri: string): string {
144 if (!uri.startsWith('/')) {
145 return uri;
146 }
147
148 const appPath = knownFolders.currentApp().path;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected