@exodus/stasis 1.0.0-beta.14 → 1.0.0-beta.15

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/src/cmd/build.js CHANGED
@@ -8,11 +8,14 @@ import { Bundle } from '@exodus/stasis-core/bundle'
8
8
  import { State } from '@exodus/stasis-core/state'
9
9
  import { toPosix } from '@exodus/stasis-core/util'
10
10
  import { StasisEsbuild } from '@exodus/stasis-plugins/esbuild'
11
- import { bundleDirHost, metroAssetFiles } from '@exodus/stasis-plugins/metro-native'
11
+ import { bundleDirHost } from '@exodus/stasis-plugins/metro-native'
12
+ import { assetsDestFiles } from '@preventive/rn/assets-dest.js'
13
+ import { assetData, assetModule } from '@preventive/rn/metro-assets.js'
12
14
  import { HERMES_UNSUPPORTED, createBabelConfigTransform, createHermesTransform } from '../babel.js'
13
15
  import { bundleFromLockfile } from '../bundle-from-lockfile.js'
14
- import { FLOW_EXTS, stripFlowTypes } from '../flow.js'
15
- import { ASSET_REGISTRY, assetRegistryResolver, metroAssetCopies, metroAssetDescriptor, metroAssetModule } from '../metro-assets.js'
16
+ import { METRO_RELEASE_DEFINE } from '../define.js'
17
+ import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from '../flow.js'
18
+ import { ASSET_REGISTRY, assetRegistryResolver } from '../metro-assets.js'
16
19
  import { parseFileWithKind } from '../parse.js'
17
20
  import { writeFile } from './output.js'
18
21
 
