@yaks/esbuild 0.0.0-stage → 0.2.36

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.
Files changed (123) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +450 -2
  3. package/_dist/bundle.d.ts +6 -0
  4. package/_dist/bundle.d.ts.map +1 -0
  5. package/_dist/compile.d.ts +72 -0
  6. package/_dist/compile.d.ts.map +1 -0
  7. package/_dist/dependencies.d.ts +15 -0
  8. package/_dist/dependencies.d.ts.map +1 -0
  9. package/_dist/file.d.ts +185 -0
  10. package/_dist/file.d.ts.map +1 -0
  11. package/_dist/graph.d.ts +104 -0
  12. package/_dist/graph.d.ts.map +1 -0
  13. package/_dist/home.d.ts +15 -0
  14. package/_dist/home.d.ts.map +1 -0
  15. package/_dist/link.d.ts +192 -0
  16. package/_dist/link.d.ts.map +1 -0
  17. package/_dist/lock.d.ts +30 -0
  18. package/_dist/lock.d.ts.map +1 -0
  19. package/_dist/mod.d.ts +6 -0
  20. package/_dist/mod.d.ts.map +1 -0
  21. package/_dist/pack.d.ts +42 -0
  22. package/_dist/pack.d.ts.map +1 -0
  23. package/_dist/package.d.ts +11 -0
  24. package/_dist/package.d.ts.map +1 -0
  25. package/_dist/plan.d.ts +135 -0
  26. package/_dist/plan.d.ts.map +1 -0
  27. package/_dist/platform.d.ts +27 -0
  28. package/_dist/platform.d.ts.map +1 -0
  29. package/_dist/resolve.d.ts +42 -0
  30. package/_dist/resolve.d.ts.map +1 -0
  31. package/_dist/shelf.d.ts +11 -0
  32. package/_dist/shelf.d.ts.map +1 -0
  33. package/_dist/strip.d.ts +51 -0
  34. package/_dist/strip.d.ts.map +1 -0
  35. package/_dist/timing.d.ts +6 -0
  36. package/_dist/timing.d.ts.map +1 -0
  37. package/_dist/unpack.d.ts +14 -0
  38. package/_dist/unpack.d.ts.map +1 -0
  39. package/analysis.js +72 -0
  40. package/analysis.js.map +1 -0
  41. package/analysis.ts +82 -0
  42. package/bundle.js +320 -0
  43. package/bundle.js.map +1 -0
  44. package/bundle.ts +415 -0
  45. package/common.js +129 -0
  46. package/common.js.map +1 -0
  47. package/common.ts +138 -0
  48. package/compile.js +500 -0
  49. package/compile.js.map +1 -0
  50. package/compile.ts +622 -0
  51. package/container.js +142 -0
  52. package/container.js.map +1 -0
  53. package/container.ts +183 -0
  54. package/deno.json +1 -0
  55. package/dependencies.js +31 -0
  56. package/dependencies.js.map +1 -0
  57. package/dependencies.ts +40 -0
  58. package/file.js +303 -0
  59. package/file.js.map +1 -0
  60. package/file.ts +443 -0
  61. package/forward.ts +49 -0
  62. package/graph.js +297 -0
  63. package/graph.js.map +1 -0
  64. package/graph.ts +360 -0
  65. package/home.js +40 -0
  66. package/home.js.map +1 -0
  67. package/home.ts +52 -0
  68. package/link.js +455 -0
  69. package/link.js.map +1 -0
  70. package/link.ts +596 -0
  71. package/lock.js +129 -0
  72. package/lock.js.map +1 -0
  73. package/lock.ts +161 -0
  74. package/mod.js +14 -0
  75. package/mod.js.map +1 -0
  76. package/mod.ts +43 -0
  77. package/npm.js +362 -0
  78. package/npm.js.map +1 -0
  79. package/npm.ts +429 -0
  80. package/pack.js +188 -0
  81. package/pack.js.map +1 -0
  82. package/pack.ts +225 -0
  83. package/package.js +199 -0
  84. package/package.js.map +1 -0
  85. package/package.json +60 -4
  86. package/package.ts +255 -0
  87. package/plan.js +273 -0
  88. package/plan.js.map +1 -0
  89. package/plan.ts +395 -0
  90. package/platform.js +160 -0
  91. package/platform.js.map +1 -0
  92. package/platform.ts +238 -0
  93. package/resolve.js +86 -0
  94. package/resolve.js.map +1 -0
  95. package/resolve.ts +144 -0
  96. package/said.js +44 -0
  97. package/said.js.map +1 -0
  98. package/said.ts +61 -0
  99. package/serve.ts +37 -0
  100. package/sha256.js +7 -0
  101. package/sha256.js.map +1 -0
  102. package/sha256.ts +10 -0
  103. package/shared.js +646 -0
  104. package/shared.js.map +1 -0
  105. package/shared.ts +813 -0
  106. package/shelf.js +73 -0
  107. package/shelf.js.map +1 -0
  108. package/shelf.ts +110 -0
  109. package/strip.js +114 -0
  110. package/strip.js.map +1 -0
  111. package/strip.ts +117 -0
  112. package/tar.js +87 -0
  113. package/tar.js.map +1 -0
  114. package/tar.ts +99 -0
  115. package/timing.js +37 -0
  116. package/timing.js.map +1 -0
  117. package/timing.ts +71 -0
  118. package/unpack.js +40 -0
  119. package/unpack.js.map +1 -0
  120. package/unpack.ts +60 -0
  121. package/used.js +91 -0
  122. package/used.js.map +1 -0
  123. package/used.ts +116 -0
