@mosetta/build 0.0.40 → 0.0.41

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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @mosetta/build
2
2
 
3
+ ## 0.0.41
4
+
5
+ ### Patch Changes
6
+
7
+ - Add per-build `SnapshotFileSystem` memoization over the `FileSystem` port (~2x faster cold builds on large multi-tile palettes with shared barrels, zero stale cache across `dev` rebuilds).
8
+ - @mosetta/router@0.0.41
9
+ - @mosetta/runtime@0.0.41
10
+ - @mosetta/server@0.0.41
11
+ - @mosetta/server-action@0.0.41
12
+ - @mosetta/tar@0.0.41
13
+
3
14
  ## 0.0.40
4
15
 
5
16
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mosetta/build",
3
- "version": "0.0.40",
3
+ "version": "0.0.41",
4
4
  "license": "MIT",
5
5
  "description": "The core of the tile forge: builds a tile palette — its tile side artifacts, their dependency closure, the tile host descriptor. Environment-agnostic — the file system and the bundling engine are supplied from outside.",
6
6
  "type": "module",
@@ -25,11 +25,11 @@
25
25
  "resolve.exports": "^2.0.3",
26
26
  "scheduler": "*",
27
27
  "yaml": "^2.9.0",
28
- "@mosetta/router": "0.0.40",
29
- "@mosetta/runtime": "0.0.40",
30
- "@mosetta/tar": "0.0.40",
31
- "@mosetta/server-action": "0.0.40",
32
- "@mosetta/server": "0.0.40"
28
+ "@mosetta/router": "0.0.41",
29
+ "@mosetta/runtime": "0.0.41",
30
+ "@mosetta/tar": "0.0.41",
31
+ "@mosetta/server": "0.0.41",
32
+ "@mosetta/server-action": "0.0.41"
33
33
  },
34
34
  "publishConfig": {
35
35
  "access": "public"
package/src/build.mjs CHANGED
@@ -10,6 +10,7 @@ import { processCss } from './css.mjs';
10
10
  import { INERT_BUILD_CONFIG } from './buildConfig.mjs';
11
11
  import { formatBytes } from './format.mjs';
12
12
  import { mtimeStamp } from './artifactCache.mjs';
13
+ import { snapshotFileSystem } from './fileSystem.mjs';
13
14
  import { describeSettings, SETTINGS_SUFFIX } from './settings.mjs';
14
15
  import { DocIndex, renderPaletteDocs, renderTileDocs, tileDocHref, tileDocModule } from './docs.mjs';
15
16
 
@@ -100,7 +101,8 @@ async function mapLimit(items, limit, fn) {
100
101
  return results;
101
102
  }
102
103
 
103
- export async function build(root, { mode = 'development', log, fileSystem, resolver, engine, frameworkDir, buildConfig = INERT_BUILD_CONFIG, cache = null, evaluate = null, released = null }) {
104
+ export async function build(root, { mode = 'development', log, fileSystem: rawFileSystem, resolver, engine, frameworkDir, buildConfig = INERT_BUILD_CONFIG, cache = null, evaluate = null, released = null }) {
105
+ const fileSystem = snapshotFileSystem(rawFileSystem);
104
106
  const concurrency = Math.max(1, Number(engine?.concurrency) || 1);
105
107
  const out = new OutputFS();
106
108
  const watchFiles = new Set();
@@ -67,3 +67,94 @@ export class FileSystem {
67
67
  }
68
68
 
69
69
  }
70
+
71
+ function memoResult(map, key, compute) {
72
+ let entry = map.get(key);
73
+ if (!entry) {
74
+ try {
75
+ entry = { ok: true, value: compute() };
76
+ } catch (error) {
77
+ entry = { ok: false, error };
78
+ }
79
+ map.set(key, entry);
80
+ }
81
+ if (!entry.ok) throw entry.error;
82
+ return entry.value;
83
+ }
84
+
85
+ /**
86
+ * Per-build read-only snapshot over any `FileSystem` port.
87
+ *
88
+ * A multi-tile palette frequently probes and reads the same shared files, symlinks, and
89
+ * `package.json` manifests hundreds of thousands of times during one `build()`. Wrapping the port
90
+ * for the duration of a single `build()` eliminates redundant OS syscalls and repeated `JSON.parse`
91
+ * calls while staying completely fresh across subsequent `dev` rebuilds (discarded on return).
92
+ */
93
+ export class SnapshotFileSystem extends FileSystem {
94
+ #inner;
95
+ #exists = new Map();
96
+ #isDir = new Map();
97
+ #realPath = new Map();
98
+ #readDir = new Map();
99
+ #readFile = new Map();
100
+ #readJson = new Map();
101
+ #mtimeMs = new Map();
102
+
103
+ constructor(inner) {
104
+ super();
105
+ this.#inner = inner instanceof SnapshotFileSystem ? inner.#inner : inner;
106
+ }
107
+
108
+ exists(file) {
109
+ if (this.#exists.has(file)) return this.#exists.get(file);
110
+ const value = this.#inner.exists(file);
111
+ this.#exists.set(file, value);
112
+ return value;
113
+ }
114
+
115
+ isDirectory(dir) {
116
+ if (this.#isDir.has(dir)) return this.#isDir.get(dir);
117
+ const value = this.#inner.isDirectory(dir);
118
+ this.#isDir.set(dir, value);
119
+ return value;
120
+ }
121
+
122
+ realPath(file) {
123
+ return memoResult(this.#realPath, file, () => this.#inner.realPath(file));
124
+ }
125
+
126
+ readDir(dir) {
127
+ return memoResult(this.#readDir, dir, () => this.#inner.readDir(dir));
128
+ }
129
+
130
+ /**
131
+ * @param {string} file
132
+ * @param {string | null} [encoding]
133
+ */
134
+ readFile(file, encoding = null) {
135
+ const key = `${encoding ?? ''}\0${file}`;
136
+ return memoResult(this.#readFile, key, () => this.#inner.readFile(file, encoding));
137
+ }
138
+
139
+ readJson(file) {
140
+ return memoResult(this.#readJson, file, () => JSON.parse(this.readFile(file, 'utf8')));
141
+ }
142
+
143
+ mtimeMs(file) {
144
+ if (this.#mtimeMs.has(file)) return this.#mtimeMs.get(file);
145
+ const value = this.#inner.mtimeMs(file);
146
+ this.#mtimeMs.set(file, value);
147
+ return value;
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Create a fresh per-build snapshot wrapping `fileSystem`.
153
+ *
154
+ * @template T
155
+ * @param {T} fileSystem
156
+ * @returns {SnapshotFileSystem}
157
+ */
158
+ export function snapshotFileSystem(fileSystem) {
159
+ return new SnapshotFileSystem(fileSystem);
160
+ }