@eighty4/dank 0.0.4-1 → 0.0.4-3

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,210 +0,0 @@
1
- import EventEmitter from 'node:events';
2
- import { writeFile } from 'node:fs/promises';
3
- import { dirname, join, resolve } from 'node:path';
4
- class ResolverImpl {
5
- #dirs;
6
- constructor(dirs) {
7
- this.#dirs = dirs;
8
- }
9
- isProjectSubpathInPagesDir(p) {
10
- return resolve(join(this.#dirs.projectResolved, p)).startsWith(this.#dirs.pagesResolved);
11
- }
12
- isPagesSubpathInPagesDir(p) {
13
- return this.isProjectSubpathInPagesDir(join(this.#dirs.pages, p));
14
- }
15
- resolveHrefInPagesDir(from, href) {
16
- const p = join(dirname(from), href);
17
- if (this.isProjectSubpathInPagesDir(p)) {
18
- return p;
19
- }
20
- else {
21
- return 'outofbounds';
22
- }
23
- }
24
- }
25
- // manages website resources during `dank build` and `dank serve`
26
- export class WebsiteRegistry extends EventEmitter {
27
- #build;
28
- // paths of bundled esbuild outputs
29
- #bundles = new Set();
30
- // public dir assets
31
- #copiedAssets = null;
32
- // map of entrypoints to their output path
33
- #entrypointHrefs = {};
34
- #pageUrls = [];
35
- #resolver;
36
- #workers = null;
37
- constructor(build) {
38
- super();
39
- this.#build = build;
40
- this.#resolver = new ResolverImpl(build.dirs);
41
- }
42
- set copiedAssets(copiedAssets) {
43
- this.#copiedAssets =
44
- copiedAssets === null ? null : new Set(copiedAssets);
45
- }
46
- set pageUrls(pageUrls) {
47
- this.#pageUrls = pageUrls;
48
- }
49
- get resolver() {
50
- return this.#resolver;
51
- }
52
- // bundleOutputs(type?: 'css' | 'js'): Array<string> {
53
- // if (!type) {
54
- // return Array.from(this.#bundles)
55
- // } else {
56
- // return Array.from(this.#bundles).filter(p => p.endsWith(type))
57
- // }
58
- // }
59
- buildRegistry() {
60
- return new BuildRegistry(this.#build, this.#onBuildManifest);
61
- }
62
- files() {
63
- const files = new Set();
64
- for (const pageUrl of this.#pageUrls)
65
- files.add(pageUrl === '/' ? '/index.html' : `${pageUrl}/index.html`);
66
- for (const f of this.#bundles)
67
- files.add(f);
68
- if (this.#copiedAssets)
69
- for (const f of this.#copiedAssets)
70
- files.add(f);
71
- return files;
72
- }
73
- mappedHref(lookup) {
74
- const found = this.#entrypointHrefs[lookup];
75
- if (found) {
76
- return found;
77
- }
78
- else {
79
- throw Error(`mapped href for ${lookup} not found`);
80
- }
81
- }
82
- workerEntryPoints() {
83
- return (this.#workers?.map(({ workerEntryPoint }) => ({
84
- in: workerEntryPoint,
85
- out: workerEntryPoint
86
- .replace(/^pages[\//]/, '')
87
- .replace(/\.(mj|t)s$/, '.js'),
88
- })) || null);
89
- }
90
- workers() {
91
- return this.#workers;
92
- }
93
- async writeManifest(buildTag) {
94
- const manifest = this.#manifest(buildTag);
95
- await writeFile(join(this.#build.dirs.projectRootAbs, this.#build.dirs.buildRoot, 'website.json'), JSON.stringify({
96
- buildTag,
97
- files: Array.from(manifest.files),
98
- pageUrls: Array.from(manifest.pageUrls),
99
- }, null, 4));
100
- return manifest;
101
- }
102
- #manifest(buildTag) {
103
- return {
104
- buildTag,
105
- files: this.files(),
106
- pageUrls: new Set(this.#pageUrls),
107
- };
108
- }
109
- #onBuildManifest = (build) => {
110
- // collect built bundle entrypoint hrefs
111
- for (const [outPath, entrypoint] of Object.entries(build.bundles)) {
112
- this.#bundles.add(outPath);
113
- if (entrypoint) {
114
- this.#entrypointHrefs[entrypoint] = outPath;
115
- }
116
- }
117
- // determine if worker entrypoints have changed
118
- let updatedWorkerEntrypoints = false;
119
- const previousWorkers = this.#workers === null
120
- ? null
121
- : new Set(this.#workers.map(w => w.workerEntryPoint));
122
- if (build.workers) {
123
- if (!previousWorkers ||
124
- previousWorkers.size !==
125
- new Set(build.workers.map(w => w.workerEntryPoint)).size) {
126
- updatedWorkerEntrypoints = true;
127
- }
128
- else {
129
- updatedWorkerEntrypoints = !build.workers.every(w => previousWorkers.has(w.workerEntryPoint));
130
- }
131
- }
132
- else if (previousWorkers) {
133
- updatedWorkerEntrypoints = true;
134
- }
135
- // merge unique entrypoints from built workers with registry state
136
- // todo filtering out unique occurrences of clientScript and workerUrl
137
- // drops reporting/summary/debugging capabilities, but currently
138
- // this.#workers is used for unique worker/client entrypoints
139
- if (build.workers) {
140
- if (!this.#workers) {
141
- this.#workers = build.workers;
142
- }
143
- else {
144
- for (const w of build.workers) {
145
- const found = this.#workers.find(w2 => {
146
- return (w.dependentEntryPoint === w2.dependentEntryPoint &&
147
- w.workerEntryPoint === w2.workerEntryPoint);
148
- });
149
- if (!found) {
150
- this.#workers.push(w);
151
- }
152
- }
153
- }
154
- }
155
- if (updatedWorkerEntrypoints) {
156
- this.emit('workers');
157
- }
158
- };
159
- }
160
- // result accumulator of an esbuild `build` or `Context.rebuild`
161
- export class BuildRegistry {
162
- #onComplete;
163
- #resolver;
164
- #workers = null;
165
- constructor(build, onComplete) {
166
- this.#onComplete = onComplete;
167
- this.#resolver = new ResolverImpl(build.dirs);
168
- }
169
- get resolver() {
170
- return this.#resolver;
171
- }
172
- // resolve web worker imported by a webpage module
173
- addWorker(worker) {
174
- // todo normalize path
175
- if (!this.#workers) {
176
- this.#workers = [worker];
177
- }
178
- else {
179
- this.#workers.push(worker);
180
- }
181
- }
182
- completeBuild(result) {
183
- const bundles = {};
184
- for (const [outPath, output] of Object.entries(result.metafile.outputs)) {
185
- bundles[outPath.replace(/^build[/\\]dist/, '')] =
186
- output.entryPoint || null;
187
- }
188
- let workers = null;
189
- if (this.#workers) {
190
- workers = [];
191
- for (const output of Object.values(result.metafile.outputs)) {
192
- if (!output.entryPoint)
193
- continue;
194
- const inputs = Object.keys(output.inputs);
195
- for (const worker of this.#workers) {
196
- if (inputs.includes(worker.clientScript)) {
197
- workers.push({
198
- ...worker,
199
- dependentEntryPoint: output.entryPoint,
200
- });
201
- }
202
- }
203
- }
204
- }
205
- this.#onComplete({
206
- bundles,
207
- workers,
208
- });
209
- }
210
- }