package/package.ts ADDED
@@ -0,0 +1,255 @@
1
+ // A package compiled once for the whole platform (D-499): an npm package at
2
+ // an exact version, or a @yaks package at a published one, built into one
3
+ // module per export entry, with the files entries share split into chunks so
4
+ // each module the package holds has one instance whichever entries a page
5
+ // imports. Every import of another package, and of this one by its own name,
6
+ // stays bare as written: the import map of the page that loads it says where
7
+ // that is. So a package's bytes never depend on the app that uses it, or on
8
+ // the versions of what it imports, and one compile serves every app.
9
+ //
10
+ // Each emitted module is named for the package's key and its place in the
11
+ // build, `compiledName`'s kind of name: a key names one package version, built
12
+ // by one compiler, so a name is written once and never changes, and the
13
+ // chunks an entry imports are named before any of them is written.
14
+ import { parse } from 'es-module-lexer/js'
15
+ import { initSync, parse as cjs } from 'cjs-module-lexer'
16
+ import { posix } from 'node:path'
17
+ import {
18
+ bundle,
19
+ type Options,
20
+ packaged,
21
+ type Source,
22
+ typedName,
23
+ } from './bundle.js'
24
+ import { COMPILER, Unexported } from './file.js'
25
+ import { rewritten } from './link.js'
26
+ import { packageOf } from './graph.js'
27
+
28
+ /** A compiled package: each export entry's specifier and the module it is,
29
+ * every module by its file name (`<name>.js`, kept and served as is), the
30
+ * bare specifiers its modules import, which a page's map resolves, and what
31
+ * each module imports statically (`links`): the package's other modules by
32
+ * file name (`./<name>.js`), and bare specifiers, which a page preloads. */
33
+ export type Compiled = {
34
+ entries: Record<string, string>
35
+ modules: Record<string, string>
36
+ imports: string[]
37
+ links: Record<string, string[]>
38
+ }
39
+
40
+ let hex = (bytes: ArrayBuffer) =>
41
+ Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, '0'))
42
+ .join('')
43
+ let sha256 = async (text: string) =>
44
+ hex(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)))
45
+
46
+ /** Each concrete browser export of an installed package, by specifier, as
47
+ * the file it resolves to. Every one is an entry of the same splitting build,
48
+ * which keeps one instance of each module whichever subpaths a page imports. */
49
+ export let exportsOf = (
50
+ files: Source,
51
+ name: string,
52
+ ): Record<string, string> => {
53
+ let pkg = JSON.parse(files.read(`node_modules/${name}/package.json`)!)
54
+ let keys = typeof pkg.exports == 'object' && pkg.exports != null &&
55
+ Object.keys(pkg.exports).some((key) => key.startsWith('.'))
56
+ ? Object.keys(pkg.exports)
57
+ : ['.']
58
+ // Preact also exports SSR adapters whose optional renderer peer belongs to
59
+ // server consumers. They cannot be entries of a browser build.
60
+ if (name == 'preact') {
61
+ keys = keys.filter((key) => !key.startsWith('./compat/server'))
62
+ }
63
+ if (name == 'react-dom') {
64
+ keys = keys.filter((key) => !/^\.\/(?:server|static)\./.test(key))
65
+ }
66
+ let out: Record<string, string> = {}
67
+ for (let key of keys.sort()) {
68
+ // A pattern export names no one file; a page imports what it matches.
69
+ if (key.includes('*')) continue
70
+ let spec = key == '.' ? name : name + key.slice(1)
71
+ let path = packaged(files, spec)
72
+ if (path && !path.endsWith('.json')) out[spec] = path
73
+ }
74
+ return out
75
+ }
76
+
77
+ /** An ES module standing for a CommonJS file: its `module.exports` as the
78
+ * default, and each name CommonJS assigns, including those it forwards from
79
+ * the files it re-exports, by name, so `import { useState } from 'react'`
80
+ * links. */
81
+ export let commonJS = (files: Source, path: string): string => {
82
+ initSync()
83
+ let names = new Set<string>()
84
+ let seen = new Set<string>()
85
+ let visit = (file: string) => {
86
+ if (seen.has(file)) return
87
+ seen.add(file)
88
+ let source = files.read(file)
89
+ if (source == null) return
90
+ let { exports, reexports } = cjs(source)
91
+ for (let name of exports) {
92
+ if (name != 'default' && name != '__esModule') names.add(name)
93
+ }
94
+ for (let next of reexports) {
95
+ if (next.startsWith('.')) visit(posix.join(posix.dirname(file), next))
96
+ }
97
+ }
98
+ visit(path)
99
+ return [
100
+ `import value from ${JSON.stringify('/' + path)}`,
101
+ 'export default value',
102
+ ...[...names].sort().map((name, i) =>
103
+ `let n${i} = value[${JSON.stringify(name)}]; export { n${i} as ${
104
+ JSON.stringify(name)
105
+ } }`
106
+ ),
107
+ ].join('\n')
108
+ }
109
+
110
+ // Whether a package file is CommonJS: `.cjs`, or JavaScript with no import
111
+ // or export in it that Node does not type as an ES module (a UMD script
112
+ // under `"type": "module"` is a module that exports nothing, as Node loads
113
+ // it). TypeScript is a module.
114
+ let isCommonJS = (files: Source, path: string) => {
115
+ if (path.endsWith('.cjs')) return true
116
+ if (!path.endsWith('.js') || typedName(files, path) != path) return false
117
+ try {
118
+ return !parse(files.read(path) ?? '')[3]
119
+ } catch {
120
+ return false // esbuild names the syntax error
121
+ }
122
+ }
123
+
124
+ let SCRIPT = /\.[mc]?[jt]sx?$/
125
+
126
+ // CommonJS's `require` of another package, as a module of this build: the
127
+ // package imported bare, as an ES module would import it, which esbuild hands
128
+ // `require` as an object. Its default is what a CommonJS package compiles to
129
+ // (`commonJS`), its `module.exports`.
130
+ let required = (spec: string) =>
131
+ [
132
+ `import * as m from ${JSON.stringify(spec)}`,
133
+ `export * from ${JSON.stringify(spec)}`,
134
+ 'export default m.default',
135
+ ].join('\n')
136
+
137
+ let REQUIRED = '__required/'
138
+ let ENTRY = '__entry/'
139
+
140
+ let production: Options = {
141
+ minify: true,
142
+ define: { 'process.env.NODE_ENV': '"production"' },
143
+ }
144
+
145
+ /** Compile the package `name`, installed in `files` under `node_modules/`,
146
+ * as `key` names it (`preact@10.26.0`): its export entries, minified, for a
147
+ * browser. `moved` is how a module that reads `import.meta.url` finds the
148
+ * package's other files (./bundle.ts). With `subpath`, a specifier a pattern
149
+ * export matches (`three/addons/controls/OrbitControls.js`), the build is of
150
+ * that alone: the files it reaches are in it, and its imports of the
151
+ * package's concrete exports stay bare. Throws esbuild's failure. */
152
+ export let packageModules = async (
153
+ files: Source,
154
+ name: string,
155
+ key: string,
156
+ { moved, subpath }: Pick<Options, 'moved'> & { subpath?: string } = {},
157
+ ): Promise<Compiled> => {
158
+ // A stylesheet entry is a file a page links, not a module.
159
+ let concrete = Object.fromEntries(
160
+ Object.entries(exportsOf(files, name)).filter(([, p]) => SCRIPT.test(p)),
161
+ )
162
+ let entries = concrete
163
+ if (subpath) {
164
+ let path = packaged(files, subpath)
165
+ if (!path || !SCRIPT.test(path)) {
166
+ throw new Unexported(`${subpath} names no module of ${name}`)
167
+ }
168
+ entries = { [subpath]: path }
169
+ }
170
+ let specs = Object.keys(entries).sort()
171
+ let written: Record<string, string> = {}
172
+ let points: Record<string, string> = {}
173
+ specs.forEach((spec, i) => {
174
+ let path = entries[spec]
175
+ if (isCommonJS(files, path)) {
176
+ path = `${ENTRY}${i}.js`
177
+ written[path] = commonJS(files, entries[spec])
178
+ }
179
+ points[`entry-${i}`] = path
180
+ })
181
+ let source: Source = {
182
+ read: (path) =>
183
+ written[path] ??
184
+ (path.startsWith(REQUIRED)
185
+ ? required(path.slice(REQUIRED.length, -'.js'.length))
186
+ : files.read(path)),
187
+ }
188
+ let built = await bundle(source, points, {
189
+ ...production,
190
+ moved,
191
+ alias: (spec, _, kind) =>
192
+ kind == 'require-call' && packageOf(spec)
193
+ ? `${REQUIRED}${spec}.js`
194
+ : undefined,
195
+ // Another package stays bare; so does this one by name, unless what it
196
+ // names is no entry, which only this build can reach.
197
+ external: (spec) => {
198
+ let of = packageOf(spec)
199
+ if (!of) return
200
+ if (of != name || spec in concrete || !packaged(files, spec)) return spec
201
+ },
202
+ })
203
+ let names = Object.fromEntries(
204
+ await Promise.all(
205
+ Object.keys(built.files).map(async (file) => {
206
+ let ext = file.slice(file.lastIndexOf('.'))
207
+ return [file, (await sha256(`${COMPILER}\0${key}\0${file}`)) + ext]
208
+ }),
209
+ ),
210
+ )
211
+ let modules: Record<string, string> = {}
212
+ let imports = new Set<string>()
213
+ let links: Record<string, string[]> = {}
214
+ for (let [file, code] of Object.entries(built.files)) {
215
+ if (!file.endsWith('.js')) {
216
+ modules[names[file]] = code
217
+ continue
218
+ }
219
+ modules[names[file]] = linked(code, names)
220
+ let statics = new Set<string>()
221
+ for (let { n, d } of parse(code)[0]) {
222
+ if (!n) continue
223
+ let bare = packageOf(n)
224
+ if (bare) imports.add(n)
225
+ // -1 is a static import; a dynamic one loads only when it runs.
226
+ if (d != -1) continue
227
+ let to = bare
228
+ ? n
229
+ : n.startsWith('./') && names[n.slice(2)]
230
+ ? `./${names[n.slice(2)]}`
231
+ : undefined
232
+ if (to) statics.add(to)
233
+ }
234
+ links[names[file]] = [...statics].sort()
235
+ }
236
+ return {
237
+ entries: Object.fromEntries(
238
+ specs.map((spec, i) => [spec, names[`entry-${i}.js`]]),
239
+ ),
240
+ modules,
241
+ imports: [...imports].sort(),
242
+ links,
243
+ }
244
+ }
245
+
246
+ // A module's imports of the build's other files, by the names they are kept
247
+ // under.
248
+ let linked = (code: string, names: Record<string, string>) =>
249
+ rewritten(
250
+ code,
251
+ (n) =>
252
+ n.startsWith('./') && names[n.slice(2)]
253
+ ? `./${names[n.slice(2)]}`
254
+ : undefined,
255
+ )
package/plan.js ADDED
@@ -0,0 +1,273 @@
1
+ // What a deploy must compile, read out of an app's files, or nothing.
2
+ //
3
+ // Two kinds of entry. The worker is the app's server source (the host says
4
+ // which file that is), compiled when a file it reaches is TypeScript or JSX,
5
+ // or when it imports a package: the runtime has no registry, so a package it
6
+ // imports is either compiled in or missing. A page script is each
7
+ // `<script type="module" src>` an HTML file loads, compiled when a file it
8
+ // reaches is TypeScript or JSX, or when it imports a package package.json
9
+ // names. A package package.json does not name is left to the browser, which
10
+ // resolves it through the page's import map; the deploy says so when the map
11
+ // does not name it. A module worker a page script starts (`new Worker(new
12
+ // URL('./grow.ts', import.meta.url))`) is a page script too, compiled by the
13
+ // same rule, except that no import map reaches it.
14
+ //
15
+ // File-only apps with no TypeScript or package.json plan nothing. A JavaScript
16
+ // worker can also ask the compiler to embed a text import.
17
+ import { packageOf, resolved, script, sources, typed } from "./graph.js";
18
+ import { dependencies, Unplanned } from "./dependencies.js";
19
+ export { dependencies, Unplanned };
20
+ // What esbuild reads as source. Anything else a worker imports is a module of
21
+ // its own kind (`.wasm`, `.txt`, bytes) that the runtime links as it is.
22
+ let COMPILED = /\.(?:js|mjs|cjs|ts|tsx|jsx|mts|cts|json|css)$/;
23
+ let decode = (bytes)=>new TextDecoder().decode(bytes);
24
+ /** A catalog's {@link Toolkits}.
25
+ *
26
+ * ```ts
27
+ * import { assertEquals } from '@std/assert'
28
+ * assertEquals(
29
+ * toolkits({
30
+ * '@yaks/a': {
31
+ * files: {},
32
+ * dependencies: { '@yaks/b': 'platform', preact: '10.29.7' },
33
+ * version: 'sha256:a',
34
+ * },
35
+ * }),
36
+ * { '@yaks/a': { version: 'sha256:a', uses: ['@yaks/b'] } },
37
+ * )
38
+ * ```
39
+ */ export let toolkits = (catalog)=>Object.fromEntries(Object.entries(catalog).map(([name, pkg])=>[
40
+ name,
41
+ {
42
+ version: pkg.version,
43
+ uses: Object.keys(pkg.dependencies).filter((dep)=>dep.startsWith('@yaks/'))
44
+ }
45
+ ]));
46
+ /** The compiler's own entry among the toolkit's digests, where a host keeps
47
+ * one: the source of this package, which makes what pages compile to, as a
48
+ * toolkit package's source makes what they import. */ export let COMPILER = '@yaks/esbuild';
49
+ /** The source digest of each toolkit package a package.json reaches, through
50
+ * the toolkit's own dependencies, as a compile seeds them, and of the
51
+ * compiler: what that compile was made of. A package the toolkit does not
52
+ * have is left out.
53
+ *
54
+ * ```ts
55
+ * import { assertEquals } from '@std/assert'
56
+ * let kit = {
57
+ * '@yaks/a': { version: 'sha256:a', uses: ['@yaks/b'] },
58
+ * '@yaks/b': { version: 'sha256:b', uses: [] },
59
+ * '@yaks/c': { version: 'sha256:c', uses: [] },
60
+ * }
61
+ * let pkg = '{"dependencies": {"@yaks/a": "platform", "three": "1"}}'
62
+ * assertEquals(toolkitOf(pkg, kit), {
63
+ * '@yaks/a': 'sha256:a',
64
+ * '@yaks/b': 'sha256:b',
65
+ * })
66
+ * assertEquals(toolkitOf(null, kit), {})
67
+ * assertEquals(
68
+ * toolkitOf(null, { '@yaks/esbuild': { version: 'sha256:e', uses: [] } }),
69
+ * { '@yaks/esbuild': 'sha256:e' },
70
+ * )
71
+ * ```
72
+ */ export let toolkitOf = (pkg, kit)=>{
73
+ let out = kit[COMPILER] ? {
74
+ [COMPILER]: kit[COMPILER].version
75
+ } : {};
76
+ let visit = (name)=>{
77
+ if (name in out || !kit[name]) return;
78
+ out[name] = kit[name].version;
79
+ kit[name].uses.forEach(visit);
80
+ };
81
+ Object.keys(dependencies(pkg)).forEach(visit);
82
+ return out;
83
+ };
84
+ let TAG = /<script\b[^>]*>/gi;
85
+ let SRC = /\bsrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))/i;
86
+ let MODULE = /\btype\s*=\s*(?:"module"|'module'|module\b)/i;
87
+ /** The module scripts an HTML file loads from the app's own files, by path.
88
+ *
89
+ * ```ts
90
+ * import { assertEquals } from '@std/assert'
91
+ * assertEquals(
92
+ * loaded('games/index.html', `<script type="module" src="./main.ts?v=2">` +
93
+ * `</script><script src="old.js"></script>` +
94
+ * `<script type=module src="https://esm.sh/x"></script>`),
95
+ * ['games/main.ts'],
96
+ * )
97
+ * ```
98
+ */ export let loaded = (page, html)=>[
99
+ ...html.matchAll(TAG)
100
+ ].filter(([tag])=>MODULE.test(tag)).map(([tag])=>tag.match(SRC)).map((m)=>m && (m[1] ?? m[2] ?? m[3]).replace(/[?#].*$/, '')).filter((src)=>!!src && !src.startsWith('/') && !/^[a-z][a-z0-9+.-]*:/i.test(src)).map((src)=>resolved(page, src.startsWith('.') ? src : `./${src}`));
101
+ let MAP = /<script\b[^>]*\btype\s*=\s*(?:"importmap"|'importmap'|importmap\b)[^>]*>([\s\S]*?)<\/script/gi;
102
+ // The keys of an import map's `imports`; none when it is not JSON, which the
103
+ // browser refuses too.
104
+ let keys = (json)=>{
105
+ try {
106
+ let imports = JSON.parse(json)?.imports;
107
+ return imports && typeof imports == 'object' ? Object.keys(imports) : [];
108
+ } catch {
109
+ return [];
110
+ }
111
+ };
112
+ /** What an HTML file's import maps name: every key of their `imports`.
113
+ *
114
+ * ```ts
115
+ * import { assertEquals } from '@std/assert'
116
+ * assertEquals(
117
+ * mapped(`<script type=importmap>{"imports": {"three": "https://esm.sh/` +
118
+ * `three", "lit/": "https://esm.sh/lit/"}}</script>`),
119
+ * ['three', 'lit/'],
120
+ * )
121
+ * ```
122
+ */ export let mapped = (html)=>[
123
+ ...html.matchAll(MAP)
124
+ ].flatMap(([, json])=>keys(json));
125
+ // Whether an import map's keys resolve a specifier: a key names it, or ends
126
+ // in `/` and begins it.
127
+ let covers = (map, spec)=>map.some((key)=>key == spec || key.endsWith('/') && spec.startsWith(key));
128
+ /**
129
+ * What a deploy of this app compiles, or null when nothing needs compiling.
130
+ * Throws {@link Unplanned} when package.json cannot be read.
131
+ */ export let plan = async (app)=>{
132
+ let main = app.main && app.paths.includes(app.main) ? app.main : undefined;
133
+ // Skip file-only apps that have no inputs to the compiler.
134
+ if (!main && !app.paths.includes('package.json') && !app.paths.some(typed)) {
135
+ return null;
136
+ }
137
+ let has = new Set(app.paths);
138
+ // The worker and each page walk their own graph, often through the same
139
+ // modules. Keep one reading of each file for this deploy.
140
+ let reads = new Map();
141
+ let readApp = (path)=>{
142
+ if (!has.has(path)) return Promise.resolve(null);
143
+ let pending = reads.get(path);
144
+ if (pending) return pending;
145
+ pending = app.read(path);
146
+ reads.set(path, pending);
147
+ return pending;
148
+ };
149
+ let text = async (path)=>{
150
+ let bytes = await readApp(path);
151
+ return bytes && decode(bytes);
152
+ };
153
+ let pkg = await text('package.json');
154
+ let named = new Set(Object.keys(dependencies(pkg)));
155
+ let files = {};
156
+ let notes = [];
157
+ let inputs = {
158
+ pages: {}
159
+ };
160
+ let send = ({ files: reached, texts })=>{
161
+ let paths = [
162
+ ...reached.keys()
163
+ ].filter((p)=>COMPILED.test(p) || texts.has(p));
164
+ for (let path of paths)files[path] = decode(reached.get(path));
165
+ return paths;
166
+ };
167
+ let walk = sources(app.strip);
168
+ let worker;
169
+ if (main) {
170
+ let graph = await walk(readApp, main);
171
+ let { files: reached, named: specs, texts, linked } = graph;
172
+ let packages = specs.filter((s)=>packageOf(s));
173
+ if (app.entryOnly || [
174
+ ...reached.keys()
175
+ ].some(typed) || packages.length || texts.size) {
176
+ inputs.worker = send(graph);
177
+ worker = {
178
+ entry: main,
179
+ flags: app.flags ?? [],
180
+ carry: [
181
+ ...reached.keys()
182
+ ].filter((p)=>!COMPILED.test(p) && (!texts.has(p) || linked.has(p)))
183
+ };
184
+ }
185
+ }
186
+ // Each module script the pages load, with the import map of every page
187
+ // that loads it. A worker a script starts joins as the walk finds it, with
188
+ // no import map at all, since none reaches a worker.
189
+ let loads = new Map();
190
+ let started = new Set();
191
+ let served = app.served ? `${app.served}/` : '';
192
+ let pagesOf = app.entryOnly ? [] : app.paths.filter((p)=>/\.html?$/i.test(p) && p.startsWith(served));
193
+ let documents = [];
194
+ let left = {};
195
+ for (let page of pagesOf){
196
+ let html = await text(page) ?? '';
197
+ let entries = loaded(page, html);
198
+ documents.push(entries);
199
+ for (let entry of entries){
200
+ loads.set(entry, [
201
+ ...loads.get(entry) ?? [],
202
+ mapped(html)
203
+ ]);
204
+ }
205
+ }
206
+ let pages = [];
207
+ for (let [entry, maps] of loads){
208
+ if (!has.has(entry) || !script(entry)) continue;
209
+ let graph = await walk(readApp, entry);
210
+ let { files: reached, named: specs, started: starts, texts } = graph;
211
+ let wanted = specs.filter((s)=>named.has(packageOf(s) ?? ''));
212
+ let compiled = [
213
+ ...reached.keys()
214
+ ].some(typed) || wanted.length > 0 || texts.size > 0;
215
+ // esbuild leaves `import.meta.url` as written, so in a compiled script
216
+ // it is the entry's address, and a worker's path resolves against the
217
+ // entry; in a script served as written, against the file that names it.
218
+ for (let [from, spec] of starts){
219
+ let path = resolved(compiled ? entry : from, spec);
220
+ if (loads.has(path)) continue;
221
+ started.add(path);
222
+ loads.set(path, [
223
+ []
224
+ ]);
225
+ }
226
+ if (!compiled) continue;
227
+ pages.push(entry);
228
+ inputs.pages[entry] = send(graph);
229
+ // A package the compile leaves in is the browser's to resolve, and only
230
+ // an import map on every page that loads the script resolves it.
231
+ for (let spec of new Set(specs)){
232
+ let name = packageOf(spec);
233
+ if (!name || named.has(name)) continue;
234
+ if (maps.every((m)=>covers(m, spec))) {
235
+ ;
236
+ (left[entry] ??= []).push(spec);
237
+ continue;
238
+ }
239
+ notes.push(`${entry} imports ${spec}, which ` + (started.has(entry) ? 'package.json does not name, and no import map reaches a ' + 'module worker, so the browser cannot load it' : "neither package.json nor the page's import map names, so the " + 'browser cannot load it'));
240
+ }
241
+ for (let css of [
242
+ ...reached.keys()
243
+ ].filter((p)=>p.endsWith('.css') && !texts.has(p))){
244
+ notes.push(`${entry} imports ${css}, and a compiled page script leaves CSS ` + `out: link it from the page instead`);
245
+ }
246
+ }
247
+ if (!worker && !pages.length) return null;
248
+ if (pkg != null) files['package.json'] = pkg;
249
+ let lock = await text('package-lock.json');
250
+ if (lock != null) files['package-lock.json'] = lock;
251
+ let together = documents.map((entries)=>[
252
+ ...new Set(entries.filter((e)=>pages.includes(e)))
253
+ ]).filter((entries)=>entries.length > 1);
254
+ return {
255
+ ask: {
256
+ files,
257
+ worker,
258
+ pages,
259
+ ...together.length ? {
260
+ together
261
+ } : {},
262
+ ...Object.keys(left).length ? {
263
+ mapped: left
264
+ } : {}
265
+ },
266
+ notes,
267
+ inputs,
268
+ reads: [
269
+ ...reads.keys()
270
+ ]
271
+ };
272
+ };
273
+ //# sourceMappingURL=plan.js.map
package/plan.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan.js","sources":["./plan.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