@exodus/stasis 1.0.0-rc.2 → 1.0.0-rc.4

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/bin/stasis.js CHANGED
@@ -148,6 +148,10 @@ if (command === '-v' || command === '--version') {
148
148
  } catch (cause) {
149
149
  fail(`Error: ${cause.message}`)
150
150
  }
151
+ if (plan.deptree) {
152
+ const { setCacheDir } = await import('../src/vfs-bundle.js')
153
+ setCacheDir(await cacheDir())
154
+ }
151
155
  const { bundleCommand } = await import('../src/cmd/bundle.js')
152
156
  await bundleCommand({ cwd, ...plan })
153
157
  } else if (command === 'github-bundle') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis",
3
- "version": "1.0.0-rc.2",
3
+ "version": "1.0.0-rc.4",
4
4
  "description": "File-level source bundles and lockfiles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -118,16 +118,18 @@
118
118
  },
119
119
  "homepage": "https://github.com/PreventiveMeasures/stasis#readme",
120
120
  "dependencies": {
121
+ "@babel/generator": "^7.29.7",
121
122
  "@exodus/bytes": "^1.16.0",
122
- "@exodus/stasis-core": "1.0.0-rc.2",
123
- "@exodus/stasis-plugins": "1.0.0-rc.2",
123
+ "@exodus/stasis-core": "1.0.0-rc.4",
124
+ "@exodus/stasis-plugins": "1.0.0-rc.4",
124
125
  "@preventive/archive": "^1.4.1",
125
- "@preventive/deptree": "^1.5.0",
126
+ "@preventive/deptree": "^1.5.3",
126
127
  "@preventive/lockfile": "^1.3.0",
127
128
  "@preventive/rn": "1.0.0-alpha.8",
128
- "@preventive/upstream": "^1.7.0",
129
+ "@preventive/upstream": "^1.8.0",
129
130
  "@preventive/vfs": "^1.1.0",
130
131
  "flow-remove-types": "^2.322.0",
132
+ "hermes-parser": "^0.36.1",
131
133
  "oxc-parser": "^0.152.0"
132
134
  },
133
135
  "peerDependencies": {
package/src/cmd/build.js CHANGED
@@ -14,9 +14,10 @@ import { assetData, assetModule } from '@preventive/rn/metro-assets.js'
14
14
  import { HERMES_UNSUPPORTED, createBabelConfigTransform, createHermesTransform } from '../babel.js'
15
15
  import { bundleFromLockfile } from '../bundle-from-lockfile.js'
16
16
  import { METRO_RELEASE_DEFINE } from '../define.js'
17
- import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from '../flow.js'
17
+ import { FLOW_EXTS, hasFlowPragma, stripFlow } from '../flow.js'
18
18
  import { ASSET_REGISTRY, assetRegistryResolver } from '../metro-assets.js'
19
19
  import { parseFileWithKind } from '../parse.js'
20
+ import { getParser, syntaxErrors } from '../scan.js'
20
21
  import { writeFile } from './output.js'
21
22
 
22
23
  export { bundleFromLockfile }
@@ -169,11 +170,11 @@ function createFlowTransform(esbuild) {
169
170
  return undefined
170
171
  } catch { /* stripped below */ }
171
172
  }
172
- try {
173
- return { contents: stripFlowTypes(text, { removeEmptyImports: true }), loader }
174
- } catch {
175
- return undefined
176
- }
173
+ // oxc, as the scan checks it, so the build lowers what the bundle's scan lowered.
174
+ const lang = loader === 'jsx' ? 'jsx' : 'js'
175
+ const parses = (code) => syntaxErrors(getParser().parseSync(path, code, { sourceType: 'unambiguous', lang })).length === 0
176
+ const { stripped } = stripFlow(text, path, parses, { removeEmptyImports: true })
177
+ return stripped === undefined ? undefined : { contents: stripped, loader }
177
178
  }
178
179
  }
179
180
 
