33// names it explicitly.
44//
55// The crate (its Node-API build, `src/node.rs`: what `rxc app` does) already hands each module to Solid's compiler
6- // (spec/11 CP-1), so a `.rx` module comes back as Solid's output for the environment's `generate`, with its source map onto the `.rx` and, on a
7- // program's entry, the program's stylesheet. Solid's plugin then takes it the way it takes lowered `.tsrx`: primitive
6+ // (spec/11 CP-1), so a `.rx` module comes back as Solid's output for the environment's `generate`, with its source map onto the `.rx` and
7+ // its own stylesheet. Solid's plugin then takes it the way it takes lowered `.tsrx`: primitive
88// names, `lazy()`, the HMR refresh pass, server functions (12 SF-7a), the stylesheet as a CSS module.
99//
1010// Entries find themselves. JavaScript imports only a program's entry (its default export mounts the app); an `.rx`
1313// changes. The crate's output imports its dependencies as `./x.rx?rx-dep`, and a dependency is served from the build
1414// of a program that contains it. A module several programs share is one module: its instance is keyed by its factory
1515// (11 CP-4b `module`), not by its place in a program.
16- import { existsSync , statSync } from "node:fs" ;
16+ import { existsSync , readdirSync , readFileSync , statSync } from "node:fs" ;
1717import { createRequire } from "node:module" ;
1818import path from "node:path" ;
1919import { fileURLToPath } from "node:url" ;
@@ -35,6 +35,29 @@ const isDep = moduleId => new URLSearchParams(moduleId.split("?")[1] ?? "").has(
3535// `import __m0 from "./catalog.rx";` → `"./catalog.rx?rx-dep"` (one line each: the module's map is by line, CP-1c)
3636const tagDeps = code => code . replace ( / ^ ( i m p o r t \w + f r o m " ) ( \. { 1 , 2 } \/ [ ^ " ? ] * \. r x ) ( " ; ? ) $ / gm, `$1$2?${ DEP } $3` ) ;
3737
38+ const RX_IMPORT = / ^ i m p o r t \b [ ^ " ' ] * ?\b f r o m \s + ( [ " ' ] ) ( \. { 1 , 2 } \/ [ ^ " ' ? ] * \. r x ) \1/ gm;
39+ /** The `.rx` files under `root` from which `file` is reached by imports: first those no other `.rx` file imports. */
40+ function entriesOf ( file , root ) {
41+ const importers = new Map ( ) ;
42+ const walk = dir => {
43+ for ( const e of readdirSync ( dir , { withFileTypes : true } ) ) {
44+ if ( e . name === "node_modules" || e . name . startsWith ( "." ) ) continue ;
45+ const p = path . join ( dir , e . name ) ;
46+ if ( e . isDirectory ( ) ) walk ( p ) ;
47+ else if ( e . name . endsWith ( ".rx" ) ) for ( const [ , , spec ] of readFileSync ( p , "utf8" ) . matchAll ( RX_IMPORT ) ) {
48+ const dep = path . resolve ( path . dirname ( p ) , spec ) ;
49+ importers . set ( dep , [ ...( importers . get ( dep ) ?? [ ] ) , p ] ) ;
50+ }
51+ }
52+ } ;
53+ walk ( root ) ;
54+ const seen = new Set ( [ file ] ) ;
55+ for ( const f of seen ) for ( const i of importers . get ( f ) ?? [ ] ) seen . add ( i ) ;
56+ seen . delete ( file ) ;
57+ const top = f => ! importers . has ( f ) ;
58+ return [ ...seen ] . sort ( ) . sort ( ( a , b ) => top ( b ) - top ( a ) ) ;
59+ }
60+
3861/** @returns {import("@solidjs/vite-plugin").Frontend } */
3962export default function rx ( ) {
4063 const builds = new Map ( ) ; // entry\0target\0moduleName\0spa → { entry, target, moduleName, spa, out, stamps }
@@ -50,28 +73,38 @@ export default function rx() {
5073 builds . set ( key , b ) ;
5174 return b ;
5275 }
76+ const built = ( file , target , moduleName , spa ) =>
77+ [ ...builds . values ( ) ] . filter ( b => b . target === target && b . moduleName === moduleName && b . spa === spa && b . out . modules ?. [ file ] ) ;
5378 /** A dependency: from a build of a program that has it, rebuilt if that program changed. */
54- function containing ( file , target , moduleName , spa ) {
55- const has = [ ... builds . values ( ) ] . filter ( b => b . target === target && b . moduleName === moduleName && b . spa === spa && b . out . modules ?. [ file ] ) ;
79+ function containing ( file , target , moduleName , spa , root ) {
80+ const has = built ( file , target , moduleName , spa ) ;
5681 const b = has . find ( fresh ) ?? has [ 0 ] ;
57- if ( ! b ) throw new Error ( `${ file } is imported as an .rx dependency, but no program built so far contains it` ) ;
58- return build ( b . entry , target , moduleName , spa ) ;
82+ if ( b ) return build ( b . entry , target , moduleName , spa ) ;
83+ // the entry's compile was skipped (Vitest's `fsModuleCache` served it), so find the program from the files
84+ let failed ;
85+ for ( const entry of entriesOf ( file , root ) ) {
86+ const found = build ( entry , target , moduleName , spa ) ;
87+ if ( found . out . modules ?. [ file ] ) return found ;
88+ if ( ! failed && found . out . diagnostics ) failed = found ; // the program holds it but does not compile: report why
89+ }
90+ if ( failed ) return failed ;
91+ throw new Error ( `${ file } is imported as an .rx dependency, but no program under ${ root } contains it` ) ;
5992 }
6093 return {
61- compile ( _source , { id, moduleId, solid, addWatchFile } ) {
94+ compile ( _source , { id, moduleId, root , solid, addWatchFile } ) {
6295 const file = path . resolve ( id ) ;
6396 const target = solid . generate === "ssr" ? "ssr" : solid . generate === "universal" ? "universal" : "dom" ;
6497 const moduleName = solid . moduleName ?? "@solidjs/web" ;
6598 // 08 CN-3d: a client-only build (no SSR, nothing hydrates) has nothing to show on a first load nothing holds
6699 const spa = target === "dom" && solid . hydratable === false ;
67100 const dep = isDep ( moduleId ) ;
68- const { entry , out } = dep ? containing ( file , target , moduleName , spa ) : build ( file , target , moduleName , spa ) ;
101+ const { out } = dep ? containing ( file , target , moduleName , spa , path . resolve ( root ?? process . cwd ( ) ) ) : build ( file , target , moduleName , spa ) ;
69102 if ( out . diagnostics ) throw new Error ( report ( out . diagnostics ) ) ;
70103 if ( out . error ) throw new Error ( out . error ) ;
71104 const m = out . modules [ file ] ;
72105 for ( const p of Object . keys ( out . modules ) ) if ( p !== file ) addWatchFile ( p ) ;
73- // CSS-4: the program's stylesheet, linked once, from its entry
74- return { code : tagDeps ( m . code ) , map : m . map , css : ! dep && file === entry ? out . css : "" } ;
106+ // CSS-4: each module links its own stylesheet, so a module several programs share links it once
107+ return { code : tagDeps ( m . code ) , map : m . map , css : m . css ?? "" } ;
75108 } ,
76109 } ;
77110}
0 commit comments