@uniweb/build 1.23.3 → 1.23.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,195 +0,0 @@
1
- /**
2
- * Do WE and VITE agree on where the foundation is?
3
- *
4
- * ## The bug this exists for
5
- *
6
- * A site build resolves its foundation **twice, by two different mechanisms**,
7
- * and until 2026-09-01 nothing checked that they agreed:
8
- *
9
- * 1. **We resolve a PATH.** `detectFoundationType('src', siteRoot)` returns
10
- * `{ type: 'local', path: <siteRoot>/../src }`, and the
11
- * `uniweb:ensure-foundation-entry` plugin generates `_entry.generated.js`
12
- * into exactly that directory.
13
- * 2. **Vite resolves a NAME.** `site/config.js` sets
14
- * `alias['#foundation'] = foundationInfo.name` — the bare specifier — so
15
- * Vite runs its own node_modules lookup and reads that package's `main`.
16
- *
17
- * Those name the same directory only because a package manager linked
18
- * `node_modules/<name>` to the foundation. Nothing enforces it, and when it does
19
- * not hold we generate into one directory while Vite reads another.
20
- *
21
- * ## ⛔ The failure is worse when it does NOT fail
22
- *
23
- * If the directory Vite reaches has no `_entry.generated.js`, the build dies with
24
- * vite's `[commonjs--resolver] Failed to resolve entry for package "<name>"` —
25
- * opaque, but loud.
26
- *
27
- * **If it has a STALE one, nothing fails at all.** The site builds against a
28
- * different foundation than the one on disk, and reports success. A developer
29
- * editing `src/` sees their change generated into `src/_entry.generated.js` and
30
- * simply not take effect. *(Measured by the `flows` lane 2026-09-01: their
31
- * harness minted fixtures under a `node_modules` that was itself a symlink to a
32
- * seed tree, so a relative `../../src` link resolved into the seed. Every run
33
- * that mutated its foundation and then built a site had been silently building
34
- * the seed's copy — and presenting as green — for as long as the harness had
35
- * existed. The red they opened a channel about was the harmless half.)*
36
- *
37
- * ⇒ **This check is about the silent case.** An error message can only reach the
38
- * loud one; comparing the two answers reaches both.
39
- *
40
- * ## Why it compares REALPATHS and not link text
41
- *
42
- * `readlink node_modules/<name>` returning `../../src` looks healthy and proves
43
- * nothing: **a relative symlink resolves against the link's target, not its
44
- * location**, so with `node_modules` itself a link, `../../src` lands in a
45
- * different tree. Only the resolved physical path answers the question vite
46
- * actually asks. *(This module's first draft compared link text. It reported
47
- * healthy on the one tree that was broken.)*
48
- *
49
- * ## Why it does its own node_modules walk
50
- *
51
- * `createRequire(...).resolve(name)` cannot be used: in the failure case `main`
52
- * points at the missing entry, so it throws rather than telling us where it
53
- * looked. `require.resolve(name + '/package.json')` cannot either — a foundation
54
- * declares `exports: { ".": "./_entry.generated.js" }`, which gates every other
55
- * subpath, so that lookup fails on a *healthy* package.
56
- *
57
- * So we walk `node_modules` upward for the package directory, which is the part
58
- * of node resolution that locates a package before `exports` or `main` is
59
- * consulted. That is the step whose answer we need.
60
- */
61
-
62
- import { existsSync, readFileSync, realpathSync } from 'node:fs'
63
- import { dirname, join, parse } from 'node:path'
64
-
65
- /**
66
- * The directory a bare specifier's package lives in, by node's own
67
- * node_modules walk — the step that runs before `exports`/`main` are read.
68
- *
69
- * @param {string} name - the bare specifier (a foundation's package name)
70
- * @param {string} fromDir - the directory resolution starts in (the site root)
71
- * @returns {string|null} the package directory, or null if no node_modules
72
- * anywhere up the tree holds it
73
- */
74
- export function findPackageDir(name, fromDir) {
75
- let dir = fromDir
76
- const { root } = parse(fromDir)
77
- // eslint-disable-next-line no-constant-condition
78
- while (true) {
79
- const candidate = join(dir, 'node_modules', name)
80
- if (existsSync(join(candidate, 'package.json'))) return candidate
81
- if (dir === root) return null
82
- const parent = dirname(dir)
83
- if (parent === dir) return null
84
- dir = parent
85
- }
86
- }
87
-
88
- /**
89
- * Does Vite's resolution of this package land on the very file we generated?
90
- *
91
- * ⭐ **This is the question the check exists to ask.** Comparing DIRECTORIES
92
- * asked a different one and got it wrong for the most common layout there is: a
93
- * package rooted at `foundations/<x>/`, its entry generated into
94
- * `foundations/<x>/src/`, and `main` pointing there. That is two directories and
95
- * ONE file. Measured 2026-09-10 against every site of a ten-site project: the
96
- * directory check fired for six — five healthy `src/` layouts, and the single
97
- * genuine case, a pnpm `file:` dependency copied into the store, buried among
98
- * them. The entry comparison leaves exactly that one.
99
- * A warning that fires on healthy projects is the failure this module's own
100
- * header warns against.
101
- *
102
- * ⛔ **True only when the match is PROVEN** — both files exist and realpath to
103
- * one place. Any failure to resolve returns false, so the caller reports the
104
- * directory disagreement rather than silencing it: a copy with no entry, or one
105
- * carrying a stale entry of its own, is still caught.
106
- *
107
- * Follows what Vite reads for a bare specifier: `exports['.']` (a string, or its
108
- * `import`/`default` condition), else `main`, else `index.js`.
109
- */
110
- function resolvesToSameEntry(generatedEntry, packageDir) {
111
- let pkg
112
- try {
113
- pkg = JSON.parse(readFileSync(join(packageDir, 'package.json'), 'utf8'))
114
- } catch {
115
- return false
116
- }
117
- const pick = (v) =>
118
- typeof v === 'string' ? v : v && typeof v === 'object' ? pick(v.import ?? v.default) : null
119
- const exp = pkg.exports
120
- let rel = null
121
- if (typeof exp === 'string') rel = exp
122
- else if (exp && typeof exp === 'object') rel = pick('.' in exp ? exp['.'] : exp)
123
- rel ??= typeof pkg.main === 'string' ? pkg.main : null
124
- rel ??= 'index.js'
125
- try {
126
- return realpathSync(generatedEntry) === realpathSync(join(packageDir, rel))
127
- } catch {
128
- return false
129
- }
130
- }
131
-
132
- /**
133
- * Compare where we generated the foundation entry against where vite will look.
134
- *
135
- * ⭐ **Silence is deliberate when the package is not found at all.** Not every
136
- * supported layout puts the foundation in the site's `node_modules` — a
137
- * `foundations/<name>/` multi-site project may not — and a warning that fires on
138
- * a healthy project is worse than no warning, because the next person learns to
139
- * ignore it. A package we cannot locate is also a case vite will fail on by
140
- * itself, loudly. **We report only a disagreement we can actually prove**, which
141
- * makes a false positive impossible by construction.
142
- *
143
- * @param {Object} args
144
- * @param {string} args.name - the foundation's declared name (the bare specifier)
145
- * @param {string} args.generatedInto - the directory we wrote `_entry.generated.js` to
146
- * @param {string} args.siteRoot - where vite resolves the bare specifier from
147
- * @param {string} [args.generatedEntry] - the entry file we wrote; defaults to
148
- * `_entry.generated.js` inside `generatedInto`
149
- * @returns {{ ok: true } | { ok: false, ours: string, theirs: string, message: string }}
150
- */
151
- export function checkFoundationResolution({ name, generatedInto, siteRoot, generatedEntry }) {
152
- const theirsRaw = findPackageDir(name, siteRoot)
153
- if (!theirsRaw) return { ok: true }
154
-
155
- let ours, theirs
156
- try {
157
- ours = realpathSync(generatedInto)
158
- theirs = realpathSync(theirsRaw)
159
- } catch {
160
- // A path vanished between the walk and the realpath. Not a disagreement we
161
- // can prove, so it is not one we report.
162
- return { ok: true }
163
- }
164
-
165
- if (ours === theirs) return { ok: true }
166
-
167
- // Two directories are not two foundations. When the package Vite reaches
168
- // resolves to the entry file we generated, the build reads what we wrote —
169
- // the `src/` layout, where the entry sits one level below the package root.
170
- if (resolvesToSameEntry(generatedEntry ?? join(generatedInto, '_entry.generated.js'), theirsRaw)) {
171
- return { ok: true }
172
- }
173
-
174
- return {
175
- ok: false,
176
- ours,
177
- theirs,
178
- message: [
179
- `[site] ⛔ Foundation "${name}" resolves to two different directories.`,
180
- ``,
181
- ` we generated its entry into : ${ours}`,
182
- ` vite will import it from : ${theirs}`,
183
- ``,
184
- ` Your site build will use the SECOND one, so edits to the first do not`,
185
- ` take effect — and if that directory carries a stale _entry.generated.js`,
186
- ` the build SUCCEEDS against the wrong foundation.`,
187
- ``,
188
- ` This means node_modules/${name} under ${siteRoot} is not the foundation`,
189
- ` directory. Re-run your package manager's install. If you copied or moved`,
190
- ` this project, check whether node_modules (or a parent of it) is a symlink`,
191
- ` into another tree — a relative link inside one resolves against the link's`,
192
- ` target, so it can point outside this project while looking correct.`,
193
- ].join('\n'),
194
- }
195
- }