@fluentic/style 0.1.0-beta.17 → 0.1.0-beta.18

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.
Files changed (82) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/compiler/compiler/cache_identity.ts +115 -0
  3. package/compiler/compiler/compiler.ts +21 -1
  4. package/compiler/compiler/persistent_cache.ts +310 -0
  5. package/compiler/compiler/types.ts +12 -0
  6. package/compiler/extract/collector.ts +2 -2
  7. package/compiler/transform/evaluator/metadata_cache.ts +124 -0
  8. package/compiler/transform/evaluator/tracer.ts +71 -63
  9. package/compiler/transform/extract/plugin.ts +2 -0
  10. package/compiler/utils/check_diagnostics.ts +18 -0
  11. package/compiler/utils/file_resolver.ts +24 -0
  12. package/dist/combine-BpOQWCtM.js.map +1 -1
  13. package/dist/compiler/index.mjs +1 -1
  14. package/dist/{compiler-Cat8TmMk.mjs → compiler-Dpd41bCS.mjs} +552 -41
  15. package/dist/compiler-Dpd41bCS.mjs.map +1 -0
  16. package/dist/entry/rsc-dev.js.map +1 -1
  17. package/dist/{loader-Cvz2pKYV.mjs → loader-CR_NH6ub.mjs} +3 -3
  18. package/dist/{loader-Cvz2pKYV.mjs.map → loader-CR_NH6ub.mjs.map} +1 -1
  19. package/dist/{misc-BKaSZF14.mjs → misc-D5AjOWP-.mjs} +3 -3
  20. package/dist/misc-D5AjOWP-.mjs.map +1 -0
  21. package/dist/plugin/farm/index.mjs +4 -4
  22. package/dist/plugin/farm/index.mjs.map +1 -1
  23. package/dist/plugin/nextjs/index.mjs +5 -5
  24. package/dist/plugin/nextjs/index.mjs.map +1 -1
  25. package/dist/plugin/nextjs/loader.mjs +4 -4
  26. package/dist/plugin/parcel/index.cjs +1 -1
  27. package/dist/plugin/parcel/index.mjs +2 -2
  28. package/dist/plugin/parcel/optimizer.cjs +1 -1
  29. package/dist/plugin/parcel/optimizer.mjs +2 -2
  30. package/dist/plugin/parcel/resolver.cjs +1 -1
  31. package/dist/plugin/parcel/resolver.mjs +2 -2
  32. package/dist/plugin/parcel/runtime.cjs +1 -1
  33. package/dist/plugin/parcel/runtime.mjs +1 -1
  34. package/dist/plugin/parcel/transformer.cjs +1 -1
  35. package/dist/plugin/parcel/transformer.mjs +2 -2
  36. package/dist/plugin/rspack/index.mjs +2 -2
  37. package/dist/plugin/vite/index.mjs +6 -6
  38. package/dist/plugin/vite/index.mjs.map +1 -1
  39. package/dist/plugin/webpack/index.mjs +2 -2
  40. package/dist/plugin/webpack/loader.mjs +2 -2
  41. package/dist/{registry-Bbxm5X90.mjs → registry-CdSHrX-o.mjs} +2 -2
  42. package/dist/{registry-Bbxm5X90.mjs.map → registry-CdSHrX-o.mjs.map} +1 -1
  43. package/dist/{shared-L-63tURh.cjs → shared-CcB1KDFI.cjs} +557 -47
  44. package/dist/shared-CcB1KDFI.cjs.map +1 -0
  45. package/dist/{shared-BVciI9ew.mjs → shared-CobOEWzt.mjs} +5 -5
  46. package/dist/shared-CobOEWzt.mjs.map +1 -0
  47. package/dist/{sidecar-B7Q2P71F.mjs → sidecar-CHDVQqN_.mjs} +3 -3
  48. package/dist/{sidecar-B7Q2P71F.mjs.map → sidecar-CHDVQqN_.mjs.map} +1 -1
  49. package/dist/style-CRs-vfY1.js.map +1 -1
  50. package/dist/types/compiler/compiler/cache_identity.d.ts +4 -0
  51. package/dist/types/compiler/compiler/compiler.d.ts +12 -0
  52. package/dist/types/compiler/compiler/persistent_cache.d.ts +14 -0
  53. package/dist/types/compiler/compiler/types.d.ts +11 -0
  54. package/dist/types/compiler/extract/collector.d.ts +1 -1
  55. package/dist/types/compiler/transform/evaluator/metadata_cache.d.ts +13 -0
  56. package/dist/types/compiler/transform/evaluator/tracer.d.ts +6 -0
  57. package/dist/types/compiler/utils/check_diagnostics.d.ts +7 -0
  58. package/dist/types/compiler/utils/file_resolver.d.ts +4 -0
  59. package/dist/types/plugin/bundler/vite/state.d.ts +24 -0
  60. package/dist/types/plugin/utils/compiler.d.ts +12 -1
  61. package/dist/types/style/tokens.d.ts +1 -1
  62. package/dist/{utils-D2QdzQkX.mjs → utils-CICsXGqS.mjs} +5 -5
  63. package/dist/utils-CICsXGqS.mjs.map +1 -0
  64. package/dist/{utils-D3KR1ht8.mjs → utils-XfsnoteV.mjs} +3 -3
  65. package/dist/{utils-D3KR1ht8.mjs.map → utils-XfsnoteV.mjs.map} +1 -1
  66. package/dist/{webpack-DRegoWgj.mjs → webpack-BAflvDbq.mjs} +6 -6
  67. package/dist/webpack-BAflvDbq.mjs.map +1 -0
  68. package/package.json +1 -1
  69. package/plugin/bundler/farm/plugin.ts +1 -1
  70. package/plugin/bundler/parcel/shared.ts +1 -1
  71. package/plugin/bundler/vite/state.ts +5 -6
  72. package/plugin/bundler/webpack/plugin.ts +1 -1
  73. package/plugin/nextjs/plugin.ts +1 -1
  74. package/plugin/nextjs/utils.ts +1 -1
  75. package/plugin/utils/compiler.ts +1 -2
  76. package/style/tokens.ts +2 -1
  77. package/dist/compiler-Cat8TmMk.mjs.map +0 -1
  78. package/dist/misc-BKaSZF14.mjs.map +0 -1
  79. package/dist/shared-BVciI9ew.mjs.map +0 -1
  80. package/dist/shared-L-63tURh.cjs.map +0 -1
  81. package/dist/utils-D2QdzQkX.mjs.map +0 -1
  82. package/dist/webpack-DRegoWgj.mjs.map +0 -1
