@kanso-labs/unplugin-style-dictionary 0.10.11 → 0.10.13
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/README.md +87 -33
- package/dist/compile.js +40 -8
- package/dist/compile.js.map +1 -1
- package/dist/index.js +36 -7
- package/dist/index.js.map +1 -1
- package/dist/patterns.js +53 -33
- package/dist/patterns.js.map +1 -1
- package/dist/types.d.ts +32 -8
- package/dist/up-to-date.js +142 -19
- package/dist/up-to-date.js.map +1 -1
- package/dist/watch-filter.js +4 -6
- package/dist/watch-filter.js.map +1 -1
- package/package.json +9 -9
package/dist/types.d.ts
CHANGED
|
@@ -66,8 +66,8 @@ export interface StyleDictionaryConfigContext {
|
|
|
66
66
|
/**
|
|
67
67
|
* Whether the host will keep rebuilding.
|
|
68
68
|
*
|
|
69
|
-
* `true` under Vite's dev server
|
|
70
|
-
* `webpack --watch`; `false` for a one-shot build. It is read from the
|
|
69
|
+
* `true` under Vite's dev server and `vite build --watch`, `rollup --watch`,
|
|
70
|
+
* `rolldown.watch()` and `webpack --watch`; `false` for a one-shot build. It is read from the
|
|
71
71
|
* host — the plugin context's `meta.watchMode` on the three rollup-shaped
|
|
72
72
|
* targets, and `compiler.watchMode` on webpack — rather than inferred from
|
|
73
73
|
* `command`, because `rollup --watch` both watches and builds.
|
|
@@ -83,8 +83,28 @@ export interface UnpluginStyleDictionaryOptions {
|
|
|
83
83
|
* 4,000-token, two-platform configuration — and under Vite it runs inside
|
|
84
84
|
* `server.listen()`, so the dev server does not accept a connection until
|
|
85
85
|
* it finishes whether or not a token changed. A configuration is treated as
|
|
86
|
-
* up to date when every file
|
|
87
|
-
*
|
|
86
|
+
* up to date when every file its last compile wrote exists and is as that
|
|
87
|
+
* compile left it, and nothing it reads, its own config file included, has
|
|
88
|
+
* changed since that compile read it. A file it declares and Style
|
|
89
|
+
* Dictionary declined to write — a filter matching no tokens — does not stop
|
|
90
|
+
* it being skipped, since that file was never going to be there.
|
|
91
|
+
*
|
|
92
|
+
* "Since" means since the moment that compile began reading, so a token
|
|
93
|
+
* saved while a rebuild is compiling is rebuilt rather than taken for part
|
|
94
|
+
* of the output the rebuild went on to write. What each compile read is
|
|
95
|
+
* kept in `node_modules/.cache/unplugin-style-dictionary` beside the nearest
|
|
96
|
+
* `package.json`, so a restart still knows it. Where nothing is recorded,
|
|
97
|
+
* the files a configuration reads are compared against the files it
|
|
98
|
+
* declares instead.
|
|
99
|
+
*
|
|
100
|
+
* A compile is also recorded against the configuration that ran it: its
|
|
101
|
+
* contents, the hooks it names as Style Dictionary's registry holds them —
|
|
102
|
+
* each `format`, `filter`, `fileHeader`, transform, transform group,
|
|
103
|
+
* parser and preprocessor it refers to by name — and the Style Dictionary
|
|
104
|
+
* version. A new body registered under a name it uses, or an upgrade,
|
|
105
|
+
* rebuilds even though no file moved. What a hook's code calls from another
|
|
106
|
+
* module is not part of that code, so an edit there is not seen; naming the
|
|
107
|
+
* module in `watch` makes the next start rebuild.
|
|
88
108
|
*
|
|
89
109
|
* Two things are never skipped, because neither can be told from the
|
|
90
110
|
* filesystem:
|
|
@@ -93,7 +113,9 @@ export interface UnpluginStyleDictionaryOptions {
|
|
|
93
113
|
* object or a function.** There is no config file to stat, so an edit to
|
|
94
114
|
* the object inside `vite.config.ts` moves no mtime. Within one process
|
|
95
115
|
* the resolved configuration is compared against the one that was last
|
|
96
|
-
* built
|
|
116
|
+
* built. An earlier process's record is not trusted for it, because its
|
|
117
|
+
* functions are compared by their source and what they close over can
|
|
118
|
+
* differ from one process to the next, so it builds.
|
|
97
119
|
* - **A platform declaring `actions`.** An action writes what no
|
|
98
120
|
* `destination` names, so a skip would leave its work undone.
|
|
99
121
|
*
|
|
@@ -212,15 +234,17 @@ export interface UnpluginStyleDictionaryOptions {
|
|
|
212
234
|
*/
|
|
213
235
|
logLevel?: 'info' | 'silent' | 'verbose' | 'warn';
|
|
214
236
|
/**
|
|
215
|
-
* Called once a build has finished, with every file it declares
|
|
216
|
-
* it took in milliseconds.
|
|
237
|
+
* Called once a build has finished, with every file it declares that is on
|
|
238
|
+
* disk and how long it took in milliseconds.
|
|
217
239
|
*
|
|
218
240
|
* The paths are absolute and platform-native, sorted so two runs of the same
|
|
219
241
|
* configuration hand back the same order. They are what the build declares
|
|
220
242
|
* rather than what it wrote this time: a configuration skipped by `cache`
|
|
221
243
|
* contributes its destinations too, because they are on disk and current,
|
|
222
244
|
* and a post-processing step that ignored them would leave half the output
|
|
223
|
-
* untouched on a rebuild that changed one file.
|
|
245
|
+
* untouched on a rebuild that changed one file. A declared file Style
|
|
246
|
+
* Dictionary declined to write — a filter matching no tokens — is left out,
|
|
247
|
+
* since there is nothing there to open.
|
|
224
248
|
*
|
|
225
249
|
* This is where formatting the generated files, type-checking them, or
|
|
226
250
|
* telling something else they have landed belongs.
|
package/dist/up-to-date.js
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
|
-
import { expandPatterns, sourcePatternsOf } from "./patterns.js";
|
|
1
|
+
import { expandPatterns, sourcePatternsOf, watchOptionPatterns } from "./patterns.js";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import StyleDictionary from "style-dictionary";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
4
6
|
//#region src/up-to-date.ts
|
|
5
|
-
|
|
7
|
+
const RECORDS_VERSION = 3;
|
|
8
|
+
function configFingerprint(root, item, configObj) {
|
|
9
|
+
let serialised;
|
|
6
10
|
try {
|
|
7
|
-
|
|
11
|
+
serialised = JSON.stringify([
|
|
12
|
+
root,
|
|
13
|
+
item.file ?? null,
|
|
14
|
+
configObj,
|
|
15
|
+
namedHooksOf(configObj),
|
|
16
|
+
StyleDictionary.VERSION
|
|
17
|
+
], (_key, value) => typeof value === "function" ? `[fn]${String(value)}` : value);
|
|
8
18
|
} catch {
|
|
9
19
|
return null;
|
|
10
20
|
}
|
|
21
|
+
return createHash("sha256").update(serialised).digest("hex");
|
|
11
22
|
}
|
|
12
23
|
function declaredDestinations(configObj, only) {
|
|
13
24
|
const destinations = [];
|
|
@@ -16,38 +27,150 @@ function declaredDestinations(configObj, only) {
|
|
|
16
27
|
for (const [, platform] of selected) for (const file of platform.files ?? []) if (file.destination) destinations.push(writtenDestination(platform.buildPath, file.destination));
|
|
17
28
|
return destinations;
|
|
18
29
|
}
|
|
19
|
-
|
|
30
|
+
function isUpToDate({ destinationRecords, fingerprint, newestSource }, item, configObj, only) {
|
|
20
31
|
if (Object.values(configObj.platforms ?? {}).some((platform) => (platform.actions?.length ?? 0) > 0)) return false;
|
|
21
32
|
const destinations = declaredDestinations(configObj, only);
|
|
22
33
|
if (destinations.length === 0) return false;
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
if (newestSource === null) return false;
|
|
35
|
+
for (const destination of destinations) {
|
|
36
|
+
const record = destinationRecords.get(destination);
|
|
37
|
+
const writtenByThis = fingerprint !== null && record?.fingerprint === fingerprint;
|
|
38
|
+
if (record !== void 0 && !writtenByThis) return false;
|
|
39
|
+
if (!item.file && (!writtenByThis || record.persisted === true)) return false;
|
|
40
|
+
if (writtenByThis && record.written === null) {
|
|
41
|
+
if (record.newestSource === null || newestSource > record.newestSource) return false;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const stats = statOrNull(destination);
|
|
45
|
+
if (!stats) return false;
|
|
46
|
+
if (writtenByThis && record.written !== stats.mtimeMs) return false;
|
|
47
|
+
const lastRead = writtenByThis ? record.newestSource : null;
|
|
48
|
+
if (lastRead === null ? stats.mtimeMs <= newestSource : newestSource > lastRead) return false;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
async function newestSourceOf({ log, root, watch }, item, configObj) {
|
|
53
|
+
const read = sourcePatternsOf(configObj);
|
|
54
|
+
const extra = watchOptionPatterns(root, watch);
|
|
55
|
+
const sources = await expandPatterns({
|
|
56
|
+
globs: [...read.globs, ...extra.globs],
|
|
57
|
+
literals: [...read.literals, ...extra.literals]
|
|
58
|
+
}, log);
|
|
25
59
|
if (item.file) sources.push(item.file.replace(/\\/g, "/"));
|
|
26
|
-
if (sources.length === 0) return
|
|
60
|
+
if (sources.length === 0) return null;
|
|
27
61
|
let newestSource = -Infinity;
|
|
28
62
|
let sawFile = false;
|
|
29
63
|
for (const source of sources) {
|
|
30
64
|
const stats = statOrNull(source);
|
|
31
|
-
if (!stats) return
|
|
65
|
+
if (!stats) return null;
|
|
32
66
|
if (stats.isDirectory()) continue;
|
|
33
67
|
sawFile = true;
|
|
34
68
|
newestSource = Math.max(newestSource, stats.mtimeMs);
|
|
35
69
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
70
|
+
return sawFile ? newestSource : null;
|
|
71
|
+
}
|
|
72
|
+
function readPersistedRecords(file) {
|
|
73
|
+
const records = /* @__PURE__ */ new Map();
|
|
74
|
+
let parsed;
|
|
75
|
+
try {
|
|
76
|
+
parsed = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
77
|
+
} catch {
|
|
78
|
+
return records;
|
|
79
|
+
}
|
|
80
|
+
if (typeof parsed !== "object" || parsed === null || !("version" in parsed) || parsed.version !== RECORDS_VERSION || !("records" in parsed) || typeof parsed.records !== "object" || parsed.records === null) return records;
|
|
81
|
+
const stored = parsed.records;
|
|
82
|
+
for (const destination of Object.keys(stored)) {
|
|
83
|
+
const value = Reflect.get(stored, destination);
|
|
84
|
+
if (typeof value === "object" && value !== null && "fingerprint" in value && typeof value.fingerprint === "string" && "newestSource" in value && (value.newestSource === null || typeof value.newestSource === "number") && "written" in value && (value.written === null || typeof value.written === "number")) records.set(destination, {
|
|
85
|
+
fingerprint: value.fingerprint,
|
|
86
|
+
newestSource: value.newestSource,
|
|
87
|
+
persisted: true,
|
|
88
|
+
written: value.written
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return records;
|
|
92
|
+
}
|
|
93
|
+
function recordOf(destination, fingerprint, newestSource, wrote) {
|
|
94
|
+
if (!wrote) return {
|
|
95
|
+
fingerprint,
|
|
96
|
+
newestSource,
|
|
97
|
+
written: null
|
|
98
|
+
};
|
|
99
|
+
const stats = statOrNull(destination);
|
|
100
|
+
return stats ? {
|
|
101
|
+
fingerprint,
|
|
102
|
+
newestSource,
|
|
103
|
+
written: stats.mtimeMs
|
|
104
|
+
} : null;
|
|
105
|
+
}
|
|
106
|
+
function recordsFileFor(root) {
|
|
107
|
+
let directory = root;
|
|
108
|
+
while (!fs.existsSync(path.join(directory, "package.json"))) {
|
|
109
|
+
const parent = path.dirname(directory);
|
|
110
|
+
if (parent === directory) {
|
|
111
|
+
directory = root;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
directory = parent;
|
|
115
|
+
}
|
|
116
|
+
return path.join(directory, "node_modules", ".cache", "unplugin-style-dictionary", "records");
|
|
117
|
+
}
|
|
118
|
+
function writePersistedRecords(file, recorded) {
|
|
119
|
+
const merged = readPersistedRecords(file);
|
|
120
|
+
for (const [destination, record] of recorded) merged.set(destination, record);
|
|
121
|
+
const records = {};
|
|
122
|
+
for (const [destination, { fingerprint, newestSource, written }] of merged) if (written === null ? statOrNull(path.dirname(destination)) : statOrNull(destination)) records[destination] = {
|
|
123
|
+
fingerprint,
|
|
124
|
+
newestSource,
|
|
125
|
+
written
|
|
126
|
+
};
|
|
127
|
+
const temporary = `${file}.${String(process.pid)}.tmp`;
|
|
128
|
+
try {
|
|
129
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
130
|
+
fs.writeFileSync(temporary, JSON.stringify({
|
|
131
|
+
records,
|
|
132
|
+
version: RECORDS_VERSION
|
|
133
|
+
}));
|
|
134
|
+
fs.renameSync(temporary, file);
|
|
135
|
+
} catch {
|
|
136
|
+
try {
|
|
137
|
+
fs.rmSync(temporary, { force: true });
|
|
138
|
+
} catch {}
|
|
42
139
|
}
|
|
43
|
-
if (oldestDestination <= newestSource) return false;
|
|
44
|
-
if (item.file) return true;
|
|
45
|
-
const fingerprint = configFingerprint(root, item);
|
|
46
|
-
return fingerprint !== null && compiledFingerprints.has(fingerprint);
|
|
47
140
|
}
|
|
48
141
|
function writtenDestination(buildPath, destination) {
|
|
49
142
|
return path.resolve(process.cwd(), buildPath ? path.join(buildPath, destination) : destination);
|
|
50
143
|
}
|
|
144
|
+
function namedHooksOf(configObj) {
|
|
145
|
+
const own = configObj.hooks ?? {};
|
|
146
|
+
const registry = StyleDictionary.hooks;
|
|
147
|
+
const named = {};
|
|
148
|
+
const add = (kind, name) => {
|
|
149
|
+
if (typeof name !== "string") return;
|
|
150
|
+
const hook = own[kind]?.[name] ?? registry[kind][name];
|
|
151
|
+
named[kind] ??= {};
|
|
152
|
+
named[kind][name] = hook ?? null;
|
|
153
|
+
};
|
|
154
|
+
for (const name of configObj.parsers ?? []) add("parsers", name);
|
|
155
|
+
for (const name of configObj.preprocessors ?? []) add("preprocessors", name);
|
|
156
|
+
for (const platform of Object.values(configObj.platforms ?? {})) {
|
|
157
|
+
for (const name of platform.preprocessors ?? []) add("preprocessors", name);
|
|
158
|
+
for (const transform of platform.transforms ?? []) add("transforms", transform);
|
|
159
|
+
const group = platform.transformGroup;
|
|
160
|
+
if (group !== void 0) {
|
|
161
|
+
add("transformGroups", group);
|
|
162
|
+
const members = own.transformGroups?.[group] ?? registry.transformGroups[group];
|
|
163
|
+
if (Array.isArray(members)) for (const name of members) add("transforms", name);
|
|
164
|
+
}
|
|
165
|
+
add("fileHeaders", platform.options?.fileHeader);
|
|
166
|
+
for (const file of platform.files ?? []) {
|
|
167
|
+
add("formats", file.format);
|
|
168
|
+
add("filters", file.filter);
|
|
169
|
+
add("fileHeaders", file.options?.fileHeader);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return named;
|
|
173
|
+
}
|
|
51
174
|
function statOrNull(file) {
|
|
52
175
|
try {
|
|
53
176
|
return fs.statSync(file);
|
|
@@ -56,6 +179,6 @@ function statOrNull(file) {
|
|
|
56
179
|
}
|
|
57
180
|
}
|
|
58
181
|
//#endregion
|
|
59
|
-
export { configFingerprint, declaredDestinations, isUpToDate, writtenDestination };
|
|
182
|
+
export { configFingerprint, declaredDestinations, isUpToDate, newestSourceOf, readPersistedRecords, recordOf, recordsFileFor, writePersistedRecords, writtenDestination };
|
|
60
183
|
|
|
61
184
|
//# sourceMappingURL=up-to-date.js.map
|
package/dist/up-to-date.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"up-to-date.js","names":[],"sources":["../src/up-to-date.ts"],"sourcesContent":["// Whether a configuration's compile can be skipped, and the destinations that\n// question is asked about.\n//\n// Nothing here holds state — not even the fingerprints of what this process\n// has compiled, which are process-wide and live in `index.ts` beside\n// `compilesInFlight`. What a plugin instance owns is handed in on each call.\n\nimport type { Config } from 'style-dictionary'\n\nimport fs from 'node:fs'\nimport path from 'node:path'\n\nimport type { Log, ResolvedConfig } from './config.js'\nimport type { UnpluginStyleDictionaryOptions } from './types.js'\n\nimport { expandPatterns, sourcePatternsOf } from './patterns.js'\n\n// A stable identity for one resolved configuration, or `null` where it\n// cannot have one. Functions are serialised by source rather than dropped,\n// because an inline `format` or `transform` is exactly the edit a\n// fingerprint has to notice, and `JSON.stringify` omits a function outright.\nexport function configFingerprint(\n root: string,\n item: ResolvedConfig,\n): null | string {\n try {\n return JSON.stringify(\n [root, item.file ?? item.config],\n (_key, value: unknown) =>\n typeof value === 'function' ? `[fn]${String(value)}` : value,\n )\n } catch {\n // Circular, or holding a BigInt. It takes no identity rather than a\n // wrong one, so it compiles every time exactly as it did before.\n return null\n }\n}\n\n// Every absolute destination a configuration declares, read off the\n// configuration itself rather than off an extended Style Dictionary\n// instance. Reading it here is the whole point: constructing the instance\n// is what the skip exists to avoid.\n//\n// Each one is named by `writtenDestination`, which `runBuilds` asks as well,\n// so the up-to-date check and the record of what was built name the same\n// files — the ones Style Dictionary actually wrote.\n//\n// `only` narrows this to named platforms, and exactly one caller wants that:\n// the up-to-date check, which asks whether the work *this* compile would do\n// is already done. Everywhere else the answer has to cover every declared\n// platform, because a file an unselected platform wrote earlier is still the\n// plugin's own output and has to stay out of the watch list.\nexport function declaredDestinations(\n configObj: Config,\n only?: string[],\n): string[] {\n const destinations: string[] = []\n\n const entries = Object.entries(configObj.platforms ?? {})\n const selected = only\n ? entries.filter(([name]) => only.includes(name))\n : entries\n\n for (const [, platform] of selected) {\n for (const file of platform.files ?? []) {\n if (file.destination) {\n destinations.push(\n writtenDestination(platform.buildPath, file.destination),\n )\n }\n }\n }\n\n return destinations\n}\n\n// Whether every file a configuration declares is already newer than every\n// file it reads, so its compile can be skipped.\n//\n// Conservative in every direction it can be: anything it cannot establish —\n// a destination that is missing, a source it cannot stat, a configuration\n// declaring no destinations at all — is a reason to build rather than to\n// skip.\nexport async function isUpToDate(\n {\n compiledFingerprints,\n log,\n root,\n watch,\n }: {\n compiledFingerprints: ReadonlySet<string>\n log: Log\n root: string\n watch: UnpluginStyleDictionaryOptions['watch']\n },\n item: ResolvedConfig,\n configObj: Config,\n only?: string[],\n): Promise<boolean> {\n // An action writes what no `destination` names, so there is nothing for\n // the comparison below to check and skipping would leave its work undone.\n const hasActions = Object.values(configObj.platforms ?? {}).some(\n (platform) => (platform.actions?.length ?? 0) > 0,\n )\n if (hasActions) return false\n\n const destinations = declaredDestinations(configObj, only)\n if (destinations.length === 0) return false\n\n // `options.watch` belongs in here as much as `source` does. A consumer\n // names an extra file because something in the build reads it — a custom\n // format's own data file, most obviously — and leaving it out let a change\n // to it be skipped over while the watcher dutifully reported it.\n const extraWatches = watch ? (Array.isArray(watch) ? watch : [watch]) : []\n\n const sources = await expandPatterns(\n [\n ...sourcePatternsOf(configObj),\n ...extraWatches.map((pattern) =>\n (path.isAbsolute(pattern)\n ? pattern\n : path.resolve(root, pattern)\n ).replace(/\\\\/g, '/'),\n ),\n ],\n log,\n )\n if (item.file) sources.push(item.file.replace(/\\\\/g, '/'))\n\n if (sources.length === 0) return false\n\n let newestSource = -Infinity\n let sawFile = false\n\n for (const source of sources) {\n const stats = statOrNull(source)\n if (!stats) return false\n\n // Directories are in this list on purpose — `expandPatterns` registers\n // each pattern's static parent so a token file created later is\n // noticed — but their mtime cannot be read as an input signal here. A\n // directory's mtime moves whenever an entry is added or renamed inside\n // it, and the atomic write renames every generated file into place, so\n // a `buildPath` inside a watched directory made the build itself the\n // newest thing the comparison could see. Nothing was ever up to date.\n if (stats.isDirectory()) continue\n\n sawFile = true\n newestSource = Math.max(newestSource, stats.mtimeMs)\n }\n\n // Every pattern expanded to directories alone, so nothing was actually\n // read. Style Dictionary would build an empty dictionary from that, and a\n // skip would present the empty result as current.\n if (!sawFile) return false\n\n let oldestDestination = Infinity\n for (const destination of destinations) {\n const stats = statOrNull(destination)\n if (!stats) return false\n oldestDestination = Math.min(oldestDestination, stats.mtimeMs)\n }\n\n if (oldestDestination <= newestSource) return false\n\n // A configuration given as a path has its own file among the sources\n // above, so an edit to it has already been accounted for and the skip\n // holds across processes.\n if (item.file) return true\n\n // One given as an object or a function has not. Only this process knows\n // what it looked like when those destinations were written, so the skip\n // holds only against a fingerprint recorded here.\n const fingerprint = configFingerprint(root, item)\n\n return fingerprint !== null && compiledFingerprints.has(fingerprint)\n}\n\n// Where Style Dictionary writes one file, as an absolute path.\n//\n// It joins the destination onto the platform's `buildPath` when there is one\n// and writes the result relative to the working directory. **Joins, not\n// resolves**: an absolute destination lands under the build path rather than\n// replacing it. Measured with `buildPath: 'gen/'` and an absolute destination,\n// Style Dictionary wrote `gen/<that path>`, while the plugin had named the\n// absolute path on its own — a file that was never written.\n//\n// `root` has no say. It is where a relative `config` path is looked up and\n// nothing more, and reading `buildPath` against it named files that did not\n// exist whenever the two differed (#362). Style Dictionary joins with\n// `path-unified/posix`; the platform's own `join` names the same file, and\n// resolving the result is what normalises the separators either way.\nexport function writtenDestination(\n buildPath: string | undefined,\n destination: string,\n): string {\n return path.resolve(\n process.cwd(),\n buildPath ? path.join(buildPath, destination) : destination,\n )\n}\n\n// `fs.statSync` without the throw. A file that is missing, or that cannot be\n// read, is the same answer to every caller here: nothing to compare against.\nfunction statOrNull(file: string): fs.Stats | null {\n try {\n return fs.statSync(file)\n } catch {\n return null\n }\n}\n"],"mappings":";;;;AAqBA,SAAgB,kBACd,MACA,MACe;CACf,IAAI;EACF,OAAO,KAAK,UACV,CAAC,MAAM,KAAK,QAAQ,KAAK,MAAM,IAC9B,MAAM,UACL,OAAO,UAAU,aAAa,OAAO,OAAO,KAAK,MAAM,KAC3D;CACF,QAAQ;EAGN,OAAO;CACT;AACF;AAgBA,SAAgB,qBACd,WACA,MACU;CACV,MAAM,eAAyB,CAAC;CAEhC,MAAM,UAAU,OAAO,QAAQ,UAAU,aAAa,CAAC,CAAC;CACxD,MAAM,WAAW,OACb,QAAQ,QAAQ,CAAC,UAAU,KAAK,SAAS,IAAI,CAAC,IAC9C;CAEJ,KAAK,MAAM,GAAG,aAAa,UACzB,KAAK,MAAM,QAAQ,SAAS,SAAS,CAAC,GACpC,IAAI,KAAK,aACP,aAAa,KACX,mBAAmB,SAAS,WAAW,KAAK,WAAW,CACzD;CAKN,OAAO;AACT;AASA,eAAsB,WACpB,EACE,sBACA,KACA,MACA,SAOF,MACA,WACA,MACkB;CAMlB,IAHmB,OAAO,OAAO,UAAU,aAAa,CAAC,CAAC,CAAC,CAAC,MACzD,cAAc,SAAS,SAAS,UAAU,KAAK,CAErC,GAAG,OAAO;CAEvB,MAAM,eAAe,qBAAqB,WAAW,IAAI;CACzD,IAAI,aAAa,WAAW,GAAG,OAAO;CAMtC,MAAM,eAAe,QAAS,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK,IAAK,CAAC;CAEzE,MAAM,UAAU,MAAM,eACpB,CACE,GAAG,iBAAiB,SAAS,GAC7B,GAAG,aAAa,KAAK,aAClB,KAAK,WAAW,OAAO,IACpB,UACA,KAAK,QAAQ,MAAM,OAAO,EAAA,CAC5B,QAAQ,OAAO,GAAG,CACtB,CACF,GACA,GACF;CACA,IAAI,KAAK,MAAM,QAAQ,KAAK,KAAK,KAAK,QAAQ,OAAO,GAAG,CAAC;CAEzD,IAAI,QAAQ,WAAW,GAAG,OAAO;CAEjC,IAAI,eAAe;CACnB,IAAI,UAAU;CAEd,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,QAAQ,WAAW,MAAM;EAC/B,IAAI,CAAC,OAAO,OAAO;EASnB,IAAI,MAAM,YAAY,GAAG;EAEzB,UAAU;EACV,eAAe,KAAK,IAAI,cAAc,MAAM,OAAO;CACrD;CAKA,IAAI,CAAC,SAAS,OAAO;CAErB,IAAI,oBAAoB;CACxB,KAAK,MAAM,eAAe,cAAc;EACtC,MAAM,QAAQ,WAAW,WAAW;EACpC,IAAI,CAAC,OAAO,OAAO;EACnB,oBAAoB,KAAK,IAAI,mBAAmB,MAAM,OAAO;CAC/D;CAEA,IAAI,qBAAqB,cAAc,OAAO;CAK9C,IAAI,KAAK,MAAM,OAAO;CAKtB,MAAM,cAAc,kBAAkB,MAAM,IAAI;CAEhD,OAAO,gBAAgB,QAAQ,qBAAqB,IAAI,WAAW;AACrE;AAgBA,SAAgB,mBACd,WACA,aACQ;CACR,OAAO,KAAK,QACV,QAAQ,IAAI,GACZ,YAAY,KAAK,KAAK,WAAW,WAAW,IAAI,WAClD;AACF;AAIA,SAAS,WAAW,MAA+B;CACjD,IAAI;EACF,OAAO,GAAG,SAAS,IAAI;CACzB,QAAQ;EACN,OAAO;CACT;AACF"}
|
|
1
|
+
{"version":3,"file":"up-to-date.js","names":[],"sources":["../src/up-to-date.ts"],"sourcesContent":["// Whether a configuration's compile can be skipped, and the destinations that\n// question is asked about.\n//\n// Nothing here holds state — not even the record of which configuration last\n// wrote each destination, which is process-wide and lives in `index.ts` beside\n// `compilesInFlight`. What a plugin instance owns is handed in on each call.\n// The functions that read and write the persisted copy of that record work on\n// a file they are handed and keep nothing.\n\nimport type { Config, Hooks } from 'style-dictionary/types'\n\nimport { createHash } from 'node:crypto'\nimport fs from 'node:fs'\nimport path from 'node:path'\nimport StyleDictionary from 'style-dictionary'\n\nimport type { Log, ResolvedConfig } from './config.js'\nimport type { UnpluginStyleDictionaryOptions } from './types.js'\n\nimport {\n expandPatterns,\n sourcePatternsOf,\n watchOptionPatterns,\n} from './patterns.js'\n\n// What is known about the compile that last built one destination: which\n// configuration it was, the newest source it saw before it read any of them,\n// and the file's mtime once it was done. `newestSource` is `null` where that\n// compile did not look, which is when `cache` was off or the sources could not\n// be established. `written` is `null` where the compile declared the file and\n// Style Dictionary declined to write it — a filter matching no tokens — so\n// there is nothing on disk for it to vouch for. `persisted` marks a record an\n// earlier process left.\nexport interface DestinationRecord {\n fingerprint: string\n newestSource: null | number\n persisted?: true\n written: null | number\n}\n\n// Bumped whenever what a record or a fingerprint means changes, so a file an\n// older version of the plugin wrote is ignored rather than misread. 2 added\n// the hooks a configuration names and the Style Dictionary version to the\n// fingerprint, and started comparing it for a configuration given as a path.\n// 3 records a declared file the compile did not write, with `written: null`.\nconst RECORDS_VERSION = 3\n\n// A stable identity for one resolved configuration, or `null` where it\n// cannot have one. Functions are serialised by source rather than dropped,\n// because an inline `format` or `transform` is exactly the edit a\n// fingerprint has to notice, and `JSON.stringify` omits a function outright.\n//\n// It carries more than the configuration: the hooks it names, as the registry\n// holds them now, and the Style Dictionary version. A hook named by a string is\n// only that string in the configuration, and its body lives in the registry,\n// which a `register*` call changes without anything on disk moving — the\n// README's own Custom Formats pattern registers inside the `config` function.\n// An upgrade changes the built-in hooks the same way. Neither is a file the\n// up-to-date check reads, so neither could be seen before this.\n//\n// A digest of that serialisation rather than the serialisation itself,\n// because it is held against every destination a build writes, and a\n// configuration carrying its tokens inline serialises to all of them.\nexport function configFingerprint(\n root: string,\n item: ResolvedConfig,\n configObj: Config,\n): null | string {\n let serialised: string\n try {\n serialised = JSON.stringify(\n [\n root,\n item.file ?? null,\n configObj,\n namedHooksOf(configObj),\n StyleDictionary.VERSION,\n ],\n (_key, value: unknown) =>\n typeof value === 'function' ? `[fn]${String(value)}` : value,\n )\n } catch {\n // Circular, or holding a BigInt. It takes no identity rather than a\n // wrong one, so it compiles every time exactly as it did before.\n return null\n }\n\n return createHash('sha256').update(serialised).digest('hex')\n}\n\n// Every absolute destination a configuration declares, read off the\n// configuration itself rather than off an extended Style Dictionary\n// instance. Reading it here is the whole point: constructing the instance\n// is what the skip exists to avoid.\n//\n// Each one is named by `writtenDestination`, which `runBuilds` asks as well,\n// so the up-to-date check and the record of what was built name each file\n// where Style Dictionary writes it. Whether it did is the record's to say: a\n// file whose filter matches no tokens is declared and never written.\n//\n// `only` narrows this to named platforms, and exactly one caller wants that:\n// the up-to-date check, which asks whether the work *this* compile would do\n// is already done. Everywhere else the answer has to cover every declared\n// platform, because a file an unselected platform wrote earlier is still the\n// plugin's own output and has to stay out of the watch list.\nexport function declaredDestinations(\n configObj: Config,\n only?: string[],\n): string[] {\n const destinations: string[] = []\n\n const entries = Object.entries(configObj.platforms ?? {})\n const selected = only\n ? entries.filter(([name]) => only.includes(name))\n : entries\n\n for (const [, platform] of selected) {\n for (const file of platform.files ?? []) {\n if (file.destination) {\n destinations.push(\n writtenDestination(platform.buildPath, file.destination),\n )\n }\n }\n }\n\n return destinations\n}\n\n// Whether a configuration's compile can be skipped: every file its last compile\n// wrote still exists as it was left, and nothing it reads has changed since\n// that compile read it. A declared file Style Dictionary declined to write is\n// not asked about, because it was never going to be on disk — asking made a\n// configuration with an empty filter compile on every build.\n//\n// \"Since\" is measured against what that compile read wherever it left a\n// record, in this process or persisted by an earlier one. The record holds the\n// newest source it saw before reading, and a source newer than that is one it\n// never read. Comparing against the output instead lost a save that landed\n// while a rebuild ran, because the rebuild wrote its output after the save.\n// It also never skipped again once an edit left one destination unchanged:\n// the atomic write keeps a byte-identical file's mtime, so the edited source\n// stayed newer than it for good. Only with no record at all is the output\n// compared.\n//\n// Conservative in every direction it can be: anything it cannot establish —\n// a destination that is missing, a source it cannot stat, a configuration\n// declaring no destinations at all — is a reason to build rather than to\n// skip.\nexport function isUpToDate(\n {\n destinationRecords,\n fingerprint,\n newestSource,\n }: {\n destinationRecords: ReadonlyMap<string, DestinationRecord>\n fingerprint: null | string\n newestSource: null | number\n },\n item: ResolvedConfig,\n configObj: Config,\n only?: string[],\n): boolean {\n // An action writes what no `destination` names, so there is nothing for\n // the comparison below to check and skipping would leave its work undone.\n const hasActions = Object.values(configObj.platforms ?? {}).some(\n (platform) => (platform.actions?.length ?? 0) > 0,\n )\n if (hasActions) return false\n\n const destinations = declaredDestinations(configObj, only)\n if (destinations.length === 0) return false\n\n if (newestSource === null) return false\n\n for (const destination of destinations) {\n const record = destinationRecords.get(destination)\n const writtenByThis =\n fingerprint !== null && record?.fingerprint === fingerprint\n\n // Recorded against something else: another configuration, or this one\n // before a hook it names changed or Style Dictionary was upgraded. The\n // file holds that compile's output, and nothing the comparison below\n // reads can say so — which is why a configuration given as a path is\n // held to this as much as one given as an object.\n if (record !== undefined && !writtenByThis) return false\n\n // A configuration given as a path has its own file among the sources, so\n // an edit to it is accounted for and the skip holds across processes. One\n // given as an object or a function has not, so it skips only over files\n // this process wrote for it. Having built it at some point is not enough:\n // an edit that was then undone left the edit's output standing, and so\n // did a build of it that threw partway. An earlier process's record is not\n // enough either, which is why the first compile of a process builds it:\n // its functions are fingerprinted by their source, and what they close\n // over can differ from one process to the next.\n if (!item.file && (!writtenByThis || record.persisted === true)) {\n return false\n }\n\n // Declared and left unwritten by this configuration's last compile. There\n // is no file to stat, so all that can have changed is what it reads.\n if (writtenByThis && record.written === null) {\n if (record.newestSource === null || newestSource > record.newestSource) {\n return false\n }\n continue\n }\n\n const stats = statOrNull(destination)\n if (!stats) return false\n\n // The file has moved since this configuration wrote it, so something\n // wrote it afterwards and nothing here can say what it holds. Its mtime\n // is no evidence either way: an edit to generated output is newer than\n // every source, and would have been kept.\n if (writtenByThis && record.written !== stats.mtimeMs) return false\n\n const lastRead = writtenByThis ? record.newestSource : null\n\n if (\n lastRead === null\n ? stats.mtimeMs <= newestSource\n : newestSource > lastRead\n ) {\n return false\n }\n }\n\n return true\n}\n\n// The newest mtime among the files a configuration reads, or `null` where that\n// cannot be established: a source that cannot be stat'ed, or patterns that\n// matched no file at all.\n//\n// Asked before a compile reads anything, and recorded against what it writes,\n// so that the next `isUpToDate` compares against what the compile actually\n// read. A save that lands afterwards is newer than this, however much newer\n// the compile's own output turns out to be. Both sides of that comparison are\n// mtimes of the files themselves, so no clock is involved and nothing depends\n// on how finely the filesystem stamps a write.\nexport async function newestSourceOf(\n {\n log,\n root,\n watch,\n }: {\n log: Log\n root: string\n watch: UnpluginStyleDictionaryOptions['watch']\n },\n item: ResolvedConfig,\n configObj: Config,\n): Promise<null | number> {\n // `options.watch` belongs in here as much as `source` does. A consumer\n // names an extra file because something in the build reads it — a custom\n // format's own data file, most obviously — and leaving it out let a change\n // to it be skipped over while the watcher dutifully reported it.\n const read = sourcePatternsOf(configObj)\n const extra = watchOptionPatterns(root, watch)\n\n const sources = await expandPatterns(\n {\n globs: [...read.globs, ...extra.globs],\n literals: [...read.literals, ...extra.literals],\n },\n log,\n )\n if (item.file) sources.push(item.file.replace(/\\\\/g, '/'))\n\n if (sources.length === 0) return null\n\n let newestSource = -Infinity\n let sawFile = false\n\n for (const source of sources) {\n const stats = statOrNull(source)\n if (!stats) return null\n\n // Directories are in this list on purpose — `expandPatterns` registers\n // each pattern's static parent so a token file created later is\n // noticed — but their mtime cannot be read as an input signal here. A\n // directory's mtime moves whenever an entry is added or renamed inside\n // it, and the atomic write renames every generated file into place, so\n // a `buildPath` inside a watched directory made the build itself the\n // newest thing the comparison could see. Nothing was ever up to date.\n if (stats.isDirectory()) continue\n\n sawFile = true\n newestSource = Math.max(newestSource, stats.mtimeMs)\n }\n\n // Every pattern expanded to directories alone, so nothing was actually\n // read. Style Dictionary would build an empty dictionary from that, and a\n // skip would present the empty result as current.\n return sawFile ? newestSource : null\n}\n\n// The records an earlier process persisted, each marked as such. Anything\n// that does not read as this version's records — a missing file, one another\n// version wrote, one cut short — is no records at all, which costs a compile\n// and never a wrong skip.\nexport function readPersistedRecords(\n file: string,\n): Map<string, DestinationRecord> {\n const records = new Map<string, DestinationRecord>()\n\n let parsed: unknown\n try {\n parsed = JSON.parse(fs.readFileSync(file, 'utf8'))\n } catch {\n return records\n }\n\n if (\n typeof parsed !== 'object' ||\n parsed === null ||\n !('version' in parsed) ||\n parsed.version !== RECORDS_VERSION ||\n !('records' in parsed) ||\n typeof parsed.records !== 'object' ||\n parsed.records === null\n ) {\n return records\n }\n\n const stored = parsed.records\n for (const destination of Object.keys(stored)) {\n const value: unknown = Reflect.get(stored, destination)\n if (\n typeof value === 'object' &&\n value !== null &&\n 'fingerprint' in value &&\n typeof value.fingerprint === 'string' &&\n 'newestSource' in value &&\n (value.newestSource === null || typeof value.newestSource === 'number') &&\n 'written' in value &&\n (value.written === null || typeof value.written === 'number')\n ) {\n records.set(destination, {\n fingerprint: value.fingerprint,\n newestSource: value.newestSource,\n persisted: true,\n written: value.written,\n })\n }\n }\n\n return records\n}\n\n// The record a compile leaves for one destination it declared. One it wrote\n// is recorded with the file's mtime, and one Style Dictionary declined to write\n// with `written: null`. `null` comes back where a file it wrote is not there\n// to vouch for, so nothing is recorded for it and the next build compiles.\nexport function recordOf(\n destination: string,\n fingerprint: string,\n newestSource: null | number,\n wrote: boolean,\n): DestinationRecord | null {\n if (!wrote) return { fingerprint, newestSource, written: null }\n\n const stats = statOrNull(destination)\n return stats ? { fingerprint, newestSource, written: stats.mtimeMs } : null\n}\n\n// Where one project's records persist: under the nearest `package.json`'s\n// `node_modules/.cache`, the directory build tools share for this, and where\n// Vite keeps its own cache. The nearest package rather than `root`, because a\n// host's root can be a subdirectory — Vite's `root: 'src'` — and a cache\n// there would put a `node_modules` inside the consumer's sources. No `.json`\n// extension, so no `source` glob a consumer writes can read it as tokens.\nexport function recordsFileFor(root: string): string {\n let directory = root\n while (!fs.existsSync(path.join(directory, 'package.json'))) {\n const parent = path.dirname(directory)\n if (parent === directory) {\n directory = root\n break\n }\n directory = parent\n }\n\n return path.join(\n directory,\n 'node_modules',\n '.cache',\n 'unplugin-style-dictionary',\n 'records',\n )\n}\n\n// Merges what one compile recorded into the persisted records.\n//\n// Read again right before the write rather than trusted from the load, so a\n// record another process added since is kept. Two processes writing at once\n// can still lose one's entries, and that only costs a compile on the next\n// start. An entry whose destination is gone is dropped, which bounds the file\n// by the files that exist — and for a file the compile left unwritten, which is\n// never there, by the directory it would have been written into. Written\n// through a temporary file and a rename, so a reader never sees half of it.\nexport function writePersistedRecords(\n file: string,\n recorded: ReadonlyMap<string, DestinationRecord>,\n): void {\n const merged = readPersistedRecords(file)\n for (const [destination, record] of recorded) merged.set(destination, record)\n\n const records: Record<string, Omit<DestinationRecord, 'persisted'>> = {}\n for (const [destination, { fingerprint, newestSource, written }] of merged) {\n const stillThere =\n written === null\n ? statOrNull(path.dirname(destination))\n : statOrNull(destination)\n if (stillThere) {\n records[destination] = { fingerprint, newestSource, written }\n }\n }\n\n const temporary = `${file}.${String(process.pid)}.tmp`\n try {\n fs.mkdirSync(path.dirname(file), { recursive: true })\n fs.writeFileSync(\n temporary,\n JSON.stringify({ records, version: RECORDS_VERSION }),\n )\n fs.renameSync(temporary, file)\n } catch {\n // Only a cache: failing to write it costs a compile on the next start,\n // and never this build.\n try {\n fs.rmSync(temporary, { force: true })\n } catch {\n // Nothing more to do about a temporary file that cannot be removed.\n }\n }\n}\n\n// Where Style Dictionary writes one file, as an absolute path.\n//\n// It joins the destination onto the platform's `buildPath` when there is one\n// and writes the result relative to the working directory. **Joins, not\n// resolves**: an absolute destination lands under the build path rather than\n// replacing it. Measured with `buildPath: 'gen/'` and an absolute destination,\n// Style Dictionary wrote `gen/<that path>`, while the plugin had named the\n// absolute path on its own — a file that was never written.\n//\n// `root` has no say. It is where a relative `config` path is looked up and\n// nothing more, and reading `buildPath` against it named files that did not\n// exist whenever the two differed (#362). Style Dictionary joins with\n// `path-unified/posix`; the platform's own `join` names the same file, and\n// resolving the result is what normalises the separators either way.\nexport function writtenDestination(\n buildPath: string | undefined,\n destination: string,\n): string {\n return path.resolve(\n process.cwd(),\n buildPath ? path.join(buildPath, destination) : destination,\n )\n}\n\n// The hooks a configuration names by string, each as the registry holds it\n// now: the parsers and preprocessors it lists, each platform's transforms and\n// the transforms its `transformGroup` expands to, and each file's `format`,\n// `filter` and `fileHeader`. A hook given inline is part of the configuration\n// already. The configuration's own `hooks` win over the registry, as they do\n// when Style Dictionary builds.\n//\n// **Only the hooks it names, never the whole registry.** The registry is\n// shared by the whole process, so fingerprinting all of it would let one\n// configuration's `register*` call defeat every other configuration's skip.\nfunction namedHooksOf(\n configObj: Config,\n): Record<string, Record<string, unknown>> {\n const own: Hooks = configObj.hooks ?? {}\n const registry = StyleDictionary.hooks\n const named: Record<string, Record<string, unknown>> = {}\n\n const add = (kind: keyof Hooks, name: unknown) => {\n if (typeof name !== 'string') return\n const hook: unknown = own[kind]?.[name] ?? registry[kind][name]\n named[kind] ??= {}\n named[kind][name] = hook ?? null\n }\n\n for (const name of configObj.parsers ?? []) add('parsers', name)\n for (const name of configObj.preprocessors ?? []) add('preprocessors', name)\n\n for (const platform of Object.values(configObj.platforms ?? {})) {\n for (const name of platform.preprocessors ?? []) add('preprocessors', name)\n for (const transform of platform.transforms ?? []) {\n add('transforms', transform)\n }\n\n const group = platform.transformGroup\n if (group !== undefined) {\n add('transformGroups', group)\n\n // `unknown`, because a group nobody registered is `undefined` here\n // whatever the registry's type says.\n const members: unknown =\n own.transformGroups?.[group] ?? registry.transformGroups[group]\n if (Array.isArray(members)) {\n for (const name of members) add('transforms', name)\n }\n }\n\n add('fileHeaders', platform.options?.fileHeader)\n for (const file of platform.files ?? []) {\n add('formats', file.format)\n add('filters', file.filter)\n add('fileHeaders', file.options?.fileHeader)\n }\n }\n\n return named\n}\n\n// `fs.statSync` without the throw. A file that is missing, or that cannot be\n// read, is the same answer to every caller here: nothing to compare against.\nfunction statOrNull(file: string): fs.Stats | null {\n try {\n return fs.statSync(file)\n } catch {\n return null\n }\n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AAA,GAAW;GACvB,IAAI,mBAAmB,KAAK;GAI5B,MAAM,UACJ,IAAI,kBAAkB,UAAU,SAAS,gBAAgB;GAC3D,IAAI,MAAM,QAAQ,OAAO,GACvB,KAAK,MAAM,QAAQ,SAAS,IAAI,cAAc,IAAI;EAEtD;EAEA,IAAI,eAAe,SAAS,SAAS,UAAU;EAC/C,KAAK,MAAM,QAAQ,SAAS,SAAS,CAAC,GAAG;GACvC,IAAI,WAAW,KAAK,MAAM;GAC1B,IAAI,WAAW,KAAK,MAAM;GAC1B,IAAI,eAAe,KAAK,SAAS,UAAU;EAC7C;CACF;CAEA,OAAO;AACT;AAIA,SAAS,WAAW,MAA+B;CACjD,IAAI;EACF,OAAO,GAAG,SAAS,IAAI;CACzB,QAAQ;EACN,OAAO;CACT;AACF"}
|
package/dist/watch-filter.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import picomatch from "picomatch";
|
|
2
2
|
//#region src/watch-filter.ts
|
|
3
|
-
function isWatchedSource(file,
|
|
4
|
-
|
|
3
|
+
function isWatchedSource(file, { globs, literals }, generatedDestinations) {
|
|
4
|
+
const normalizedFile = file.replace(/\\/g, "/");
|
|
5
|
+
return !generatedDestinations.has(normalizedFile) && (literals.includes(normalizedFile) || matchesWatchedFile(normalizedFile, globs));
|
|
5
6
|
}
|
|
6
7
|
function matchesWatchedFile(file, patterns) {
|
|
7
8
|
const normalizedFile = file.replace(/\\/g, "/");
|
|
8
|
-
return patterns.some((pattern) =>
|
|
9
|
-
const normalizedPattern = pattern.replace(/\\/g, "/");
|
|
10
|
-
return normalizedPattern === normalizedFile || picomatch.isMatch(normalizedFile, normalizedPattern);
|
|
11
|
-
});
|
|
9
|
+
return patterns.some((pattern) => pattern === normalizedFile || picomatch.isMatch(normalizedFile, pattern));
|
|
12
10
|
}
|
|
13
11
|
//#endregion
|
|
14
12
|
export { isWatchedSource, matchesWatchedFile };
|
package/dist/watch-filter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watch-filter.js","names":[],"sources":["../src/watch-filter.ts"],"sourcesContent":["import picomatch from 'picomatch'\n\n// Whether a changed file is a token or config source rather than something\n// this plugin just wrote. Both watch entry points ask through here, so\n// neither can react to its own output.\n//\n// `generatedDestinations` is the plugin instance's own record of what it\n// wrote, so it is handed in rather than held: one module serves every\n// instance in the process, and each one's output is its own.\nexport function isWatchedSource(\n file: string,\n
|
|
1
|
+
{"version":3,"file":"watch-filter.js","names":[],"sources":["../src/watch-filter.ts"],"sourcesContent":["import picomatch from 'picomatch'\n\nimport type { WatchPatterns } from './patterns.js'\n\n// Whether a changed file is a token or config source rather than something\n// this plugin just wrote. Both watch entry points ask through here, so\n// neither can react to its own output.\n//\n// `generatedDestinations` is the plugin instance's own record of what it\n// wrote, so it is handed in rather than held: one module serves every\n// instance in the process, and each one's output is its own.\n//\n// A literal is a path, so it is compared as one. A config file reaches here\n// that way, and so does a token file named without a glob, and neither may be\n// read as a pattern: the project's own directory can hold characters a glob\n// treats as syntax.\nexport function isWatchedSource(\n file: string,\n { globs, literals }: WatchPatterns,\n generatedDestinations: ReadonlySet<string>,\n): boolean {\n const normalizedFile = file.replace(/\\\\/g, '/')\n\n return (\n !generatedDestinations.has(normalizedFile) &&\n (literals.includes(normalizedFile) ||\n matchesWatchedFile(normalizedFile, globs))\n )\n}\n\n// Whether `file` matches one of the resolved config/token watch patterns.\n// Shared by the Vite-specific `configureServer` watcher and the universal\n// `watchChange` hook — both need it, and both must skip files that don't\n// match: without this filter, `watchChange` reacts to *any* changed\n// module-graph file, including this plugin's own generated output (since\n// consuming code imports it). Every regenerate is itself a \"change\", which\n// without filtering re-triggers a rebuild forever.\n//\n// Matching a pattern is only half of that, and this function is only the half\n// it can answer. A `buildPath` inside a `source` directory is a supported\n// layout, and under any correct matcher its output matches the very glob that\n// produced it — so the caller also subtracts what the last build wrote. See\n// `isWatchedSource` above, and `generatedDestinations` in the factory.\n//\n// The patterns are Style Dictionary's own `source` and `include` globs, so the\n// filter has to admit exactly what the build reads — which is why the matching\n// is a real globber's rather than hand-rolled. The version this replaces was\n// wrong in both directions at once: it stripped `/**` out of a pattern and\n// prefix-matched the remainder, so `tokens/**/*.json` matched nothing sitting\n// directly in `tokens/` and `tokens/**` matched a `tokens-backup/` sibling,\n// while its regex branch mapped every `*` to `.*` — crossing `/` — and tested\n// it unanchored, so generated output under a watched directory matched its own\n// source glob and rebuilt forever.\n//\n// picomatch rather than `path.matchesGlob`, which would need no dependency at\n// all: that function is documented experimental, and on Node 20 — the floor\n// `engines` declares — it prints `ExperimentalWarning: glob is an experimental\n// feature and might change at any time` into the consumer's build output. The\n// dependency is free in practice, since `unplugin` depends on the same\n// picomatch and is already installed wherever this plugin is. Its `dot: false`\n// default is deliberate: it is what glob, and so Style Dictionary, reads\n// sources with, so a dotfile is invisible to the filter and to the build alike.\n//\n// **The patterns are used exactly as given.** A backslash in one is an escape:\n// the part of a pattern the plugin prefixed is escaped, and every producer\n// already writes separators as `/`. Rewriting backslashes to slashes here\n// turned each escape into a separator, so a project in `Dropbox (Personal)`\n// matched none of its own token files.\nexport function matchesWatchedFile(file: string, patterns: string[]): boolean {\n const normalizedFile = file.replace(/\\\\/g, '/')\n\n return patterns.some(\n (pattern) =>\n // An absolute path a consumer wrote with a glob character in it is filed\n // as a glob, and still names itself.\n pattern === normalizedFile || picomatch.isMatch(normalizedFile, pattern),\n )\n}\n"],"mappings":";;AAgBA,SAAgB,gBACd,MACA,EAAE,OAAO,YACT,uBACS;CACT,MAAM,iBAAiB,KAAK,QAAQ,OAAO,GAAG;CAE9C,OACE,CAAC,sBAAsB,IAAI,cAAc,MACxC,SAAS,SAAS,cAAc,KAC/B,mBAAmB,gBAAgB,KAAK;AAE9C;AAwCA,SAAgB,mBAAmB,MAAc,UAA6B;CAC5E,MAAM,iBAAiB,KAAK,QAAQ,OAAO,GAAG;CAE9C,OAAO,SAAS,MACb,YAGC,YAAY,kBAAkB,UAAU,QAAQ,gBAAgB,OAAO,CAC3E;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kanso-labs/unplugin-style-dictionary",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.13",
|
|
4
4
|
"description": "Compile Style Dictionary design tokens ahead of your bundler (Vite, Rolldown, Rollup, or Webpack) from a single unplugin-based plugin, with automatic watching and rebuilding under Vite",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"unplugin",
|
|
@@ -84,30 +84,30 @@
|
|
|
84
84
|
"@commitlint/config-conventional": "21.2.3",
|
|
85
85
|
"@eslint/js": "10.0.1",
|
|
86
86
|
"@rspack/core": "2.2.7",
|
|
87
|
-
"@types/node": "26.6.
|
|
87
|
+
"@types/node": "26.6.3",
|
|
88
88
|
"@types/picomatch": "4.0.3",
|
|
89
89
|
"@types/semver": "7.8.0",
|
|
90
|
-
"@vitest/coverage-v8": "5.0.
|
|
90
|
+
"@vitest/coverage-v8": "5.0.2",
|
|
91
91
|
"eslint": "10.11.0",
|
|
92
92
|
"eslint-plugin-oxlint": "1.85.0",
|
|
93
93
|
"eslint-plugin-perfectionist": "5.12.1",
|
|
94
94
|
"globals": "17.12.0",
|
|
95
95
|
"husky": "9.1.7",
|
|
96
|
-
"lint-staged": "17.
|
|
96
|
+
"lint-staged": "17.6.0",
|
|
97
97
|
"oxfmt": "0.70.0",
|
|
98
98
|
"oxlint": "1.85.0",
|
|
99
|
-
"oxlint-tsgolint": "7.0.
|
|
99
|
+
"oxlint-tsgolint": "7.0.2003",
|
|
100
100
|
"publint": "0.3.24",
|
|
101
101
|
"rimraf": "6.1.3",
|
|
102
|
-
"rolldown": "1.2.
|
|
103
|
-
"rollup": "4.63.
|
|
102
|
+
"rolldown": "1.2.11",
|
|
103
|
+
"rollup": "4.63.5",
|
|
104
104
|
"semver": "7.8.5",
|
|
105
105
|
"style-dictionary": "5.5.5",
|
|
106
106
|
"tsdown": "0.23.0",
|
|
107
107
|
"typescript": "6.0.3",
|
|
108
108
|
"typescript-eslint": "8.70.1",
|
|
109
|
-
"vite": "8.3.
|
|
110
|
-
"vitest": "5.0.
|
|
109
|
+
"vite": "8.3.1",
|
|
110
|
+
"vitest": "5.0.2",
|
|
111
111
|
"webpack": "5.111.1"
|
|
112
112
|
},
|
|
113
113
|
"peerDependencies": {
|