@master/css-compiler 2.0.0-rc.73 → 2.0.0-rc.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/browser.d.ts +23 -19
- package/dist/browser.js +42 -0
- package/dist/core.d.ts +43 -41
- package/dist/core.js +1639 -0
- package/dist/css-transform.d.ts +10 -6
- package/dist/css-transform.js +17 -0
- package/dist/diagnostics.d.ts +11 -0
- package/dist/diagnostics.js +12 -0
- package/dist/index.d.ts +41 -38
- package/dist/index.js +330 -0
- package/dist/lexer/imports.d.ts +16 -12
- package/dist/lexer/imports.js +38 -0
- package/dist/lexer/source.d.ts +6 -3
- package/dist/lexer/source.js +8 -0
- package/dist/lexer/standalone-master.d.ts +23 -19
- package/dist/lexer/standalone-master.js +219 -0
- package/dist/lower-css-directives.d.ts +18 -13
- package/dist/lower-css-directives.js +753 -0
- package/dist/master-css-manifest.d.ts +18 -12
- package/dist/master-css-manifest.js +546 -0
- package/dist/native-declaration.d.ts +6 -2
- package/dist/native-declaration.js +23 -0
- package/dist/utils/selectors.d.ts +6 -3
- package/dist/utils/selectors.js +49 -0
- package/dist/utils/unquote.d.ts +4 -1
- package/dist/utils/unquote.js +9 -0
- package/dist/utils/wrap-at-rules.d.ts +4 -1
- package/dist/utils/wrap-at-rules.js +11 -0
- package/package.json +69 -1
- package/dist/browser.mjs +0 -47
- package/dist/core.mjs +0 -2146
- package/dist/css-transform.mjs +0 -18
- package/dist/index.mjs +0 -395
- package/dist/lexer/imports.mjs +0 -44
- package/dist/lexer/source.mjs +0 -9
- package/dist/lexer/standalone-master.mjs +0 -233
- package/dist/lower-css-directives.mjs +0 -969
- package/dist/master-css-manifest.mjs +0 -760
- package/dist/native-declaration.mjs +0 -25
- package/dist/utils/selectors.mjs +0 -55
- package/dist/utils/unquote.mjs +0 -10
- package/dist/utils/wrap-at-rules.mjs +0 -10
package/dist/css-transform.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import * as LightningCSS from "lightningcss";
|
|
2
|
+
|
|
3
|
+
//#region src/css-transform.d.ts
|
|
4
|
+
type CSSTransform = typeof LightningCSS.transform;
|
|
5
|
+
declare function setCSSTransform(transformer: CSSTransform): void;
|
|
6
|
+
declare function getCSSTransform(): typeof LightningCSS.transform;
|
|
7
|
+
declare function encodeCSS(source: string): Uint8Array<ArrayBuffer>;
|
|
8
|
+
declare function decodeCSS(code: Uint8Array): string;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { CSSTransform, decodeCSS, encodeCSS, getCSSTransform, setCSSTransform };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/css-transform.ts
|
|
2
|
+
let transform;
|
|
3
|
+
function setCSSTransform(transformer) {
|
|
4
|
+
transform = transformer;
|
|
5
|
+
}
|
|
6
|
+
function getCSSTransform() {
|
|
7
|
+
if (!transform) throw new Error("@master/css-compiler requires a CSS transform implementation. Use the default Node entry or initialize the browser entry.");
|
|
8
|
+
return transform;
|
|
9
|
+
}
|
|
10
|
+
function encodeCSS(source) {
|
|
11
|
+
return new TextEncoder().encode(source);
|
|
12
|
+
}
|
|
13
|
+
function decodeCSS(code) {
|
|
14
|
+
return new TextDecoder().decode(code);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { decodeCSS, encodeCSS, getCSSTransform, setCSSTransform };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/diagnostics.d.ts
|
|
2
|
+
interface CompilerDiagnosticRecorder {
|
|
3
|
+
time<T>(metricId: string, callback: () => T): T;
|
|
4
|
+
addCount(metricId: string, value?: number): void;
|
|
5
|
+
setCount(metricId: string, value: number): void;
|
|
6
|
+
}
|
|
7
|
+
declare function timeCompilerDiagnostic<T>(diagnostics: CompilerDiagnosticRecorder | undefined, metricId: string, callback: () => T): T;
|
|
8
|
+
declare function addCompilerDiagnosticCount(diagnostics: CompilerDiagnosticRecorder | undefined, metricId: string, value?: number): void;
|
|
9
|
+
declare function setCompilerDiagnosticCount(diagnostics: CompilerDiagnosticRecorder | undefined, metricId: string, value: number): void;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { CompilerDiagnosticRecorder, addCompilerDiagnosticCount, setCompilerDiagnosticCount, timeCompilerDiagnostic };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/diagnostics.ts
|
|
2
|
+
function timeCompilerDiagnostic(diagnostics, metricId, callback) {
|
|
3
|
+
return diagnostics ? diagnostics.time(metricId, callback) : callback();
|
|
4
|
+
}
|
|
5
|
+
function addCompilerDiagnosticCount(diagnostics, metricId, value = 1) {
|
|
6
|
+
diagnostics?.addCount(metricId, value);
|
|
7
|
+
}
|
|
8
|
+
function setCompilerDiagnosticCount(diagnostics, metricId, value) {
|
|
9
|
+
diagnostics?.setCount(metricId, value);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { addCompilerDiagnosticCount, setCompilerDiagnosticCount, timeCompilerDiagnostic };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,46 +1,49 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
export * from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { MasterCSSManifest } from "@master/css-schema/manifest";
|
|
2
|
+
import { CSSReferenceStatement, CSSReferenceStatement as CSSReferenceStatement$1, CompileCSSFileOptions, CompileCSSFileOptions as CompileCSSFileOptions$1, CompileCSSOptions, CompileCSSOptions as CompileCSSOptions$1, CompileCSSResult, CompileCSSResult as CompileCSSResult$1, ResolvedCSSImportGraph, ResolvedCSSImportGraph as ResolvedCSSImportGraph$1, StandaloneCSSDirectiveStatement, StandaloneMasterDirectiveStatement } from "./core.js";
|
|
3
|
+
export * from "./core.js";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
interface InspectCSSResult {
|
|
7
|
+
hasMasterEntryDirective: boolean;
|
|
8
|
+
hasMasterCSSImport: boolean;
|
|
9
|
+
hasMasterEntry: boolean;
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
interface ResolveCSSImportGraphOptions {
|
|
12
|
+
projectDir?: string;
|
|
13
|
+
expandPackageImports?: boolean;
|
|
14
|
+
onReference?: (reference: CSSReferenceStatement$1, fromFile: string) => void;
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
type CompileCSSManifestOptions = CompileCSSFileOptions$1 & {
|
|
17
|
+
baseManifest?: MasterCSSManifest;
|
|
17
18
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
type CompileCSSManifestSourceOptions = CompileCSSOptions$1 & {
|
|
20
|
+
baseManifest?: MasterCSSManifest;
|
|
21
|
+
root?: string;
|
|
21
22
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
interface CompileCSSManifestResult extends Omit<CompileCSSResult$1, 'manifestInput'> {
|
|
24
|
+
manifest: MasterCSSManifest;
|
|
25
|
+
directives: CompileCSSResult$1;
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
interface CompileProjectManifestResult extends CompileCSSManifestResult {
|
|
28
|
+
entries: string[];
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
type CompileCSSManifestJSONResult = CompileCSSManifestResult & {
|
|
31
|
+
json: string;
|
|
32
|
+
directives: CompileCSSResult$1;
|
|
32
33
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
declare function resolveMasterCSSPackageEntryFile(importSource: string, fromFile?: string, projectDir?: string): string | undefined;
|
|
35
|
+
declare function inspectCSS(source: string): InspectCSSResult;
|
|
36
|
+
declare function resolveCSSImportGraph(file: string, options?: ResolveCSSImportGraphOptions): ResolvedCSSImportGraph$1;
|
|
37
|
+
declare function resolveMasterCSSPackageImportGraph(projectDir?: string): ResolvedCSSImportGraph$1;
|
|
38
|
+
declare function isMasterCSSPackageStyleFile(id: string, projectDir?: string): boolean;
|
|
39
|
+
declare function compileCSSFile(file: string, options?: CompileCSSFileOptions$1): CompileCSSResult$1;
|
|
40
|
+
declare function createManifestFromCSSResult(result: CompileCSSResult$1, options?: CompileCSSManifestSourceOptions): CompileCSSManifestResult;
|
|
41
|
+
declare function compileCSSManifest(source: string, options?: CompileCSSManifestSourceOptions): CompileCSSManifestResult;
|
|
42
|
+
declare function compileCSSManifestFile(file: string, options?: CompileCSSManifestOptions): CompileCSSManifestResult;
|
|
43
|
+
declare function compileProjectManifest(entries: string[], options?: CompileCSSManifestOptions): CompileProjectManifestResult;
|
|
44
|
+
declare function compileCSSManifestJSON(file: string, options?: CompileCSSManifestOptions): CompileCSSManifestJSONResult;
|
|
45
|
+
declare function compileProjectManifestJSON(entries: string[], options?: CompileCSSManifestOptions): CompileProjectManifestResult & {
|
|
46
|
+
json: string;
|
|
46
47
|
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { type CSSReferenceStatement, type CompileCSSFileOptions, CompileCSSManifestJSONResult, CompileCSSManifestOptions, CompileCSSManifestResult, CompileCSSManifestSourceOptions, type CompileCSSOptions, type CompileCSSResult, CompileProjectManifestResult, InspectCSSResult, ResolveCSSImportGraphOptions, type ResolvedCSSImportGraph, type StandaloneCSSDirectiveStatement, type StandaloneMasterDirectiveStatement, compileCSSFile, compileCSSManifest, compileCSSManifestFile, compileCSSManifestJSON, compileProjectManifest, compileProjectManifestJSON, createManifestFromCSSResult, inspectCSS, isMasterCSSPackageStyleFile, resolveCSSImportGraph, resolveMasterCSSPackageEntryFile, resolveMasterCSSPackageImportGraph };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { setCSSTransform } from "./css-transform.js";
|
|
2
|
+
import { collectStandaloneCSSDirectiveExtractionPolicy, createCSSDirectiveExtractionPolicy, findStandaloneCSSDirectiveStatements, findStandaloneMasterDirectiveStatements, mergeCSSDirectiveExtractionPolicy, removeStandaloneCSSDirectives, removeStandaloneMasterDirectives } from "./lexer/standalone-master.js";
|
|
3
|
+
import { compileCSS, findCSSReferenceStatements, maskManagedPatternEntryNames, parseDirectives, removeCSSReferenceStatements } from "./core.js";
|
|
4
|
+
import { createMasterCSSManifest } from "./master-css-manifest.js";
|
|
5
|
+
import lowerCSSDirectives from "./lower-css-directives.js";
|
|
6
|
+
import { findCSSImportStatements, removeCSSImportStatements, replaceCSSImportStatements } from "./lexer/imports.js";
|
|
7
|
+
import { createRequire } from "node:module";
|
|
8
|
+
import { MASTER_CSS_ENTRY_DIRECTIVE_NAME } from "@master/css-lexer";
|
|
9
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
10
|
+
import { dirname, extname, isAbsolute, resolve } from "node:path";
|
|
11
|
+
import { transform } from "lightningcss";
|
|
12
|
+
import { stringifyMasterCSSManifestJSON } from "@master/css-schema/manifest-json";
|
|
13
|
+
//#region src/index.ts
|
|
14
|
+
setCSSTransform(transform);
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
const MASTER_CSS_PACKAGE_ID = "@master/css";
|
|
17
|
+
const MASTER_CSS_PACKAGE_IDS = /* @__PURE__ */ new Set([MASTER_CSS_PACKAGE_ID, "@master/css-preset"]);
|
|
18
|
+
function isExpandableImportSource(source) {
|
|
19
|
+
return (source.startsWith("./") || source.startsWith("../")) && extname(source) === ".css";
|
|
20
|
+
}
|
|
21
|
+
function readJSONFile(file) {
|
|
22
|
+
return JSON.parse(readFileSync(file, "utf-8"));
|
|
23
|
+
}
|
|
24
|
+
function findPackageRoot(entryFile, packageName) {
|
|
25
|
+
let directory = dirname(entryFile);
|
|
26
|
+
while (true) {
|
|
27
|
+
const packageJSONFile = resolve(directory, "package.json");
|
|
28
|
+
if (existsSync(packageJSONFile)) try {
|
|
29
|
+
const packageJSON = readJSONFile(packageJSONFile);
|
|
30
|
+
if (packageJSON.name === packageName) return {
|
|
31
|
+
directory,
|
|
32
|
+
packageJSON
|
|
33
|
+
};
|
|
34
|
+
} catch {}
|
|
35
|
+
const parentDirectory = dirname(directory);
|
|
36
|
+
if (parentDirectory === directory) return;
|
|
37
|
+
directory = parentDirectory;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function getPackageStyleEntry(packageJSON) {
|
|
41
|
+
if (typeof packageJSON.style === "string") return packageJSON.style;
|
|
42
|
+
if (!packageJSON.exports || typeof packageJSON.exports !== "object") return;
|
|
43
|
+
const rootExport = packageJSON.exports["."];
|
|
44
|
+
if (!rootExport || typeof rootExport !== "object") return;
|
|
45
|
+
const styleExport = rootExport.style;
|
|
46
|
+
return typeof styleExport === "string" ? styleExport : void 0;
|
|
47
|
+
}
|
|
48
|
+
function createProjectRequire(fromFile, projectDir) {
|
|
49
|
+
return createRequire(resolve(projectDir || dirname(fromFile), "package.json"));
|
|
50
|
+
}
|
|
51
|
+
function resolveMasterCSSPackageEntryFile(importSource, fromFile = process.cwd(), projectDir) {
|
|
52
|
+
if (!MASTER_CSS_PACKAGE_IDS.has(importSource)) return;
|
|
53
|
+
const resolver = createProjectRequire(fromFile, projectDir);
|
|
54
|
+
let packageEntryFile;
|
|
55
|
+
try {
|
|
56
|
+
packageEntryFile = resolver.resolve(importSource);
|
|
57
|
+
} catch {
|
|
58
|
+
packageEntryFile = require.resolve(importSource);
|
|
59
|
+
}
|
|
60
|
+
const packageRoot = findPackageRoot(packageEntryFile, importSource);
|
|
61
|
+
if (!packageRoot) return;
|
|
62
|
+
const styleEntry = getPackageStyleEntry(packageRoot.packageJSON);
|
|
63
|
+
if (!styleEntry) return;
|
|
64
|
+
const styleFile = resolve(packageRoot.directory, styleEntry);
|
|
65
|
+
if (!existsSync(styleFile)) throw new Error(`${importSource} CSS style entry was not found: ${styleFile}`);
|
|
66
|
+
return styleFile;
|
|
67
|
+
}
|
|
68
|
+
function inspectCSS(source) {
|
|
69
|
+
const hasMasterEntryDirective = findStandaloneMasterDirectiveStatements(source).some((statement) => statement.name === MASTER_CSS_ENTRY_DIRECTIVE_NAME);
|
|
70
|
+
const hasMasterCSSImport = findCSSImportStatements(source).some((statement) => statement.source === MASTER_CSS_PACKAGE_ID);
|
|
71
|
+
return {
|
|
72
|
+
hasMasterEntryDirective,
|
|
73
|
+
hasMasterCSSImport,
|
|
74
|
+
hasMasterEntry: hasMasterEntryDirective || hasMasterCSSImport
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function resolveCSSImportGraphFile(file, dependencies, dependencySet, stack, options = {}) {
|
|
78
|
+
const absoluteFile = resolve(file);
|
|
79
|
+
if (stack.includes(absoluteFile)) throw new Error(`Circular CSS import: ${[...stack, absoluteFile].join(" -> ")}`);
|
|
80
|
+
if (!existsSync(absoluteFile)) throw new Error(`CSS manifest entry file not found: ${absoluteFile}`);
|
|
81
|
+
if (!dependencySet.has(absoluteFile)) {
|
|
82
|
+
dependencySet.add(absoluteFile);
|
|
83
|
+
dependencies.push(absoluteFile);
|
|
84
|
+
}
|
|
85
|
+
const source = readFileSync(absoluteFile, "utf-8");
|
|
86
|
+
const references = findCSSReferenceStatements(source, absoluteFile);
|
|
87
|
+
for (const reference of references) options.onReference?.(reference, absoluteFile);
|
|
88
|
+
const sourceWithoutReferences = removeCSSReferenceStatements(source, absoluteFile);
|
|
89
|
+
const imports = findCSSImportStatements(sourceWithoutReferences, absoluteFile);
|
|
90
|
+
if (!imports.length) return sourceWithoutReferences;
|
|
91
|
+
const preservedImports = [];
|
|
92
|
+
const resolvedSource = replaceCSSImportStatements(sourceWithoutReferences, absoluteFile, (importStatement) => {
|
|
93
|
+
const importSource = importStatement.source;
|
|
94
|
+
const packageFile = options.expandPackageImports !== false ? resolveMasterCSSPackageEntryFile(importSource, absoluteFile, options.projectDir) : void 0;
|
|
95
|
+
if (packageFile || isExpandableImportSource(importSource)) return resolveCSSImportGraphFile(packageFile || resolve(dirname(absoluteFile), importSource), dependencies, dependencySet, [...stack, absoluteFile], options);
|
|
96
|
+
preservedImports.push(importStatement.statement.trim());
|
|
97
|
+
return "";
|
|
98
|
+
});
|
|
99
|
+
if (!preservedImports.length) return resolvedSource;
|
|
100
|
+
const firstImportStart = imports[0].start;
|
|
101
|
+
const afterImports = resolvedSource.slice(firstImportStart);
|
|
102
|
+
return resolvedSource.slice(0, firstImportStart) + preservedImports.join("\n") + (afterImports ? "\n" : "") + afterImports;
|
|
103
|
+
}
|
|
104
|
+
function resolveCSSImportGraph(file, options = {}) {
|
|
105
|
+
const dependencies = [];
|
|
106
|
+
const references = [];
|
|
107
|
+
return {
|
|
108
|
+
source: resolveCSSImportGraphFile(file, dependencies, /* @__PURE__ */ new Set(), [], {
|
|
109
|
+
...options,
|
|
110
|
+
onReference(reference, fromFile) {
|
|
111
|
+
references.push(reference);
|
|
112
|
+
options.onReference?.(reference, fromFile);
|
|
113
|
+
}
|
|
114
|
+
}),
|
|
115
|
+
dependencies,
|
|
116
|
+
...references.length ? { references } : {}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function resolveMasterCSSPackageImportGraph(projectDir) {
|
|
120
|
+
const entry = resolveMasterCSSPackageEntryFile(MASTER_CSS_PACKAGE_ID, projectDir || process.cwd(), projectDir);
|
|
121
|
+
if (!entry) throw new Error(`Cannot resolve ${MASTER_CSS_PACKAGE_ID} CSS entry.`);
|
|
122
|
+
return resolveCSSImportGraph(entry, { projectDir });
|
|
123
|
+
}
|
|
124
|
+
function stripRequest(id) {
|
|
125
|
+
return id.replace(/[?#].*$/, "");
|
|
126
|
+
}
|
|
127
|
+
function resolveComparablePath(file) {
|
|
128
|
+
const filename = resolve(file);
|
|
129
|
+
try {
|
|
130
|
+
return realpathSync(filename);
|
|
131
|
+
} catch {
|
|
132
|
+
return filename;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function isMasterCSSPackageStyleFile(id, projectDir) {
|
|
136
|
+
const filename = resolveComparablePath(stripRequest(id));
|
|
137
|
+
if (extname(filename) !== ".css") return false;
|
|
138
|
+
try {
|
|
139
|
+
return resolveMasterCSSPackageImportGraph(projectDir).dependencies.some((dependency) => {
|
|
140
|
+
return resolveComparablePath(dependency) === filename;
|
|
141
|
+
});
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function compileCSSFile(file, options = {}) {
|
|
147
|
+
const { root, ...compileOptions } = options;
|
|
148
|
+
const absoluteFile = isAbsolute(file) ? file : resolve(root || "", file);
|
|
149
|
+
const graph = resolveCSSImportGraph(absoluteFile, { projectDir: root });
|
|
150
|
+
return {
|
|
151
|
+
...compileCSS(compileOptions.preserveNativeCSS === false ? removeCSSImportStatements(graph.source, absoluteFile) : graph.source, {
|
|
152
|
+
...compileOptions,
|
|
153
|
+
from: absoluteFile
|
|
154
|
+
}),
|
|
155
|
+
dependencies: graph.dependencies,
|
|
156
|
+
...graph.references?.length ? { references: graph.references } : {}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function addUnique(target, values) {
|
|
160
|
+
if (!values) return;
|
|
161
|
+
for (const value of values) if (!target.includes(value)) target.push(value);
|
|
162
|
+
}
|
|
163
|
+
function resolveCSSReferenceFile(reference, options = {}) {
|
|
164
|
+
const fromFile = reference.file ? isAbsolute(reference.file) ? reference.file : resolve(options.root || "", reference.file) : resolve(options.root || process.cwd(), "master.css");
|
|
165
|
+
const packageFile = resolveMasterCSSPackageEntryFile(reference.source, fromFile, options.root);
|
|
166
|
+
if (packageFile) return packageFile;
|
|
167
|
+
if (isExpandableImportSource(reference.source)) return resolve(dirname(fromFile), reference.source);
|
|
168
|
+
throw new Error(`@reference only supports relative CSS files or Master CSS package entries: ${reference.source}`);
|
|
169
|
+
}
|
|
170
|
+
function normalizeReferenceStack(stack) {
|
|
171
|
+
return (stack || []).map((file) => resolveComparablePath(file));
|
|
172
|
+
}
|
|
173
|
+
function resolveCSSReferenceContext(references, options = {}) {
|
|
174
|
+
const dependencies = [];
|
|
175
|
+
const warnings = [];
|
|
176
|
+
let manifest = options.baseManifest;
|
|
177
|
+
let hasReferences = false;
|
|
178
|
+
for (const reference of references || []) {
|
|
179
|
+
const referenceFile = resolveCSSReferenceFile(reference, options);
|
|
180
|
+
const comparableReferenceFile = resolveComparablePath(referenceFile);
|
|
181
|
+
if (normalizeReferenceStack(options.referenceStack).includes(comparableReferenceFile)) throw new Error(`Circular CSS reference: ${[...options.referenceStack || [], referenceFile].join(" -> ")}`);
|
|
182
|
+
const result = compileCSSManifestFileInternal(referenceFile, {
|
|
183
|
+
...options,
|
|
184
|
+
baseManifest: manifest,
|
|
185
|
+
preserveNativeCSS: false,
|
|
186
|
+
referenceStack: options.referenceStack
|
|
187
|
+
});
|
|
188
|
+
hasReferences = true;
|
|
189
|
+
manifest = result.manifest;
|
|
190
|
+
addUnique(dependencies, result.dependencies);
|
|
191
|
+
addUnique(warnings, result.warnings);
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
dependencies,
|
|
195
|
+
warnings,
|
|
196
|
+
...hasReferences ? { manifest } : {}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function toCompileCSSManifestResult(result, options = {}) {
|
|
200
|
+
const { manifestInput: _directiveManifestInput, ...directiveData } = result;
|
|
201
|
+
const referenceContext = resolveCSSReferenceContext(result.references, options);
|
|
202
|
+
const lowerResult = lowerCSSDirectives(result, {
|
|
203
|
+
baseManifest: options.baseManifest,
|
|
204
|
+
resolutionManifest: referenceContext.manifest,
|
|
205
|
+
onWarning: options.onWarning,
|
|
206
|
+
diagnostics: options.diagnostics
|
|
207
|
+
});
|
|
208
|
+
const dependencies = [];
|
|
209
|
+
const warnings = [];
|
|
210
|
+
addUnique(dependencies, result.dependencies);
|
|
211
|
+
addUnique(dependencies, referenceContext.dependencies);
|
|
212
|
+
addUnique(warnings, referenceContext.warnings);
|
|
213
|
+
addUnique(warnings, lowerResult.warnings);
|
|
214
|
+
const generatedCSS = lowerResult.generatedCSS || "";
|
|
215
|
+
const css = [result.nativeCSS, generatedCSS].filter(Boolean).join("\n");
|
|
216
|
+
return {
|
|
217
|
+
...directiveData,
|
|
218
|
+
dependencies,
|
|
219
|
+
manifest: lowerResult.manifest,
|
|
220
|
+
warnings,
|
|
221
|
+
generatedCSS,
|
|
222
|
+
css,
|
|
223
|
+
directives: result
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function createManifestFromCSSResult(result, options = {}) {
|
|
227
|
+
return toCompileCSSManifestResult(result, options);
|
|
228
|
+
}
|
|
229
|
+
function compileCSSManifest(source, options = {}) {
|
|
230
|
+
const from = options.from ? stripRequest(options.from) : void 0;
|
|
231
|
+
const fromFile = from ? isAbsolute(from) ? from : resolve(options.root || "", from) : void 0;
|
|
232
|
+
return toCompileCSSManifestResult(compileCSS(source, {
|
|
233
|
+
...options,
|
|
234
|
+
...fromFile ? { from: fromFile } : {}
|
|
235
|
+
}), {
|
|
236
|
+
...options,
|
|
237
|
+
...fromFile ? {
|
|
238
|
+
from: fromFile,
|
|
239
|
+
referenceStack: [...options.referenceStack || [], fromFile]
|
|
240
|
+
} : {}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
function compileCSSManifestFileInternal(file, options = {}) {
|
|
244
|
+
const absoluteFile = isAbsolute(file) ? file : resolve(options.root || "", file);
|
|
245
|
+
return toCompileCSSManifestResult(compileCSSFile(file, {
|
|
246
|
+
...options,
|
|
247
|
+
preserveNativeCSS: options.preserveNativeCSS ?? false
|
|
248
|
+
}), {
|
|
249
|
+
...options,
|
|
250
|
+
from: absoluteFile,
|
|
251
|
+
referenceStack: [...options.referenceStack || [], absoluteFile]
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
function compileCSSManifestFile(file, options = {}) {
|
|
255
|
+
return compileCSSManifestFileInternal(file, options);
|
|
256
|
+
}
|
|
257
|
+
function compileProjectManifest(entries, options = {}) {
|
|
258
|
+
const dependencies = [];
|
|
259
|
+
let extractionPolicy = createCSSDirectiveExtractionPolicy();
|
|
260
|
+
const classNames = [];
|
|
261
|
+
const nativeClassNames = [];
|
|
262
|
+
const nativeCSS = [];
|
|
263
|
+
const css = [];
|
|
264
|
+
const generatedCSS = [];
|
|
265
|
+
const warnings = [];
|
|
266
|
+
let directives = {
|
|
267
|
+
manifestInput: {},
|
|
268
|
+
extractionPolicy: createCSSDirectiveExtractionPolicy(),
|
|
269
|
+
classNames: [],
|
|
270
|
+
nativeClassNames: [],
|
|
271
|
+
nativeCSS: "",
|
|
272
|
+
css: "",
|
|
273
|
+
generatedCSS: "",
|
|
274
|
+
warnings: [],
|
|
275
|
+
dependencies: []
|
|
276
|
+
};
|
|
277
|
+
let manifest = options.baseManifest;
|
|
278
|
+
for (const entry of entries) {
|
|
279
|
+
const result = compileCSSFile(entry, {
|
|
280
|
+
...options,
|
|
281
|
+
preserveNativeCSS: options.preserveNativeCSS ?? false
|
|
282
|
+
});
|
|
283
|
+
directives = result;
|
|
284
|
+
extractionPolicy = mergeCSSDirectiveExtractionPolicy(extractionPolicy, result.extractionPolicy);
|
|
285
|
+
addUnique(dependencies, result.dependencies);
|
|
286
|
+
addUnique(classNames, result.classNames);
|
|
287
|
+
addUnique(nativeClassNames, result.nativeClassNames);
|
|
288
|
+
addUnique(warnings, result.warnings);
|
|
289
|
+
const manifestResult = toCompileCSSManifestResult(result, {
|
|
290
|
+
...options,
|
|
291
|
+
baseManifest: manifest,
|
|
292
|
+
from: entry,
|
|
293
|
+
referenceStack: [isAbsolute(entry) ? entry : resolve(options.root || "", entry)]
|
|
294
|
+
});
|
|
295
|
+
manifest = manifestResult.manifest;
|
|
296
|
+
addUnique(dependencies, manifestResult.dependencies);
|
|
297
|
+
addUnique(warnings, manifestResult.warnings);
|
|
298
|
+
const entryGeneratedCSS = manifestResult.generatedCSS || "";
|
|
299
|
+
if (result.nativeCSS) nativeCSS.push(result.nativeCSS);
|
|
300
|
+
if (entryGeneratedCSS) generatedCSS.push(entryGeneratedCSS);
|
|
301
|
+
if (manifestResult.css) css.push(manifestResult.css);
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
entries,
|
|
305
|
+
manifest: manifest || createMasterCSSManifest(),
|
|
306
|
+
dependencies,
|
|
307
|
+
extractionPolicy,
|
|
308
|
+
classNames,
|
|
309
|
+
nativeClassNames,
|
|
310
|
+
nativeCSS: nativeCSS.join("\n"),
|
|
311
|
+
css: css.join("\n"),
|
|
312
|
+
generatedCSS: generatedCSS.join("\n"),
|
|
313
|
+
warnings,
|
|
314
|
+
directives
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function toManifestJSONResult(result) {
|
|
318
|
+
return {
|
|
319
|
+
...result,
|
|
320
|
+
json: stringifyMasterCSSManifestJSON(result.manifest)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function compileCSSManifestJSON(file, options = {}) {
|
|
324
|
+
return toManifestJSONResult(compileCSSManifestFile(file, options));
|
|
325
|
+
}
|
|
326
|
+
function compileProjectManifestJSON(entries, options = {}) {
|
|
327
|
+
return toManifestJSONResult(compileProjectManifest(entries, options));
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
export { collectStandaloneCSSDirectiveExtractionPolicy, compileCSS, compileCSSFile, compileCSSManifest, compileCSSManifestFile, compileCSSManifestJSON, compileProjectManifest, compileProjectManifestJSON, createCSSDirectiveExtractionPolicy, createManifestFromCSSResult, findCSSReferenceStatements, findStandaloneCSSDirectiveStatements, findStandaloneMasterDirectiveStatements, inspectCSS, isMasterCSSPackageStyleFile, maskManagedPatternEntryNames, mergeCSSDirectiveExtractionPolicy, parseDirectives, removeCSSReferenceStatements, removeStandaloneCSSDirectives, removeStandaloneMasterDirectives, resolveCSSImportGraph, resolveMasterCSSPackageEntryFile, resolveMasterCSSPackageImportGraph, setCSSTransform };
|
package/dist/lexer/imports.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Rule } from "lightningcss";
|
|
2
|
+
|
|
3
|
+
//#region src/lexer/imports.d.ts
|
|
4
|
+
interface CSSImportStatement {
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
source: string;
|
|
8
|
+
statement: string;
|
|
9
|
+
rule: Extract<Rule, {
|
|
10
|
+
type: 'import';
|
|
11
|
+
}>;
|
|
10
12
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
declare function findCSSImportStatements(source: string, filename?: string): CSSImportStatement[];
|
|
14
|
+
declare function removeCSSImportStatements(source: string, filename?: string): string;
|
|
15
|
+
declare function replaceCSSImportStatements(source: string, filename: string, getReplacement: (statement: CSSImportStatement) => string | undefined): string;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { CSSImportStatement, findCSSImportStatements, removeCSSImportStatements, replaceCSSImportStatements };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { encodeCSS, getCSSTransform } from "../css-transform.js";
|
|
2
|
+
import { createSourceLocationResolver, findAtRuleStatementEnd, removeSourceRanges, replaceSourceRanges } from "./source.js";
|
|
3
|
+
import { maskManagedPatternEntryNames } from "../core.js";
|
|
4
|
+
//#region src/lexer/imports.ts
|
|
5
|
+
function findCSSImportStatements(source, filename = "master.css") {
|
|
6
|
+
const statements = [];
|
|
7
|
+
const maskedSource = maskManagedPatternEntryNames(source);
|
|
8
|
+
const resolveLocation = createSourceLocationResolver(maskedSource);
|
|
9
|
+
getCSSTransform()({
|
|
10
|
+
filename,
|
|
11
|
+
code: encodeCSS(maskedSource),
|
|
12
|
+
visitor: { Rule: { import(rule) {
|
|
13
|
+
const start = resolveLocation(rule.value.loc);
|
|
14
|
+
if (start === -1) return;
|
|
15
|
+
const end = findAtRuleStatementEnd(source, start);
|
|
16
|
+
if (end === -1) return;
|
|
17
|
+
statements.push({
|
|
18
|
+
start,
|
|
19
|
+
end,
|
|
20
|
+
source: rule.value.url,
|
|
21
|
+
statement: source.slice(start, end),
|
|
22
|
+
rule
|
|
23
|
+
});
|
|
24
|
+
} } }
|
|
25
|
+
});
|
|
26
|
+
return statements;
|
|
27
|
+
}
|
|
28
|
+
function removeCSSImportStatements(source, filename = "master.css") {
|
|
29
|
+
return removeSourceRanges(source, findCSSImportStatements(source, filename));
|
|
30
|
+
}
|
|
31
|
+
function replaceCSSImportStatements(source, filename, getReplacement) {
|
|
32
|
+
return replaceSourceRanges(source, findCSSImportStatements(source, filename).map((statement) => ({
|
|
33
|
+
...statement,
|
|
34
|
+
replacement: getReplacement(statement) ?? statement.statement
|
|
35
|
+
})));
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { findCSSImportStatements, removeCSSImportStatements, replaceCSSImportStatements };
|
package/dist/lexer/source.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import { createSourceLocationResolver, removeSourceRanges, replaceSourceRanges
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { SourceRange, createSourceLocationResolver, removeSourceRanges, replaceSourceRanges } from "@master/css-lexer";
|
|
2
|
+
|
|
3
|
+
//#region src/lexer/source.d.ts
|
|
4
|
+
declare function findAtRuleStatementEnd(source: string, start: number): number;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { type SourceRange, createSourceLocationResolver, findAtRuleStatementEnd, removeSourceRanges, replaceSourceRanges };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createSourceLocationResolver, findCSSStatementEnd, removeSourceRanges, replaceSourceRanges } from "@master/css-lexer";
|
|
2
|
+
//#region src/lexer/source.ts
|
|
3
|
+
function findAtRuleStatementEnd(source, start) {
|
|
4
|
+
const statementEnd = findCSSStatementEnd(source, start);
|
|
5
|
+
return statementEnd.reason === "semicolon" ? statementEnd.end : -1;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { createSourceLocationResolver, findAtRuleStatementEnd, removeSourceRanges, replaceSourceRanges };
|
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { CSSDirectiveExtractionPolicy } from "@master/css-schema/css-directives";
|
|
2
|
+
|
|
3
|
+
//#region src/lexer/standalone-master.d.ts
|
|
4
|
+
type StandaloneCSSDirectiveName = 'master' | 'source' | 'safelist' | 'blocklist' | 'preserve';
|
|
5
|
+
interface StandaloneCSSDirectiveStatement {
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
atRuleName: StandaloneCSSDirectiveName;
|
|
9
|
+
name: string;
|
|
10
|
+
statement: string;
|
|
11
|
+
args: string[];
|
|
12
|
+
modifiers: string[];
|
|
11
13
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
type StandaloneMasterDirectiveStatement = StandaloneCSSDirectiveStatement & {
|
|
15
|
+
atRuleName: 'master';
|
|
14
16
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
declare function createCSSDirectiveExtractionPolicy(): CSSDirectiveExtractionPolicy;
|
|
18
|
+
declare function mergeCSSDirectiveExtractionPolicy(...policies: (Partial<CSSDirectiveExtractionPolicy> | undefined)[]): CSSDirectiveExtractionPolicy;
|
|
19
|
+
declare function findStandaloneCSSDirectiveStatements(source: string, filename?: string): StandaloneCSSDirectiveStatement[];
|
|
20
|
+
declare function findStandaloneMasterDirectiveStatements(source: string, filename?: string): StandaloneMasterDirectiveStatement[];
|
|
21
|
+
declare function collectStandaloneCSSDirectiveExtractionPolicy(source: string, filename?: string): CSSDirectiveExtractionPolicy;
|
|
22
|
+
declare function removeStandaloneCSSDirectives(source: string, filename?: string): string;
|
|
23
|
+
declare function removeStandaloneMasterDirectives(source: string, filename?: string): string;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { StandaloneCSSDirectiveName, StandaloneCSSDirectiveStatement, StandaloneMasterDirectiveStatement, collectStandaloneCSSDirectiveExtractionPolicy, createCSSDirectiveExtractionPolicy, findStandaloneCSSDirectiveStatements, findStandaloneMasterDirectiveStatements, mergeCSSDirectiveExtractionPolicy, removeStandaloneCSSDirectives, removeStandaloneMasterDirectives };
|