package/src/cmd/bundle.js CHANGED
@@ -2180,10 +2180,10 @@ async function buildOfCommand({ cwd = process.cwd(), env = process.env, home, en
2180
2180
  const bundleExtra = builds !== undefined
2181
2181
  let union, root
2182
2182
  if (deptree) {
2183
- // The disk read through the tree, nothing cached (`cache: false`, and no setCacheDir here).
2183
+ // The disk read through the tree, what is fetched cached where setCacheDir says (the CLI's user cache directory).
2184
2184
  const { createClient } = await import('@preventive/upstream/github.js')
2185
2185
  const built = await withNpmToken(home, () => buildFromLockfile('bundleCommand', diskHost, {
2186
- cwd: resolve(cwd), lockfile, cache: false, lazy: false, client: createClient({ token: env.GITHUB_TOKEN || null }),
2186
+ cwd: resolve(cwd), lockfile, lazy: false, client: createClient({ token: env.GITHUB_TOKEN || null }),
2187
2187
  }, { env, plan: { builds: plans, bundleExtra }, outputsAt, refuseInstalled: true, why: '--deptree lays out the dependencies from a lockfile' }))
2188
2188
  console.warn(unpackedSummary(built.stats))
2189
2189
  root = built.root
package/src/flow.js CHANGED
@@ -1,6 +1,9 @@
1
1
  // --flow (which --metro turns on): Flow type syntax stripped as Metro's Babel (@react-native/babel-preset) strips it,
2
2
  // by flow-remove-types -- stasis's own dependency, never the project's, loaded lazily.
3
3
  import { createRequire } from 'node:module'
4
+ import { splitNodeModulesPath, toPosix } from '@exodus/stasis-core/util'
5
+
6
+ const require = createRequire(import.meta.url)
4
7
 
5
8
  // The JS family alone: flow-remove-types would corrupt TS-only constructs. It preserves JSX.
6
9
  export const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
@@ -14,14 +17,54 @@ const NOFLOW = '\n/* @noflow */'
14
17
  const NOFLOW_STRIPPED = `\n/* ${' '.repeat('@noflow'.length)} */`
15
18
 
16
19
  let flowRemoveTypes
20
+ let hermesParser
21
+ let generate
17
22
 
18
23
  // `source` with its Flow types blanked in place (offsets kept): one marked `@flow` as Flow throughout (`f<T>(x)` a
19
24
  // call), any other as a `@noflow` file, marked so past its end. Flow code (`enum`s) stays. Throws where it can't parse.
20
- export function stripFlowTypes(source, { removeEmptyImports = false } = {}) {
21
- flowRemoveTypes ??= createRequire(import.meta.url)('flow-remove-types')
25
+ function stripFlowTypes(source, { removeEmptyImports = false } = {}) {
26
+ flowRemoveTypes ??= require('flow-remove-types')
22
27
  if (hasFlowPragma(source)) return flowRemoveTypes(source, { all: true, removeEmptyImports }).toString()
23
28
  if (source.includes('@noflow')) return flowRemoveTypes(source, { removeEmptyImports }).toString()
24
29
  const stripped = flowRemoveTypes(source + NOFLOW, { removeEmptyImports }).toString()
25
30
  if (!stripped.endsWith(NOFLOW_STRIPPED)) throw new Error('flow-remove-types left the @noflow comment stasis appended')
26
31
  return stripped.slice(0, -NOFLOW_STRIPPED.length)
27
32
  }
33
+
34
+ // A file of the package installed as `react-native`, not of one nested in it.
35
+ export const inReactNative = (path) => splitNodeModulesPath(toPosix(path))?.name === 'react-native'
36
+
37
+ // `source` with its Flow code (`component`, `hook`, `match`, `enum`) lowered as Metro's Babel lowers it, types kept;
38
+ // an `enum` requires `flow-enums-runtime`. Throws where it can't parse.
39
+ function lowerFlowSyntax(source) {
40
+ hermesParser ??= require('hermes-parser')
41
+ generate ??= require('@babel/generator').default
42
+ const flow = hasFlowPragma(source)
43
+ const ast = hermesParser.parse(source, {
44
+ babel: true,
45
+ flow: flow ? 'all' : 'detect',
46
+ sourceType: 'unambiguous',
47
+ transformOptions: { TransformEnumSyntax: { enable: true } },
48
+ })
49
+ // `comments: false` drops the `@flow` mark the strip reads.
50
+ return (flow ? '/* @flow */' : '') + generate(ast, { comments: false, retainLines: true }).code
51
+ }
52
+
53
+ // `{ parsed, source, stripped }`: `text` with its types stripped, as `parse` reads it (falsy where it can't). In a file
54
+ // of react-native that doesn't parse so, `source` is `text` with its Flow code lowered first. Neither parsing: the
55
+ // plain strip, `parsed` null, with any `error` thrown.
56
+ export function stripFlow(text, path, parse, { removeEmptyImports = false } = {}) {
57
+ const read = (lower) => {
58
+ try {
59
+ const source = lower ? lowerFlowSyntax(text) : text
60
+ const stripped = stripFlowTypes(source, { removeEmptyImports })
61
+ return { parsed: parse(stripped) || null, source, stripped }
62
+ } catch (error) {
63
+ return { parsed: null, error }
64
+ }
65
+ }
66
+ const plain = read(false)
67
+ if (plain.parsed !== null || !inReactNative(path)) return plain
68
+ const lowered = read(true)
69
+ return lowered.parsed !== null ? lowered : { ...plain, error: lowered.error ?? plain.error }
70
+ }
package/src/installed.js CHANGED
@@ -135,7 +135,10 @@ function patchLocator(file, patchers, host) {
135
135
  })
136
136
  }
137
137
 
138
- function pnpmLocks(file, host, strict) {
138
+ // `aliases`, the top-level names pnpm's copy of the lockfile lists where it is read, are those held where they are: where
139
+ // pnpm hoists, the place of one it left out (a devDependency `pnpm install --prod` skips) holds whatever hoists into it,
140
+ // held by its name and version. `named` carries the names this one lists.
141
+ function pnpmLocks(file, host, strict, aliases) {
139
142
  const parsed = parseLock(file, host, strict, (text) => ({ lockfile: parsePnpmLockfile(text).lockfile }))
140
143
  if (parsed === undefined) return undefined
141
144
  const { lockfile } = parsed
@@ -156,13 +159,14 @@ function pnpmLocks(file, host, strict) {
156
159
  for (const importer of Object.values(lockfile?.importers ?? {})) {
157
160
  for (const [as, key] of [importer.dependencies, importer.devDependencies, importer.optionalDependencies].flatMap((deps) => Object.entries(deps ?? {}))) {
158
161
  const record = keyed.get(key)
159
- if (record) top.set(as, top.has(as) && top.get(as) !== record ? undefined : record)
162
+ if (record && (aliases?.has(as) ?? true)) top.set(as, top.has(as) && top.get(as) !== record ? undefined : record)
160
163
  }
161
164
  }
162
165
  const named = byId(ids)
166
+ const atTop = (location) => (location === `node_modules/${nameAt(location)}` ? top.get(nameAt(location)) : undefined)
163
167
  return {
164
168
  stored: (name) => stored.get(name) ?? stored.get(STORE_HASH.exec(name)?.[0]),
165
- named: (location, ...of) => (location === `node_modules/${nameAt(location)}` ? top.get(nameAt(location)) : undefined) ?? named(location, ...of),
169
+ named: Object.assign((location, ...of) => atTop(location) ?? named(location, ...of), { aliases: new Set(top.keys()) }),
166
170
  }
167
171
  }
168
172
 
@@ -201,7 +205,7 @@ const MANAGERS = [
201
205
  marker: '.modules.yaml',
202
206
  lockfiles: ['pnpm-lock.yaml'],
203
207
  installed: (dir, host) => pnpmLocks(join(dir, 'node_modules', '.pnpm', 'lock.yaml'), host)?.named,
204
- locks: (file, host) => pnpmLocks(file, host, true).named,
208
+ locks: (file, host, installed) => pnpmLocks(file, host, true, installed.aliases).named,
205
209
  none: (version) => `${version}; it locks none`,
206
210
  },
207
211
  ]
@@ -216,7 +220,7 @@ function layoutOf(dir, host) {
216
220
  if (locking.length > 1 && held.length === 0) throw new Error(`${dir}: ${locking.map(([, file]) => basename(file)).join(' and ')} are both there, and which installed its node_modules isn't known`)
217
221
  const [manager = marked.length === 1 ? marked[0] : undefined, lockfile] = held[0] ?? []
218
222
  const installed = (marked.length === 1 && marked[0] === manager && manager.installed(dir, host)) || NONE
219
- return { installed, lockfile, locks: lockfile && manager.locks(lockfile, host), none: manager?.none }
223
+ return { installed, lockfile, locks: lockfile && manager.locks(lockfile, host, installed), none: manager?.none }
220
224
  }
221
225
 
222
226
  // pnpm's, where it isolates: `store`'s lock.yaml held to the pnpm-lock.yaml of the directory the store is in.
package/src/scan.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { basename, dirname, extname, isAbsolute, join, resolve as resolvePath, relative } from 'node:path'
2
2
  import { fileURLToPath, pathToFileURL } from 'node:url'
3
3
  import { createRequire, isBuiltin } from 'node:module'
4
+ import { totalmem } from 'node:os'
4
5
  import assert from 'node:assert/strict'
5
6
  import { packageType } from '@exodus/stasis-core/bundle-util'
6
7
  import { classifyExtension, classifyFormat, JS_EXTENSIONS, nestedMap, relativeEscapes } from '@exodus/stasis-core/util'
@@ -9,7 +10,7 @@ import { RELATIVE_REQUEST } from './resolve-node.js'
9
10
  import { branchFolder, parseDefine } from './define.js'
10
11
  import { diskHost } from '@exodus/stasis-core/host'
11
12
  import { hostMemo } from './vfs-bundle/lazy.js'
12
- import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from './flow.js'
13
+ import { FLOW_EXTS, hasFlowPragma, stripFlow } from './flow.js'
13
14
 
14
15
  // Static require/import graph walker: parses source, never loads or executes user code.
15
16
 
@@ -21,13 +22,23 @@ const JSX_EXTS = new Set(['.js', '.cjs', '.mjs'])
21
22
 
22
23
  const TS_EXTS = new Set(['.ts', '.cts', '.mts', '.tsx'])
23
24
 
24
- // Required lazily so non-JS bundlers don't load the native oxc parser. Raw transfer, where the platform takes it,
25
- // hands each AST over from the parser's own buffer rather than as JSON, which takes longer to parse than the source.
25
+ // Raw transfer reserves 6 GiB for the parser's buffer, of which a parse writes only what it takes. Linux refuses one
26
+ // mapping past its RAM and swap, so on a smaller machine (a 4 GiB function) that buffer can't be had: a RangeError on
27
+ // the main thread, and in a worker under Node 26 the worker terminated as out of heap, past any catch. The machine's
28
+ // memory, or its process's where that is less, is to hold that buffer and the rest of the process. Never on Vercel
29
+ // (VERCEL_ENV set, in its builds and functions alike), whose functions are that small machine.
30
+ const RAW_TRANSFER_MEMORY = 8 * 2 ** 30
31
+ export const rawTransferFits = (memory = Math.min(totalmem(), process.constrainedMemory() || Infinity), env = process.env) =>
32
+ env.VERCEL_ENV === undefined && memory >= RAW_TRANSFER_MEMORY
33
+
34
+ // Required lazily so non-JS bundlers don't load the native oxc parser. Raw transfer, where the platform takes it and
35
+ // its buffer fits, hands each AST over from the parser's own buffer rather than as JSON, which takes longer to parse
36
+ // than the source.
26
37
  let _parser
27
38
  export function getParser() {
28
39
  if (_parser === undefined) {
29
40
  const oxc = createRequire(import.meta.url)('oxc-parser')
30
- _parser = oxc.rawTransferSupported() ? { ...oxc, parseSync: (file, source, options) => oxc.parseSync(file, source, { experimentalRawTransfer: true, ...options }) } : oxc
41
+ _parser = oxc.rawTransferSupported() && rawTransferFits() ? { ...oxc, parseSync: (file, source, options) => oxc.parseSync(file, source, { experimentalRawTransfer: true, ...options }) } : oxc
31
42
  }
32
43
  return _parser
33
44
  }
@@ -303,16 +314,6 @@ export class Scan {
303
314
  }
304
315
  }
305
316
 
306
- // null where stripping fails: the caller keeps oxc's parse, saying so where that failed (`failed`).
307
- #stripFlow(src, file, failed) {
308
- try {
309
- return stripFlowTypes(src)
310
- } catch (cause) {
311
- if (failed) console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${cause.message})`)
312
- return null
313
- }
314
- }
315
-
316
317
  // As Node loads it: under the format its name and package declare, or for a typeless .js/.ts the one Node detects.
317
318
  parseFile(file) {
318
319
  const ext = extname(file)
@@ -359,18 +360,19 @@ export class Scan {
359
360
 
360
361
  // --flow, as Metro's Babel reads the file: one marked `@flow` even where oxc parses it (`f<T>(x)` a comparison
361
362
  // there), any other only where oxc's parse failed. A re-parse that still fails keeps the original's errors.
363
+ let source = src
362
364
  let strippedSource = null
363
365
  if (this.flow && FLOW_EXTS.has(ext) && (errors.length > 0 || hasFlowPragma(src))) {
364
- const flowSrc = this.#stripFlow(src, file, errors.length > 0)
365
- const asFlow = flowSrc === null ? null : cleanParse(flowSrc, baseSourceType)
366
- if (asFlow !== null) {
367
- parsed = asFlow
366
+ const flow = stripFlow(src, file, (text) => cleanParse(text, baseSourceType))
367
+ if (flow.parsed !== null) {
368
+ ({ parsed, source, stripped: strippedSource } = flow)
368
369
  format = declared ?? detectedFormat(parsed)
369
370
  errors = []
370
- strippedSource = flowSrc
371
+ } else if (flow.error && errors.length > 0) {
372
+ console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${flow.error.message})`)
371
373
  }
372
374
  }
373
- return { format, parsed, errors, source: src, strippedSource }
375
+ return { format, parsed, errors, source, strippedSource }
374
376
  }
375
377
 
376
378
  #scanFile(url, queue) {