@@ -1,10 +1,16 @@
1
+ import { createRequire } from "node:module";
1
2
  import * as crypto from "node:crypto";
3
+ import { createHash, randomUUID } from "node:crypto";
2
4
  import * as fs$1 from "node:fs";
3
5
  import * as path$1 from "node:path";
6
+ import path from "node:path";
4
7
  import * as BabelCore from "@babel/core";
5
8
  import picomatch from "picomatch";
6
9
  import { SelectorType, parse } from "css-what";
7
10
  import { ResolverFactory } from "oxc-resolver";
11
+ import { fileURLToPath } from "node:url";
12
+ import { types } from "node:util";
13
+ import { deserialize, serialize } from "node:v8";
8
14
  //#region compiler/compiler/cache.ts
9
15
  function createCompilerCache(args) {
10
16
  const { cacheDir } = args;
@@ -85,8 +91,8 @@ function createCssCollector() {
85
91
  add(item) {
86
92
  items.push(item);
87
93
  },
88
- addMany(items) {
89
- items.push(...items);
94
+ addMany(rules) {
95
+ items.push(...rules);
90
96
  },
91
97
  getItems() {
92
98
  return items;
@@ -621,7 +627,7 @@ const DEV_CONFIG_DEFAULT = {
621
627
  sourcemapLocationMode: "style",
622
628
  isElementClassNameEnabled: true
623
629
  };
624
- globalData("config.dev.isDev", () => ({ isDev: false }));
630
+ const IS_DEV = globalData("config.dev.isDev", () => ({ isDev: false }));
625
631
  const DEV_CONFIG = globalData("config.dev", () => ({ ...DEV_CONFIG_DEFAULT }));
626
632
  globalData("config.dev.plugin", () => ({ config: null }));
627
633
  globalData("config.dev.base", () => ({ config: null }));
@@ -5981,6 +5987,20 @@ function transformDebug(internal, args, deps) {
5981
5987
  //#endregion
5982
5988
  //#region compiler/utils/file_resolver.ts
5983
5989
  const contentCache = /* @__PURE__ */ new Map();
5990
+ let resolutionObserver = null;
5991
+ /** Synchronous extraction only; restore the previous observer even when extraction throws. */
5992
+ function observeFileResolutions(observer, run) {
5993
+ const previous = resolutionObserver;
5994
+ resolutionObserver = (source, fromFile, result) => {
5995
+ previous?.(source, fromFile, result);
5996
+ observer(source, fromFile, result);
5997
+ };
5998
+ try {
5999
+ return run();
6000
+ } finally {
6001
+ resolutionObserver = previous;
6002
+ }
6003
+ }
5984
6004
  const resolver = new ResolverFactory({
5985
6005
  conditionNames: [
5986
6006
  "import",
@@ -6005,6 +6025,11 @@ function clearResolverCache() {
6005
6025
  resolver.clearCache();
6006
6026
  }
6007
6027
  function resolveFile(source, fromFile) {
6028
+ const result = readResolvedFile(source, fromFile);
6029
+ resolutionObserver?.(source, fromFile, result);
6030
+ return result;
6031
+ }
6032
+ function readResolvedFile(source, fromFile) {
6008
6033
  const filePath = resolveFilePath(source, fromFile);
6009
6034
  if (!filePath) return null;
6010
6035
  let stat;
@@ -7575,15 +7600,221 @@ var LazyBindings = class LazyBindings extends Map {
7575
7600
  }
7576
7601
  };
7577
7602
  //#endregion
7603
+ //#region compiler/compiler/cache_identity.ts
7604
+ let identity;
7605
+ /** Fingerprint actual compiler and dependency bytes, including locally patched packages.
7606
+ * Computed once per process: replacing loaded compiler code requires restarting Node.
7607
+ */
7608
+ function getCompilerCacheIdentity() {
7609
+ if (identity !== void 0) return identity;
7610
+ try {
7611
+ const filename = typeof __filename === "string" ? __filename : fileURLToPath(import.meta.url);
7612
+ const root = findPackageRoot(filename, "@fluentic/style");
7613
+ const hash = createHash("sha256");
7614
+ hash.update(JSON.stringify([
7615
+ process.versions,
7616
+ process.platform,
7617
+ process.arch,
7618
+ process.execArgv,
7619
+ process.env.NODE_OPTIONS,
7620
+ process.env.NODE_ENV,
7621
+ process.env.BABEL_ENV
7622
+ ]));
7623
+ const visited = /* @__PURE__ */ new Set();
7624
+ const hashTree = (directory, source = false) => {
7625
+ for (const item of fs$1.readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
7626
+ if (item.name === "node_modules" || item.name === ".git") continue;
7627
+ const file = path.join(directory, item.name);
7628
+ if (item.isDirectory()) hashTree(file, source);
7629
+ else if (item.isFile()) {
7630
+ if (!(source ? /\.(?:[cm]?[jt]sx?|json|node|wasm)$/ : /\.(?:[cm]?js|json|node|wasm)$/).test(item.name)) continue;
7631
+ hash.update(file).update("\0").update(fs$1.readFileSync(file)).update("\0");
7632
+ } else throw new Error("Unsupported package entry");
7633
+ }
7634
+ };
7635
+ const visit = (directory) => {
7636
+ directory = fs$1.realpathSync(directory);
7637
+ if (visited.has(directory)) return;
7638
+ visited.add(directory);
7639
+ hashTree(directory);
7640
+ const manifest = JSON.parse(fs$1.readFileSync(path.join(directory, "package.json"), "utf8"));
7641
+ const require = createRequire(path.join(directory, "package.json"));
7642
+ const optional = manifest.optionalDependencies ?? {};
7643
+ for (const name of Object.keys({
7644
+ ...manifest.dependencies,
7645
+ ...optional
7646
+ }).sort()) {
7647
+ let dependency;
7648
+ try {
7649
+ dependency = resolvePackageRoot(require, name);
7650
+ } catch (error) {
7651
+ if (name in optional) {
7652
+ hash.update("absent:" + name);
7653
+ continue;
7654
+ }
7655
+ throw error;
7656
+ }
7657
+ visit(dependency);
7658
+ }
7659
+ };
7660
+ const manifest = JSON.parse(fs$1.readFileSync(path.join(root, "package.json"), "utf8"));
7661
+ hash.update(fs$1.readFileSync(path.join(root, "package.json")));
7662
+ if (filename.startsWith(path.join(root, "dist") + path.sep)) hashTree(path.join(root, "dist"));
7663
+ else for (const item of manifest.files) {
7664
+ if (item === "dist") continue;
7665
+ const file = path.join(root, item);
7666
+ if (!fs$1.existsSync(file)) continue;
7667
+ if (fs$1.statSync(file).isDirectory()) hashTree(file, true);
7668
+ else if (/\.[cm]?[jt]sx?$/.test(item)) hash.update(item).update(fs$1.readFileSync(file));
7669
+ }
7670
+ const require = createRequire(path.join(root, "package.json"));
7671
+ for (const name of [
7672
+ "@babel/core",
7673
+ "@jridgewell/trace-mapping",
7674
+ "css-what",
7675
+ "oxc-resolver",
7676
+ "picomatch"
7677
+ ]) visit(resolvePackageRoot(require, name));
7678
+ identity = hash.digest("hex");
7679
+ } catch {
7680
+ identity = null;
7681
+ }
7682
+ return identity;
7683
+ }
7684
+ function resolvePackageRoot(require, name) {
7685
+ for (const directory of require.resolve.paths(name) ?? []) {
7686
+ const root = path.join(directory, name);
7687
+ if (fs$1.existsSync(path.join(root, "package.json"))) return fs$1.realpathSync(root);
7688
+ }
7689
+ try {
7690
+ return path.dirname(require.resolve(name + "/package.json"));
7691
+ } catch {
7692
+ return findPackageRoot(require.resolve(name), name);
7693
+ }
7694
+ }
7695
+ function findPackageRoot(filename, name) {
7696
+ let directory = path.dirname(filename);
7697
+ while (true) {
7698
+ const manifest = path.join(directory, "package.json");
7699
+ if (fs$1.existsSync(manifest) && JSON.parse(fs$1.readFileSync(manifest, "utf8")).name === name) return directory;
7700
+ const parent = path.dirname(directory);
7701
+ if (parent === directory) throw new Error("Package root not found");
7702
+ directory = parent;
7703
+ }
7704
+ }
7705
+ //#endregion
7706
+ //#region compiler/transform/evaluator/metadata_cache.ts
7707
+ const defaultParser = BabelCore.parseSync;
7708
+ const MAX_MEMORY_BYTES = 32 * 1024 * 1024;
7709
+ /** Immutable syntax only. Every consumer gets fresh nodes before annotations/evaluation. */
7710
+ function createTraceMetadataCache(internal) {
7711
+ const memory = /* @__PURE__ */ new Map();
7712
+ let memoryBytes = 0;
7713
+ let memoryVersion = internal.options.cache?.version;
7714
+ const stats = {
7715
+ parses: 0,
7716
+ memoryHits: 0,
7717
+ diskHits: 0,
7718
+ writes: 0
7719
+ };
7720
+ const remember = (filePath, contentHash, json) => {
7721
+ const previous = memory.get(filePath);
7722
+ if (previous) memoryBytes -= previous.json.length * 2;
7723
+ memory.delete(filePath);
7724
+ if (json.length * 2 > MAX_MEMORY_BYTES) return;
7725
+ while (memoryBytes + json.length * 2 > MAX_MEMORY_BYTES) {
7726
+ const oldest = memory.keys().next().value;
7727
+ memoryBytes -= memory.get(oldest).json.length * 2;
7728
+ memory.delete(oldest);
7729
+ }
7730
+ memory.set(filePath, {
7731
+ contentHash,
7732
+ json
7733
+ });
7734
+ memoryBytes += json.length * 2;
7735
+ };
7736
+ const parse = (babel, filePath, content) => {
7737
+ if (!Object.is(memoryVersion, internal.options.cache?.version)) {
7738
+ memory.clear();
7739
+ memoryBytes = 0;
7740
+ memoryVersion = internal.options.cache?.version;
7741
+ }
7742
+ const supportedParser = babel.parseSync === defaultParser;
7743
+ const contentHash = hash(content);
7744
+ const cached = supportedParser ? memory.get(filePath) : void 0;
7745
+ if (cached?.contentHash === contentHash) {
7746
+ stats.memoryHits++;
7747
+ memory.delete(filePath);
7748
+ memory.set(filePath, cached);
7749
+ return JSON.parse(cached.json);
7750
+ }
7751
+ const identity = supportedParser && internal.options.cache?.persistent !== false ? getCompilerCacheIdentity() : null;
7752
+ const filename = identity ? path.join(internal.cacheDir, "trace-metadata-v1", identity, hash(JSON.stringify(internal.options.cache?.version ?? null)), hash(filePath) + ".json") : null;
7753
+ if (filename) try {
7754
+ const record = JSON.parse(fs$1.readFileSync(filename, "utf8"));
7755
+ if (record.version === 1 && record.filePath === filePath && record.contentHash === contentHash && record.ast?.type === "File" && record.ast.program?.type === "Program" && Array.isArray(record.ast.program.body)) {
7756
+ const json = JSON.stringify(record.ast);
7757
+ if (record.checksum === hash(json)) {
7758
+ remember(filePath, contentHash, json);
7759
+ stats.diskHits++;
7760
+ return record.ast;
7761
+ }
7762
+ }
7763
+ } catch {}
7764
+ let ast;
7765
+ try {
7766
+ stats.parses++;
7767
+ ast = babel.parseSync(content, {
7768
+ ...babelTransformOptions(),
7769
+ filename: filePath
7770
+ });
7771
+ } catch {
7772
+ return null;
7773
+ }
7774
+ if (!ast || !supportedParser) return ast;
7775
+ let temporary;
7776
+ try {
7777
+ const json = JSON.stringify(ast, (_key, value) => {
7778
+ if (typeof value === "number" && (!Number.isFinite(value) || Object.is(value, -0)) || typeof value === "bigint" || typeof value === "function" || typeof value === "symbol") throw new Error("Non-JSON syntax data");
7779
+ return value;
7780
+ });
7781
+ remember(filePath, contentHash, json);
7782
+ if (filename) {
7783
+ const record = {
7784
+ version: 1,
7785
+ filePath,
7786
+ contentHash,
7787
+ checksum: hash(json),
7788
+ ast
7789
+ };
7790
+ fs$1.mkdirSync(path.dirname(filename), { recursive: true });
7791
+ temporary = filename + "." + randomUUID() + ".tmp";
7792
+ fs$1.writeFileSync(temporary, JSON.stringify(record), "utf8");
7793
+ fs$1.renameSync(temporary, filename);
7794
+ stats.writes++;
7795
+ }
7796
+ } catch {} finally {
7797
+ if (temporary) try {
7798
+ fs$1.unlinkSync(temporary);
7799
+ } catch {}
7800
+ }
7801
+ return ast;
7802
+ };
7803
+ return {
7804
+ parse,
7805
+ getStats: () => ({ ...stats })
7806
+ };
7807
+ }
7808
+ function hash(content) {
7809
+ return createHash("sha256").update(content).digest("hex");
7810
+ }
7811
+ //#endregion
7578
7812
  //#region compiler/transform/evaluator/tracer.ts
7579
- const CACHE_TYPE = "transform-trace-lazy-v1";
7813
+ const CACHE_TYPE = "transform-trace-links-v2";
7580
7814
  function createTracer(internal) {
7581
7815
  const active = /* @__PURE__ */ new Set();
7582
- const dependencyHashes = /* @__PURE__ */ new Map();
7583
- const getDependencyHash = (filePath) => {
7584
- if (!dependencyHashes.has(filePath)) dependencyHashes.set(filePath, hashFileContent(filePath));
7585
- return dependencyHashes.get(filePath) ?? null;
7586
- };
7816
+ const cyclic = /* @__PURE__ */ new Set();
7817
+ const metadata = createTraceMetadataCache(internal);
7587
7818
  const resolveImport = (babel, source, fromFile) => {
7588
7819
  return traceModuleResult(babel, source, fromFile)?.bindings ?? null;
7589
7820
  };
@@ -7596,27 +7827,43 @@ function createTracer(internal) {
7596
7827
  contentHash,
7597
7828
  cacheType: CACHE_TYPE
7598
7829
  });
7599
- if (cached?.bindings instanceof Map && areDependenciesFresh(cached.dependencies ?? [], getDependencyHash)) return {
7830
+ if (cached?.bindings instanceof Map && areDependenciesFresh(cached.dependencies ?? [])) return {
7831
+ source,
7832
+ fromFile,
7600
7833
  filePath: resolved.filePath,
7601
7834
  contentHash,
7602
7835
  bindings: cached.bindings,
7603
7836
  dependencies: cached.dependencies ?? []
7604
7837
  };
7605
- if (active.has(resolved.filePath)) return null;
7838
+ if (active.has(resolved.filePath)) {
7839
+ for (const file of active) cyclic.add(file);
7840
+ return null;
7841
+ }
7606
7842
  active.add(resolved.filePath);
7607
7843
  try {
7608
7844
  const dependencies = /* @__PURE__ */ new Map();
7609
7845
  const dependencyList = [];
7846
+ const onTrack = () => {
7847
+ dependencyList.splice(0, dependencyList.length, ...dependencies.values());
7848
+ };
7610
7849
  const resolveDependency = (source, fromFile) => {
7850
+ const dependency = resolveFile(source, fromFile);
7851
+ addTraceDependency(dependencies, {
7852
+ source,
7853
+ fromFile,
7854
+ filePath: dependency?.filePath ?? null,
7855
+ contentHash: dependency ? hashContent(dependency.content) : null
7856
+ });
7857
+ onTrack();
7611
7858
  const traced = traceModuleResult(babel, source, fromFile);
7612
7859
  if (!traced) return null;
7613
- return createTrackedBindings(traced, dependencies, () => {
7614
- dependencyList.splice(0, dependencyList.length, ...dependencies.values());
7615
- });
7860
+ return createTrackedBindings(traced, dependencies, onTrack, () => traceModuleResult(babel, source, fromFile));
7616
7861
  };
7617
- const bindings = parseAndExtractModule(internal.projectDir, resolved.filePath, resolved.content, babel, resolveDependency, internal.options);
7862
+ const ast = metadata.parse(babel, resolved.filePath, resolved.content);
7863
+ if (!ast) return null;
7864
+ const bindings = extractModuleBindings(internal.projectDir, resolved.filePath, ast, babel, resolveDependency, internal.options);
7618
7865
  if (!bindings) return null;
7619
- internal.cache.setItem({
7866
+ if (!cyclic.has(resolved.filePath)) internal.cache.setItem({
7620
7867
  filePath: resolved.filePath,
7621
7868
  contentHash,
7622
7869
  cacheType: CACHE_TYPE,
@@ -7627,6 +7874,8 @@ function createTracer(internal) {
7627
7874
  }
7628
7875
  });
7629
7876
  return {
7877
+ source,
7878
+ fromFile,
7630
7879
  filePath: resolved.filePath,
7631
7880
  contentHash,
7632
7881
  bindings,
@@ -7642,22 +7891,14 @@ function createTracer(internal) {
7642
7891
  return {
7643
7892
  resolveImport,
7644
7893
  traceModule,
7894
+ getMetadataCacheStats: metadata.getStats,
7645
7895
  beginCompilation() {
7646
- dependencyHashes.clear();
7896
+ clearResolverCache();
7897
+ cyclic.clear();
7647
7898
  }
7648
7899
  };
7649
7900
  }
7650
- function parseAndExtractModule(projectDir, filePath, content, babel, resolveImport, options) {
7651
- let ast = null;
7652
- try {
7653
- ast = babel.parseSync(content, {
7654
- ...babelTransformOptions(),
7655
- filename: filePath
7656
- });
7657
- } catch {
7658
- return null;
7659
- }
7660
- if (!ast) return null;
7901
+ function extractModuleBindings(projectDir, filePath, ast, babel, resolveImport, options) {
7661
7902
  const imports = /* @__PURE__ */ new Map();
7662
7903
  const rawBindings = /* @__PURE__ */ new Map();
7663
7904
  const bindings = new LazyBindings();
@@ -7926,12 +8167,12 @@ function createSlotRef(fileId, loc) {
7926
8167
  };
7927
8168
  }
7928
8169
  function hashContent(content) {
7929
- return crypto.createHash("sha256").update(content).digest("hex").slice(0, 16);
8170
+ return crypto.createHash("sha256").update(content).digest("hex");
7930
8171
  }
7931
8172
  function addTraceDependency(dependencies, dependency) {
7932
- dependencies.set(dependency.filePath, dependency);
8173
+ dependencies.set(JSON.stringify([dependency.source, dependency.fromFile]), dependency);
7933
8174
  }
7934
- function createTrackedBindings(traced, dependencies, onTrack) {
8175
+ function createTrackedBindings(traced, dependencies, onTrack, refresh) {
7935
8176
  const track = () => {
7936
8177
  addTraceDependency(dependencies, traced);
7937
8178
  traced.dependencies.forEach((dependency) => addTraceDependency(dependencies, dependency));
@@ -7939,24 +8180,39 @@ function createTrackedBindings(traced, dependencies, onTrack) {
7939
8180
  };
7940
8181
  const bindings = new LazyBindings();
7941
8182
  for (const key of traced.bindings.keys()) bindings.setLazy(key, () => {
7942
- const value = traced.bindings.get(key);
8183
+ const current = refresh();
8184
+ if (!current) return evalFail(`Cannot resolve export: ${key}`);
8185
+ traced = current;
8186
+ const value = traced.bindings.get(key) ?? evalFail(`Cannot resolve export: ${key}`);
7943
8187
  track();
7944
8188
  return value;
7945
8189
  });
7946
8190
  track();
7947
8191
  return bindings;
7948
8192
  }
7949
- function areDependenciesFresh(dependencies, getHash) {
7950
- for (const dependency of dependencies) if (getHash(dependency.filePath) !== dependency.contentHash) return false;
8193
+ function areDependenciesFresh(dependencies) {
8194
+ for (const dependency of dependencies) {
8195
+ const current = resolveFile(dependency.source, dependency.fromFile);
8196
+ if ((current?.filePath ?? null) !== dependency.filePath || (current ? hashContent(current.content) : null) !== dependency.contentHash) return false;
8197
+ }
7951
8198
  return true;
7952
8199
  }
7953
- function hashFileContent(filePath) {
8200
+ //#endregion
8201
+ //#region compiler/utils/check_diagnostics.ts
8202
+ let observer;
8203
+ /** Extraction is synchronous; restore the previous observer for nested compilations. */
8204
+ function observeCheckDiagnostics(onDiagnostic, run) {
8205
+ const previous = observer;
8206
+ observer = onDiagnostic;
7954
8207
  try {
7955
- return hashContent(fs$1.readFileSync(filePath, "utf8"));
7956
- } catch {
7957
- return null;
8208
+ return run();
8209
+ } finally {
8210
+ observer = previous;
7958
8211
  }
7959
8212
  }
8213
+ function recordCheckDiagnostic(diagnostic) {
8214
+ observer?.(diagnostic);
8215
+ }
7960
8216
  //#endregion
7961
8217
  //#region compiler/utils/imports.ts
7962
8218
  function getStyleRuntimeImportPath(mode) {
@@ -8585,6 +8841,10 @@ function createExtractPlugin(args) {
8585
8841
  }
8586
8842
  if (!result) return;
8587
8843
  checkCompiledImportantUsage(result, options.check?.important, (level, message) => {
8844
+ recordCheckDiagnostic({
8845
+ level,
8846
+ message
8847
+ });
8588
8848
  if (level === "error") throw path.buildCodeFrameError(message);
8589
8849
  console.warn("[fluentic-style] " + message);
8590
8850
  });
@@ -8927,11 +9187,248 @@ function createPlugin(rewriteSource) {
8927
9187
  });
8928
9188
  }
8929
9189
  //#endregion
9190
+ //#region compiler/compiler/persistent_cache.ts
9191
+ function createPersistentTransformCache(internal) {
9192
+ const stats = {
9193
+ hits: 0,
9194
+ misses: 0,
9195
+ bypasses: 0,
9196
+ writes: 0
9197
+ };
9198
+ const directory = path.join(internal.cacheDir, "extract-v1");
9199
+ let previousContext = null;
9200
+ const contextFor = () => {
9201
+ if (internal.options.cache?.persistent === false || DEV_CONFIG.isDev) return null;
9202
+ try {
9203
+ const identity = getCompilerCacheIdentity();
9204
+ if (!identity) return null;
9205
+ const options = internal.options;
9206
+ assertDataProperties(options);
9207
+ if (Object.getPrototypeOf(options) !== Object.prototype && Object.getPrototypeOf(options) !== null) throw new Error("Non-plain compiler options");
9208
+ if (options.importSources) assertPlainArray(options.importSources);
9209
+ const importSources = options.importSources?.map((entry) => {
9210
+ assertDataProperties(entry);
9211
+ if (Object.getPrototypeOf(entry) !== Object.prototype && Object.getPrototypeOf(entry) !== null) throw new Error("Non-plain import source");
9212
+ if (entry.styleFn && types.isProxy(entry.styleFn)) throw new Error("Proxy style function");
9213
+ if (!entry.meta && entry.styleFn) {
9214
+ const metadata = Object.getOwnPropertyDescriptor(entry.styleFn, symbol("style.fn:meta"));
9215
+ if (!metadata || !("value" in metadata)) throw new Error("Dynamic style metadata");
9216
+ }
9217
+ return {
9218
+ ...entry,
9219
+ styleFn: void 0,
9220
+ meta: entry.meta ?? (entry.styleFn ? getStyleFnMeta(entry.styleFn) : null)
9221
+ };
9222
+ });
9223
+ return digest(encode({
9224
+ schema: 1,
9225
+ identity,
9226
+ cwd: process.cwd(),
9227
+ projectDir: internal.projectDir,
9228
+ runtimeMode: internal.runtimeMode,
9229
+ options: {
9230
+ ...options,
9231
+ importSources
9232
+ },
9233
+ globals: {
9234
+ CSS_CONFIG,
9235
+ CSS_EXTRA_CONFIG,
9236
+ DEBUG_CONFIG,
9237
+ DEV_CONFIG,
9238
+ IS_DEV,
9239
+ RUNTIME_CONFIG,
9240
+ CHECK_CONFIG,
9241
+ DEFAULT_CONFIG,
9242
+ PrioritySelectors
9243
+ }
9244
+ }, options.cache?.dynamicConfig !== false));
9245
+ } catch {
9246
+ return null;
9247
+ }
9248
+ };
9249
+ const run = (args, compile) => {
9250
+ const context = contextFor();
9251
+ let key = null;
9252
+ try {
9253
+ if (context) key = digest(encode({
9254
+ context,
9255
+ args
9256
+ }));
9257
+ } catch {}
9258
+ if (!key) {
9259
+ stats.bypasses++;
9260
+ previousContext = null;
9261
+ internal.cache.clear();
9262
+ clearResolverCache();
9263
+ return {
9264
+ result: compile(),
9265
+ hit: false
9266
+ };
9267
+ }
9268
+ const filename = path.join(directory, getCompilerCacheIdentity(), digest(encode(internal.options.cache?.version)), digest(args.filePath) + ".bin");
9269
+ clearResolverCache();
9270
+ let hit;
9271
+ try {
9272
+ const buffer = fs$1.readFileSync(filename);
9273
+ const body = buffer.subarray(65);
9274
+ if (buffer.subarray(0, 64).toString() === digest(body)) {
9275
+ const record = deserialize(body);
9276
+ if (record.key === key && Array.isArray(record.dependencies) && validRecord(record) && record.dependencies.every(isFresh)) hit = record;
9277
+ }
9278
+ } catch {}
9279
+ if (hit) {
9280
+ stats.hits++;
9281
+ for (const diagnostic of hit.diagnostics) if (diagnostic.level === "warn") console.warn("[fluentic-style] " + diagnostic.message);
9282
+ if (hit.error) {
9283
+ const error = new Error(hit.error.message);
9284
+ error.name = hit.error.name;
9285
+ error.stack = hit.error.stack;
9286
+ throw error;
9287
+ }
9288
+ return {
9289
+ result: hit.result,
9290
+ hit: true
9291
+ };
9292
+ }
9293
+ stats.misses++;
9294
+ clearResolverCache();
9295
+ if (context !== previousContext) internal.cache.clear();
9296
+ previousContext = context;
9297
+ const dependencies = /* @__PURE__ */ new Map();
9298
+ const diagnostics = [];
9299
+ let result = null;
9300
+ let failure = null;
9301
+ try {
9302
+ result = observeCheckDiagnostics((diagnostic) => diagnostics.push(diagnostic), () => observeFileResolutions((source, fromFile, resolved) => {
9303
+ dependencies.set(JSON.stringify([source, fromFile]), {
9304
+ source,
9305
+ fromFile,
9306
+ filePath: resolved?.filePath ?? null,
9307
+ hash: resolved ? digest(resolved.content) : null
9308
+ });
9309
+ }, compile));
9310
+ } catch (error) {
9311
+ if (!(error instanceof Error) || diagnostics[diagnostics.length - 1]?.level !== "error") throw error;
9312
+ failure = error;
9313
+ }
9314
+ const cachedError = failure ? {
9315
+ name: failure.name,
9316
+ message: failure.message,
9317
+ stack: failure.stack
9318
+ } : null;
9319
+ if (result || failure) {
9320
+ let temporary;
9321
+ try {
9322
+ encode(result);
9323
+ const records = [...dependencies.values()];
9324
+ clearResolverCache();
9325
+ if (!records.every(isFresh) || contextFor() !== context) throw new Error("Cache snapshot changed");
9326
+ const body = serialize({
9327
+ key,
9328
+ dependencies: records,
9329
+ result,
9330
+ diagnostics,
9331
+ error: cachedError
9332
+ });
9333
+ fs$1.mkdirSync(path.dirname(filename), { recursive: true });
9334
+ temporary = filename + "." + randomUUID() + ".tmp";
9335
+ fs$1.writeFileSync(temporary, Buffer.concat([Buffer.from(digest(body) + "\n"), body]));
9336
+ fs$1.renameSync(temporary, filename);
9337
+ stats.writes++;
9338
+ } catch {} finally {
9339
+ if (temporary) try {
9340
+ fs$1.unlinkSync(temporary);
9341
+ } catch {}
9342
+ }
9343
+ }
9344
+ if (failure) throw failure;
9345
+ return {
9346
+ result,
9347
+ hit: false
9348
+ };
9349
+ };
9350
+ return {
9351
+ run,
9352
+ getStats: () => ({ ...stats })
9353
+ };
9354
+ }
9355
+ function isFresh(dependency) {
9356
+ if (typeof dependency.source !== "string" || typeof dependency.fromFile !== "string") return false;
9357
+ const resolved = resolveFile(dependency.source, dependency.fromFile);
9358
+ return (resolved?.filePath ?? null) === dependency.filePath && (resolved ? digest(resolved.content) : null) === dependency.hash;
9359
+ }
9360
+ function validRecord(record) {
9361
+ if (!Array.isArray(record.diagnostics) || !record.diagnostics.every((item) => item && (item.level === "warn" || item.level === "error") && typeof item.message === "string")) return false;
9362
+ if (record.error) return record.result === null && record.diagnostics[record.diagnostics.length - 1]?.level === "error" && typeof record.error.name === "string" && typeof record.error.message === "string" && (record.error.stack === void 0 || typeof record.error.stack === "string");
9363
+ return record.error === null && record.diagnostics.every((item) => item.level === "warn") && validResult(record.result);
9364
+ }
9365
+ function validResult(result) {
9366
+ return !!result && typeof result.code === "string" && (result.sourcemap === null || typeof result.sourcemap === "string") && Array.isArray(result.rules) && result.rules.every((rule) => typeof rule.css === "string" && typeof rule.dedupe === "string" && typeof rule.className === "string" && Array.isArray(rule.priority));
9367
+ }
9368
+ function digest(value) {
9369
+ return createHash("sha256").update(value).digest("hex");
9370
+ }
9371
+ function assertDataProperties(value) {
9372
+ if (types.isProxy(value)) throw new Error("Proxy configuration");
9373
+ for (const key of Reflect.ownKeys(value)) {
9374
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
9375
+ if (typeof key === "symbol" || !("value" in descriptor) || !descriptor.enumerable && !(Array.isArray(value) && key === "length") && !(value instanceof RegExp && key === "lastIndex")) throw new Error("Dynamic configuration");
9376
+ }
9377
+ }
9378
+ function assertPlainArray(value) {
9379
+ assertDataProperties(value);
9380
+ if (Object.getPrototypeOf(value) !== Array.prototype || Object.keys(value).some((key) => !/^(0|[1-9][0-9]*)$/.test(key))) throw new Error("Non-plain array");
9381
+ }
9382
+ /** Tagged, order-preserving encoding: property order can affect generated CSS. */
9383
+ function encode(value, dynamicConfig = false) {
9384
+ return JSON.stringify(encodeValue(value, /* @__PURE__ */ new Set(), dynamicConfig));
9385
+ }
9386
+ function encodeValue(value, seen = /* @__PURE__ */ new Set(), dynamicConfig = false) {
9387
+ if (typeof value === "function" && dynamicConfig) {
9388
+ if (types.isProxy(value)) throw new Error("Proxy callback");
9389
+ for (const key of Reflect.ownKeys(value)) {
9390
+ if (typeof key !== "string" || ![
9391
+ "length",
9392
+ "name",
9393
+ "prototype",
9394
+ "arguments",
9395
+ "caller"
9396
+ ].includes(key)) throw new Error("Callback with custom properties");
9397
+ if (!("value" in Object.getOwnPropertyDescriptor(value, key))) throw new Error("Callback accessor");
9398
+ }
9399
+ const source = Function.prototype.toString.call(value);
9400
+ if (source.includes("[native code]")) throw new Error("Opaque callback");
9401
+ return ["function", source];
9402
+ }
9403
+ if (value === void 0) return ["undefined"];
9404
+ if (value === null || typeof value === "string" || typeof value === "boolean") return value;
9405
+ if (typeof value === "number" && Number.isFinite(value)) return ["number", Object.is(value, -0) ? "-0" : value];
9406
+ if (typeof value !== "object" || value === null || seen.has(value)) throw new Error("Unsupported cache input");
9407
+ assertDataProperties(value);
9408
+ if (value instanceof RegExp) {
9409
+ if (value.global || value.sticky || Object.getPrototypeOf(value) !== RegExp.prototype || Object.keys(value).length) throw new Error("Dynamic pattern");
9410
+ return [
9411
+ "regexp",
9412
+ value.source,
9413
+ value.flags
9414
+ ];
9415
+ }
9416
+ if (Array.isArray(value)) assertPlainArray(value);
9417
+ if (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw new Error("Non-plain cache input");
9418
+ seen.add(value);
9419
+ try {
9420
+ return [Array.isArray(value) ? ["array", value.length] : "object", Object.keys(value).map((key) => [key, encodeValue(value[key], seen, dynamicConfig)])];
9421
+ } finally {
9422
+ seen.delete(value);
9423
+ }
9424
+ }
9425
+ //#endregion
8930
9426
  //#region compiler/compiler/compiler.ts
8931
9427
  function createCompiler(args, options) {
8932
9428
  const internal = createCompilerInternal(args, options);
8933
9429
  const tracer = createTracer(internal);
8934
9430
  const collector = createCssCollector();
9431
+ const persistentCache = createPersistentTransformCache(internal);
8935
9432
  const compileDebug = (args) => {
8936
9433
  tracer.beginCompilation();
8937
9434
  return rewriteTransformResult(transformDebug(internal, args, { tracer }), internal.runtimeMode, args.code);
@@ -8958,6 +9455,17 @@ function createCompiler(args, options) {
8958
9455
  };
8959
9456
  };
8960
9457
  const compileExtract = (args) => {
9458
+ const startIndex = collector.getItems().length;
9459
+ try {
9460
+ const cached = persistentCache.run(args, () => compileExtractUncached(args));
9461
+ if (cached.hit && cached.result) collector.addMany(cached.result.rules);
9462
+ return cached.result;
9463
+ } catch (error) {
9464
+ collector.getItems().splice(startIndex);
9465
+ throw error;
9466
+ }
9467
+ };
9468
+ const compileExtractUncached = (args) => {
8961
9469
  tracer.beginCompilation();
8962
9470
  const startIndex = collector.getItems().length;
8963
9471
  const result = transformExtract(internal, args, {
@@ -8985,11 +9493,14 @@ function createCompiler(args, options) {
8985
9493
  compileDebugRSC,
8986
9494
  compileExtract,
8987
9495
  getExtractedCss,
8988
- invalidateFile
9496
+ invalidateFile,
9497
+ getPersistentCacheStats: persistentCache.getStats,
9498
+ getTraceMetadataCacheStats: tracer.getMetadataCacheStats
8989
9499
  };
8990
9500
  }
8991
9501
  function createCompilerInternal(args, options) {
8992
- const { projectDir, cacheDir, runtimeMode } = args;
9502
+ const { projectDir, runtimeMode } = args;
9503
+ const cacheDir = options.cache?.location === void 0 ? args.cacheDir : path.resolve(projectDir, options.cache.location);
8993
9504
  return {
8994
9505
  projectDir,
8995
9506
  cacheDir,
@@ -9069,4 +9580,4 @@ const CssPropPresets = {
9069
9580
  //#endregion
9070
9581
  export { getDefaultSourcemapUrl as a, getStyleFnMeta as c, babelTransform as d, extractCss as f, createCompilerCache as g, globalData as h, CompilerRuntimeMode as i, isPromiseLike as l, SIDECAR_URL_GLOBAL_SYMBOL as m, createCompiler as n, normalizeSourcemapSourcePath as o, compareLayerPriority as p, getStyleRuntimeImportPath as r, normalizePath as s, CssPropPresets as t, babelPlugin as u };
9071
9582
 
9072
- //# sourceMappingURL=compiler-Cat8TmMk.mjs.map
9583
+ //# sourceMappingURL=compiler-Dpd41bCS.mjs.map