@@ -185,47 +188,55 @@ function loadStateForBundle(root, bundle) {
185
188
  // A file's bytes as the State serves them: a Buffer for a base64 resource, else a string.
186
189
  const asBytes = (source) => (Buffer.isBuffer(source) ? source : Buffer.from(source))
187
190
 
188
- // The bytes the artifact carries for `file` (a path from the State's root), verified by getFile.
191
+ // The bytes the artifact carries for `file` (a path from the State's root, or an absolute one), verified
192
+ // by getFile.
189
193
  const readCarried = (state, file) => asBytes(state.getFile(pathToFileURL(resolve(state.root, file)).toString()).source)
190
194
 
191
195
  // --metro=PLATFORM's resource hook (StasisEsbuild's `resource`): each resource the build reaches -- in a
192
196
  // --metro bundle, a Metro asset, as it records an import edge to no other -- served as the module
193
- // Metro builds of it for `platform` (metro-assets.js), from the files of the asset the artifact carries
194
- // in its directory. The module's require of the asset registry resolves to the file the artifact
195
- // carries for it, an edge added to the State's graph: the bundle records none, as Metro adds it past
196
- // the source. `built` collects each asset's { descriptor, files } for --assets-dest.
197
+ // Metro builds of it for `platform` (@preventive/rn's assetData and assetModule), from the files of the
198
+ // asset the artifact carries in its directory, at its path from the project root. The module's require
199
+ // of the asset registry resolves to the file the artifact carries for it (metro-assets.js), an edge
200
+ // added to the State's graph: the bundle records none, as Metro adds it past the source. `built`
201
+ // collects each asset's data for --assets-dest.
197
202
  function metroAssetResource(state, platform, built) {
198
- const host = bundleDirHost(state)
203
+ const listing = bundleDirHost(state)
199
204
  const registryFor = assetRegistryResolver(platform, state.sources)
200
205
  return (source, { path }) => {
201
206
  const rel = toPosix(state.relative(path))
202
- const files = metroAssetFiles(path, platform, host).map((file) => toPosix(state.relative(file)))
203
- // The asset's own bytes are those the plugin serves it by.
204
- const asset = metroAssetDescriptor(rel, { platform, files, read: (file) => (file === rel ? asBytes(source) : readCarried(state, file)) })
207
+ // The files the artifact carries, the asset's own bytes those the plugin serves it by.
208
+ const host = {
209
+ readdir: (dir) => listing.readdir(dir).map(({ name }) => name),
210
+ readFile: (file) => (file === path ? asBytes(source) : readCarried(state, file)),
211
+ }
212
+ const data = assetData(path, { urlPath: rel, platform, host })
205
213
  state.addImport(pathToFileURL(path).href, ASSET_REGISTRY, pathToFileURL(resolve(state.root, registryFor(rel))).href)
206
- built.set(rel, asset)
207
- return { contents: metroAssetModule(asset.descriptor), loader: 'js' }
214
+ built.set(rel, data)
215
+ return { contents: assetModule(data, ASSET_REGISTRY), loader: 'js' }
208
216
  }
209
217
  }
210
218
 
211
219
  // --flow's per-file transform (StasisEsbuild's `transform` hook shape), as `stasis bundle --flow` reads
212
- // a file: a JS-family one esbuild can't parse gets its Flow types stripped (flow.js), an import listing
213
- // types alone dropped whole, as Babel's Flow transform drops it (a --metro bundle records no edge for
214
- // it). One that parses is never touched, as stripping plain JS as Flow can misread it (`a < b > (c)`
215
- // as a call). A file still unparseable fails the build in esbuild: Flow syntax that is code, which
216
- // stripping leaves, or one flow-remove-types itself can't parse, served as it is.
220
+ // a file, and Metro's Babel builds it: a JS-family one gets its Flow types stripped (flow.js), an
221
+ // import listing types alone dropped whole, as Babel's Flow transform drops it (a --metro bundle
222
+ // records no edge for it) -- one marked `@flow` (hasFlowPragma) whether or not esbuild parses it as it
223
+ // is (where `f<T>(x)` is a comparison, not Flow's call), any other only where esbuild can't parse it.
224
+ // A file still unparseable fails the build in esbuild: Flow syntax that is code, which stripping
225
+ // leaves, or one flow-remove-types itself can't parse, served as it is.
217
226
  function createFlowTransform(esbuild) {
218
227
  return async (source, { path, loader }) => {
219
228
  if (!FLOW_EXTS.has(extname(path))) return undefined
220
- try {
221
- await esbuild.transform(source, { loader, sourcefile: path, logLevel: 'silent' })
222
- return undefined
223
- } catch {
229
+ const text = String(source)
230
+ if (!hasFlowPragma(text)) {
224
231
  try {
225
- return { contents: stripFlowTypes(source, { removeEmptyImports: true }), loader }
226
- } catch {
232
+ await esbuild.transform(text, { loader, sourcefile: path, logLevel: 'silent' })
227
233
  return undefined
228
- }
234
+ } catch { /* stripped below */ }
235
+ }
236
+ try {
237
+ return { contents: stripFlowTypes(text, { removeEmptyImports: true }), loader }
238
+ } catch {
239
+ return undefined
229
240
  }
230
241
  }
231
242
  }
@@ -374,6 +385,18 @@ export async function buildCommand({
374
385
  }
375
386
  if (taken.length > 0) console.warn(`[stasis] ${selectedEntry}: ${taken.join(' ')}, as ${CONFIG_FILE}'s entries ${JSON.stringify(configured.name)} sets`)
376
387
  }
388
+ // --metro, given or the config's, builds as `stasis bundle --metro` reads the files: JSX in .js
389
+ // (--loader=.js:jsx, where no --loader names .js), Flow stripped (--flow, not under --babel, whose
390
+ // config compiles it), and each of METRO_RELEASE_DEFINE's names defined as Metro's release build
391
+ // defines it, where neither --define nor the config gives it a value, as esbuild takes one (JS):
392
+ // outside Metro's prelude, `__DEV__` is no global at all.
393
+ if (metro !== undefined) {
394
+ if (loader['.js'] === undefined) loader = { ...loader, '.js': 'jsx' }
395
+ if (!babel) flow = true
396
+ for (const [key, value] of Object.entries(METRO_RELEASE_DEFINE)) {
397
+ if (!Object.hasOwn(define, key)) define = { ...define, [key]: JSON.stringify(value) }
398
+ }
399
+ }
377
400
  if (assetsDest !== undefined && metro === undefined) {
378
401
  throw new Error('stasis build: --assets-dest copies the assets --metro builds; pass --metro=PLATFORM with it')
379
402
  }
@@ -441,11 +464,12 @@ export async function buildCommand({
441
464
  }
442
465
 
443
466
  // --assets-dest's copies of the assets built, as `react-native bundle --assets-dest` lays them out for
444
- // the platform, from the bytes the artifact carries, in a stable order (esbuild loads in parallel).
467
+ // the platform (@preventive/rn's assetsDestFiles), from the bytes the artifact carries, in a stable
468
+ // order (esbuild loads in parallel).
445
469
  const assetFiles = assetsDest === undefined
446
470
  ? []
447
- : metroAssetCopies(metro, [...builtAssets.keys()].toSorted().map((rel) => builtAssets.get(rel)))
448
- .map(({ dest, file, contents }) => ({ path: resolve(cwd, assetsDest, dest), contents: contents ?? readCarried(state, file) }))
471
+ : assetsDestFiles([...builtAssets.keys()].toSorted().map((rel) => builtAssets.get(rel)), metro)
472
+ .map(({ dest, src, contents }) => ({ path: resolve(cwd, assetsDest, dest), contents: contents ?? readCarried(state, src) }))
449
473
  const writes = [...result.outputFiles, ...assetFiles]
450
474
 
451
475
  // Refuse to clobber an attested source, checked against the actual output paths before any write.
@@ -134,9 +134,15 @@ function buildOptions(values, { allSol, allJs, allRust }) {
134
134
  if (values.conditions !== undefined && conditions.length === 0) {
135
135
  throw new Error('--conditions must list at least one condition name (e.g. --conditions=react-native,browser)')
136
136
  }
137
- // --flow: strip Flow type syntax (via stasis's own flow-remove-types) before parsing; JS-only.
137
+ // --metro: resolve like Metro/RN (its own conditions + mainFields, platform suffixes per
138
+ // --platforms target). --platforms is repeatable and/or comma-separated; the values union.
139
+ // It turns on --jsx and --flow, as Metro's Babel parses JSX and Flow in every file (and defines what
140
+ // Metro's release build does, see buildJs).
141
+ const metro = Boolean(values.metro)
142
+ // --flow: strip Flow type syntax (via stasis's own flow-remove-types) from a file marked `@flow`
143
+ // before parsing; JS-only.
138
144
  if (values.flow && !allJs) throw new Error('--flow is only valid for JS bundles')
139
- const flow = Boolean(values.flow)
145
+ const flow = Boolean(values.flow) || metro
140
146
  // --typescript: resolve like tsc -- a specifier naming a missing JS output (./x.js, .mjs, .cjs,
141
147
  // extensionless) lands on its on-disk TS source (./x.ts, .mts, .cts); an existing .js always wins.
142
148
  if (values.typescript && !allJs) throw new Error('--typescript is only valid for JS bundles')
@@ -152,9 +158,6 @@ function buildOptions(values, { allSol, allJs, allRust }) {
152
158
  if (values.mainFields !== undefined && mainFields.length === 0) {
153
159
  throw new Error('--mainFields must list at least one field (e.g. --mainFields=react-native,browser,main)')
154
160
  }
155
- // --metro: resolve like Metro/RN (its own conditions + mainFields, platform suffixes per
156
- // --platforms target). --platforms is repeatable and/or comma-separated; the values union.
157
- const metro = Boolean(values.metro)
158
161
  // --metro-resolver: resolve through the project's own metro-resolver instead of the built-in
159
162
  // approximation. Only meaningful alongside --metro (enforced below and in classifyEntries).
160
163
  const metroResolver = Boolean(values['metro-resolver'])
@@ -166,8 +169,8 @@ function buildOptions(values, { allSol, allJs, allRust }) {
166
169
  if ((metro || platforms.length > 0) && !allJs) throw new Error('--metro is only valid for JS bundles')
167
170
  // --jsx: parse React Native's JSX-in-.js source in the static scanner (off by default; oxc,
168
171
  // like tsc, auto-enables JSX for .jsx/.tsx only). Orthogonal to the resolver, so it pairs with
169
- // plain/--mainFields/--metro alike -- JS-only, since no other entry language is scanned.
170
- const jsx = Boolean(values.jsx)
172
+ // plain/--mainFields/--metro alike -- JS-only, since no other entry language is scanned. On under --metro.
173
+ const jsx = Boolean(values.jsx) || metro
171
174
  if (jsx && !allJs) throw new Error('--jsx is only valid for JS bundles')
172
175
  // --package-json: fold each bundled module's package.json into the bundle. JS-only (only JS
173
176
  // modules carry an npm package.json; the sol/php/bash/rust bucketizers have no such manifest).