gemcss 0.3.0 → 0.4.0

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.
@@ -1,31 +0,0 @@
1
- import {
2
- parseCss
3
- } from "./chunk-CNXDOGPX.js";
4
-
5
- // src/core/compile.ts
6
- import postcss from "postcss";
7
- import postcssModules from "postcss-modules";
8
- async function compile(code, id, opts = {}) {
9
- let scoped = {};
10
- const result = await postcss([
11
- postcssModules({
12
- generateScopedName: opts.generateScopedName ?? "[name]__[local]__[hash:base64:5]",
13
- getJSON: (_file, json) => {
14
- scoped = json;
15
- }
16
- })
17
- ]).process(code, { from: id });
18
- const module = parseCss(code, { from: id });
19
- return { css: result.css, scoped, module };
20
- }
21
-
22
- // src/core/runtime-path.ts
23
- import { createRequire } from "module";
24
- function runtimePath() {
25
- return createRequire(import.meta.url).resolve("gemcss/runtime");
26
- }
27
-
28
- export {
29
- compile,
30
- runtimePath
31
- };
@@ -1,6 +0,0 @@
1
- interface CompileOptions {
2
- /** Scoped name template for postcss-modules. */
3
- generateScopedName?: string;
4
- }
5
-
6
- export type { CompileOptions as C };
@@ -1,206 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
-
25
- // src/ts-plugin.ts
26
- var import_node_path = require("path");
27
-
28
- // src/ts-plugin/generate-dts.ts
29
- var import_node_fs = require("fs");
30
-
31
- // src/core/dts.ts
32
- var IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
33
- function propKey(name) {
34
- return IDENT.test(name) ? name : JSON.stringify(name);
35
- }
36
- function strLit(value) {
37
- return `'${value.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
38
- }
39
- function propsParam(block) {
40
- const keys = Object.keys(block.modifiers);
41
- if (keys.length === 0) return "";
42
- const fields = keys.map((k) => {
43
- const mod = block.modifiers[k];
44
- const type = mod.kind === "bool" ? "boolean" : mod.values.map(strLit).join(" | ");
45
- return `${propKey(k)}?: ${type}`;
46
- });
47
- return `props?: { ${fields.join("; ")} }`;
48
- }
49
- function generateDts(module2) {
50
- const lines = ["declare const gems: {"];
51
- for (const block of Object.values(module2.blocks)) {
52
- lines.push(` ${propKey(block.name)}(${propsParam(block)}): string;`);
53
- }
54
- lines.push("};");
55
- lines.push("export default gems;");
56
- lines.push("");
57
- return lines.join("\n");
58
- }
59
-
60
- // src/core/parser.ts
61
- var import_postcss = __toESM(require("postcss"), 1);
62
- var import_postcss_selector_parser = __toESM(require("postcss-selector-parser"), 1);
63
- var GemcssParseError = class extends Error {
64
- constructor(message) {
65
- super(message);
66
- this.name = "GemcssParseError";
67
- }
68
- };
69
- function ctx(from) {
70
- return from ? ` (in ${from})` : "";
71
- }
72
- function parseCss(css, opts = {}) {
73
- const root = import_postcss.default.parse(css, { from: opts.from });
74
- const classNames = [];
75
- const seen = /* @__PURE__ */ new Set();
76
- root.walkRules((rule) => {
77
- (0, import_postcss_selector_parser.default)((selectors) => {
78
- selectors.walkClasses((cls) => {
79
- if (!seen.has(cls.value)) {
80
- seen.add(cls.value);
81
- classNames.push(cls.value);
82
- }
83
- });
84
- }).processSync(rule.selector);
85
- });
86
- return buildModule(classNames, opts.from);
87
- }
88
- function buildModule(classNames, from) {
89
- const blocks = {};
90
- for (const raw of classNames) {
91
- const segments = raw.split("_");
92
- const blockName = segments[0];
93
- if (!blockName) {
94
- throw new GemcssParseError(`Empty block name in class ".${raw}"${ctx(from)}`);
95
- }
96
- const block = blocks[blockName] ??= {
97
- name: blockName,
98
- modifiers: {}
99
- };
100
- const tail = segments.slice(1);
101
- if (tail.length === 0) continue;
102
- if (tail.length === 1) {
103
- const key = tail[0];
104
- if (!key) throw new GemcssParseError(`Empty modifier in class ".${raw}"${ctx(from)}`);
105
- assignBool(block, key, raw, from);
106
- continue;
107
- }
108
- if (tail.length === 2) {
109
- const key = tail[0];
110
- const value = tail[1];
111
- if (!key || !value) {
112
- throw new GemcssParseError(`Empty modifier key/value in class ".${raw}"${ctx(from)}`);
113
- }
114
- assignEnum(block, key, value, raw, from);
115
- continue;
116
- }
117
- throw new GemcssParseError(
118
- `Class ".${raw}" has more than one modifier segment after block "${blockName}". Allowed: block, block_bool, block_key_value.${ctx(from)}`
119
- );
120
- }
121
- return { blocks };
122
- }
123
- function assignBool(block, key, raw, from) {
124
- const existing = block.modifiers[key];
125
- if (existing && existing.kind === "enum") {
126
- throw new GemcssParseError(
127
- `Modifier "${key}" of block "${block.name}" used as both boolean (".${raw}") and enum.${ctx(from)}`
128
- );
129
- }
130
- block.modifiers[key] = { kind: "bool" };
131
- }
132
- function assignEnum(block, key, value, raw, from) {
133
- const existing = block.modifiers[key];
134
- if (existing && existing.kind === "bool") {
135
- throw new GemcssParseError(
136
- `Modifier "${key}" of block "${block.name}" used as both enum (".${raw}") and boolean.${ctx(from)}`
137
- );
138
- }
139
- const mod = existing && existing.kind === "enum" ? existing : block.modifiers[key] = { kind: "enum", values: [] };
140
- if (!mod.values.includes(value)) {
141
- mod.values.push(value);
142
- }
143
- }
144
-
145
- // src/ts-plugin/generate-dts.ts
146
- var FALLBACK = "declare const gems: Record<string, (props?: Record<string, string | boolean>) => string>;\nexport default gems;\n";
147
- function dtsForFile(fileName, read = defaultRead) {
148
- const css = read(fileName);
149
- if (css == null) return FALLBACK;
150
- try {
151
- return generateDts(parseCss(css, { from: fileName }));
152
- } catch {
153
- return FALLBACK;
154
- }
155
- }
156
- function defaultRead(f) {
157
- try {
158
- return (0, import_node_fs.readFileSync)(f, "utf8");
159
- } catch {
160
- return void 0;
161
- }
162
- }
163
-
164
- // src/ts-plugin.ts
165
- var MODULE_CSS = /\.module\.css$/;
166
- function init({ typescript: ts }) {
167
- function create(info) {
168
- const host = info.languageServiceHost;
169
- const read = (f) => host.readFile?.(f);
170
- const origKind = host.getScriptKind?.bind(host);
171
- host.getScriptKind = (fileName) => {
172
- if (MODULE_CSS.test(fileName)) return ts.ScriptKind.TS;
173
- return origKind ? origKind(fileName) : ts.ScriptKind.Unknown;
174
- };
175
- const origSnapshot = host.getScriptSnapshot.bind(host);
176
- host.getScriptSnapshot = (fileName) => {
177
- if (MODULE_CSS.test(fileName)) {
178
- return ts.ScriptSnapshot.fromString(dtsForFile(fileName, read));
179
- }
180
- return origSnapshot(fileName);
181
- };
182
- if (host.resolveModuleNameLiterals) {
183
- const origResolve = host.resolveModuleNameLiterals.bind(host);
184
- host.resolveModuleNameLiterals = (literals, containingFile, ...rest) => {
185
- const resolved = origResolve(literals, containingFile, ...rest);
186
- return literals.map((literal, i) => {
187
- const name = literal.text;
188
- if (MODULE_CSS.test(name)) {
189
- const fileName = name.startsWith(".") ? (0, import_node_path.resolve)((0, import_node_path.dirname)(containingFile), name) : name;
190
- return {
191
- resolvedModule: {
192
- resolvedFileName: fileName,
193
- extension: ts.Extension.Dts,
194
- isExternalLibraryImport: false
195
- }
196
- };
197
- }
198
- return resolved[i];
199
- });
200
- };
201
- }
202
- return info.languageService;
203
- }
204
- return { create };
205
- }
206
- module.exports = init;