@riboseinc/anafero-cli 0.0.47 → 0.0.49
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/bootstrap.js +34 -27
- package/bootstrap.js.map +3 -3
- package/build-site.mjs +445 -266
- package/dependencies.mts +325 -174
- package/generate-to-filesystem.tsx +57 -11
- package/package.json +1 -1
- package/riboseinc-anafero-cli-0.0.49.tgz +0 -0
- package/riboseinc-anafero-cli-0.0.47.tgz +0 -0
package/dependencies.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { join, basename } from 'node:path';
|
|
2
|
-
import { rmdir, readFile, cp, mkdtemp, stat } from 'node:fs/promises';
|
|
2
|
+
import { readdir, rmdir, readFile, cp, mkdtemp, stat, mkdir, writeFile } from 'node:fs/promises';
|
|
3
3
|
import vm, { type Module as VMModule } from 'node:vm';
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import fs from 'node:fs';
|
|
@@ -31,7 +31,7 @@ const preloaded = {
|
|
|
31
31
|
'prosemirror-schema-list': pmSchemaList,
|
|
32
32
|
'react-helmet': helmet,
|
|
33
33
|
'react': react,
|
|
34
|
-
} as Record<string, unknown
|
|
34
|
+
} as Readonly<Record<string, unknown>>;
|
|
35
35
|
|
|
36
36
|
|
|
37
37
|
const depRegistry: Record<string, Promise<unknown>> = {};
|
|
@@ -140,6 +140,9 @@ const decoder = new TextDecoder();
|
|
|
140
140
|
* with instantiated module and a map of any other generated files.
|
|
141
141
|
* NOTE: Doesn’t really validate that returned module conforms to anything.
|
|
142
142
|
*
|
|
143
|
+
* Handles caching (if a module was already requested, will resolve
|
|
144
|
+
* the same promise).
|
|
145
|
+
*
|
|
143
146
|
* This currently relies on Node, and is therefore here in the CLI module;
|
|
144
147
|
* see TODO about resolveDir about making dependencies buildable
|
|
145
148
|
* in the browser.
|
|
@@ -149,194 +152,342 @@ async function fetchDependency(
|
|
|
149
152
|
moduleRef,
|
|
150
153
|
onProgress,
|
|
151
154
|
) {
|
|
152
|
-
|
|
153
|
-
|
|
155
|
+
|
|
156
|
+
if (!depRegistry[moduleRef]) {
|
|
157
|
+
depRegistry[moduleRef] = async function resolveDep() {
|
|
158
|
+
let sourceDir: string;
|
|
159
|
+
|
|
160
|
+
const localPath = moduleRef.split('file:')[1];
|
|
161
|
+
if (!localPath) {
|
|
162
|
+
//throw new Error("Only dependencies on local filesystem are supported for now.");
|
|
163
|
+
onProgress({ state: `fetching ${moduleRef} to ${localPath}` });
|
|
164
|
+
sourceDir = await fetchSourceFromGit(moduleRef, onProgress);
|
|
165
|
+
} else {
|
|
166
|
+
sourceDir = localPath;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const [bundledCode, otherFiles] = await getDependencyAssets(
|
|
170
|
+
moduleRef,
|
|
171
|
+
sourceDir,
|
|
172
|
+
onProgress,
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
const code = decoder.decode(bundledCode);
|
|
176
|
+
|
|
177
|
+
onProgress({ state: "instantiating module" });
|
|
178
|
+
|
|
179
|
+
// const fn = `${process.cwd()}/test.js`;
|
|
180
|
+
// console.debug("Will write to", fn);
|
|
181
|
+
// await writeFile(fn, result.outputFiles[0]!.contents, { encoding: 'utf-8' })
|
|
182
|
+
|
|
183
|
+
const context = vm.createContext({
|
|
184
|
+
Array,
|
|
185
|
+
Object,
|
|
186
|
+
crypto,
|
|
187
|
+
XPathResult,
|
|
188
|
+
document: getDoc(),
|
|
189
|
+
console,
|
|
190
|
+
Function,
|
|
191
|
+
setTimeout,
|
|
192
|
+
clearTimeout,
|
|
193
|
+
setInterval,
|
|
194
|
+
TextEncoder,
|
|
195
|
+
TextDecoder,
|
|
196
|
+
Blob,
|
|
197
|
+
btoa,
|
|
198
|
+
atob,
|
|
199
|
+
});
|
|
200
|
+
const mod = new vm.SourceTextModule(code, {
|
|
201
|
+
// TODO: Try moduleRef as VM module identifier?
|
|
202
|
+
// Take care of special characters, though.
|
|
203
|
+
identifier: 'anafero-dependency',
|
|
204
|
+
context,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
async function link(specifier: string, referencingModule: VMModule) {
|
|
208
|
+
const isAbsoluteURL = specifier.startsWith('https://');
|
|
209
|
+
let base: string | undefined;
|
|
210
|
+
try {
|
|
211
|
+
new URL(referencingModule.identifier);
|
|
212
|
+
base = referencingModule.identifier;
|
|
213
|
+
// The module that does the importing is referenced by URL.
|
|
214
|
+
} catch (e) {
|
|
215
|
+
// The module that does the importing is not referenced by URL.
|
|
216
|
+
base = undefined;
|
|
217
|
+
}
|
|
218
|
+
const isRelativePath =
|
|
219
|
+
specifier.startsWith('/')
|
|
220
|
+
|| specifier.startsWith('./')
|
|
221
|
+
|| specifier.startsWith('../');
|
|
222
|
+
const isRelativeURL = isRelativePath && base !== undefined;
|
|
223
|
+
|
|
224
|
+
if (isAbsoluteURL || isRelativeURL) {
|
|
225
|
+
// Create a new absolute URL from the imported
|
|
226
|
+
// module's URL (specifier) and the parent module's
|
|
227
|
+
// URL (referencingModule.identifier).
|
|
228
|
+
//console.debug("anafero: building module URL", specifier, 'imported from', referencingModule.identifier);
|
|
229
|
+
if (isRelativeURL && base === undefined) {
|
|
230
|
+
throw new Error("Unable to resolve relative specifier without a base");
|
|
231
|
+
}
|
|
232
|
+
const url = new URL(specifier, base).toString();
|
|
233
|
+
// Download the raw source code.
|
|
234
|
+
//console.debug("anafero: fetching module", url);
|
|
235
|
+
const source = await (await fetch(url)).text();
|
|
236
|
+
// TODO: Fetched source needs to be cached appropriately
|
|
237
|
+
// Version needs to be taken into account?
|
|
238
|
+
|
|
239
|
+
// Instantiate a new module and return it.
|
|
240
|
+
return new vm.SourceTextModule(source, {
|
|
241
|
+
identifier: url,
|
|
242
|
+
context: referencingModule.context,
|
|
243
|
+
});
|
|
244
|
+
} else {
|
|
245
|
+
//console.debug("anafero: fetching preloaded", specifier);
|
|
246
|
+
const madeAvailable = preloaded[specifier]
|
|
247
|
+
? preloaded[specifier]
|
|
248
|
+
// TODO: Don’t do the following
|
|
249
|
+
: await import(specifier);
|
|
250
|
+
const exportNames = Object.keys(madeAvailable);
|
|
251
|
+
// Construct a new module from the actual import
|
|
252
|
+
return new vm.SyntheticModule(
|
|
253
|
+
exportNames,
|
|
254
|
+
function () {
|
|
255
|
+
for (const name of exportNames) {
|
|
256
|
+
this.setExport(name, madeAvailable[name]);
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
identifier: specifier,
|
|
261
|
+
context: referencingModule.context,
|
|
262
|
+
},
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
await mod.link(link);
|
|
268
|
+
|
|
269
|
+
await mod.evaluate();
|
|
270
|
+
|
|
271
|
+
const defaultExport = (mod.namespace as any).default as any;
|
|
272
|
+
|
|
273
|
+
dependencySources[moduleRef] = code;
|
|
274
|
+
dependencySupportingFiles[moduleRef] = otherFiles;
|
|
275
|
+
|
|
276
|
+
return defaultExport;
|
|
277
|
+
|
|
278
|
+
// It would be nice if this did work…
|
|
279
|
+
//let module = {};
|
|
280
|
+
//const req = (pkg: string) => {
|
|
281
|
+
// console.debug("REQUIRED", pkg);
|
|
282
|
+
// const result = import(pkg);
|
|
283
|
+
// console.debug("GOT", result);
|
|
284
|
+
// return result;
|
|
285
|
+
//};
|
|
286
|
+
//console.debug((new Function('module', 'require', decoder.decode(result.outputFiles[0]!.contents)))(module, req));
|
|
287
|
+
//const out = await import(fn);
|
|
288
|
+
//console.debug(out.name);
|
|
289
|
+
//throw new Error("TODO");
|
|
290
|
+
//const data = await readFile(outfile, { encoding: 'utf-8' });
|
|
291
|
+
//return new Function(data);
|
|
292
|
+
}();
|
|
154
293
|
}
|
|
155
|
-
depRegistry[moduleRef] = async function resolveDep() {
|
|
156
|
-
let sourceDir: string;
|
|
157
|
-
|
|
158
|
-
const localPath = moduleRef.split('file:')[1];
|
|
159
|
-
if (!localPath) {
|
|
160
|
-
//throw new Error("Only dependencies on local filesystem are supported for now.");
|
|
161
|
-
onProgress({ state: `fetching ${moduleRef} to ${localPath}` });
|
|
162
|
-
sourceDir = await fetchSourceFromGit(moduleRef, onProgress);
|
|
163
|
-
} else {
|
|
164
|
-
sourceDir = localPath;
|
|
165
|
-
}
|
|
166
294
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
`anafero-dist-${moduleRef.replace(/[^a-z0-9]/gi, '_')}-`,
|
|
170
|
-
));
|
|
295
|
+
return await depRegistry[moduleRef] as any;
|
|
296
|
+
};
|
|
171
297
|
|
|
172
|
-
onProgress({ state: `copying into build dir ${buildDir}` });
|
|
173
|
-
|
|
174
|
-
await cp(sourceDir, buildDir, { recursive: true });
|
|
175
|
-
|
|
176
|
-
//const outfile = join(buildDir, 'out.mjs');
|
|
177
|
-
//await esbuildTransform('oi');
|
|
178
|
-
|
|
179
|
-
onProgress({ state: "compiling" });
|
|
180
|
-
|
|
181
|
-
const result = await esbuild({
|
|
182
|
-
//entryPoints: [join(buildDir, 'index.mts')],
|
|
183
|
-
stdin: {
|
|
184
|
-
contents: await readFile(join(buildDir, 'index.mts')),
|
|
185
|
-
loader: 'ts',
|
|
186
|
-
|
|
187
|
-
// TODO: This means we use filesystem when resolving
|
|
188
|
-
// imports in the entry point. That’s not great, if we
|
|
189
|
-
// want to build e.g. in the browser.
|
|
190
|
-
// It may be possible to avoid this by writing a custom
|
|
191
|
-
// resolver plugin for esbuild. See
|
|
192
|
-
// - https://github.com/evanw/esbuild/issues/591#issuecomment-742962090
|
|
193
|
-
// - https://esbuild.github.io/plugins/#on-resolve-arguments
|
|
194
|
-
resolveDir: buildDir,
|
|
195
|
-
|
|
196
|
-
sourcefile: 'index.mts',
|
|
197
|
-
},
|
|
198
|
-
loader: {
|
|
199
|
-
'.mts': 'ts',
|
|
200
|
-
'.css': 'local-css',
|
|
201
|
-
},
|
|
202
|
-
entryNames: '[dir]/[name]',
|
|
203
|
-
assetNames: '[dir]/[name]',
|
|
204
|
-
format: 'esm',
|
|
205
|
-
target: ['es2022'],
|
|
206
|
-
tsconfigRaw: '{}',
|
|
207
|
-
//external: [],
|
|
208
|
-
packages: 'external',
|
|
209
|
-
//plugins: [{
|
|
210
|
-
// name: 'plugin-resolver',
|
|
211
|
-
// setup(build) {
|
|
212
|
-
// build.onLoad({ filter: /^prosemirror-model-metanorma/ }, args => {
|
|
213
|
-
// return import(args.path);
|
|
214
|
-
// });
|
|
215
|
-
// },
|
|
216
|
-
//}],
|
|
217
|
-
minify: false,
|
|
218
|
-
platform: 'browser',
|
|
219
|
-
write: false,
|
|
220
|
-
logLevel: 'silent',
|
|
221
|
-
//logLevel: 'info',
|
|
222
|
-
sourcemap: true,
|
|
223
|
-
bundle: true,
|
|
224
|
-
outfile: 'index.js',
|
|
225
|
-
treeShaking: true,
|
|
226
|
-
//outfile,
|
|
227
|
-
});
|
|
228
298
|
|
|
229
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Builds dependency from TypeScript if needed,
|
|
301
|
+
* returns dependency source as string.
|
|
302
|
+
*
|
|
303
|
+
* Does not cache anything.
|
|
304
|
+
*/
|
|
305
|
+
async function getDependencyAssets(
|
|
306
|
+
moduleRef: string,
|
|
307
|
+
sourceDirPath: string,
|
|
308
|
+
onProgress: (progress: Progress) => void,
|
|
309
|
+
): Promise<[
|
|
310
|
+
dependency: Uint8Array,
|
|
311
|
+
assets: Record<string, Uint8Array>,
|
|
312
|
+
]> {
|
|
313
|
+
if (await isPreBuilt(sourceDirPath)) {
|
|
314
|
+
console.debug("Using pre-built", moduleRef);
|
|
315
|
+
return await readPreBuiltDependency(sourceDirPath);
|
|
316
|
+
} else {
|
|
317
|
+
return await buildDependency(moduleRef, sourceDirPath, onProgress);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
230
320
|
|
|
231
|
-
const mainOutput = result.outputFiles.
|
|
232
|
-
find(({ path }) => basename(path) === 'index.js')?.contents;
|
|
233
321
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
for (const { path, contents } of result.outputFiles) {
|
|
238
|
-
if (!path.endsWith('/index.js')) {
|
|
239
|
-
otherFiles[basename(path)] = contents;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
322
|
+
function getPreBuiltRoot(sourceDirPath: string): string {
|
|
323
|
+
return join(sourceDirPath, 'dist');
|
|
324
|
+
}
|
|
243
325
|
|
|
244
|
-
const code = decoder.decode(mainOutput);
|
|
245
|
-
|
|
246
|
-
onProgress({ state: "instantiating module" });
|
|
247
|
-
|
|
248
|
-
// const fn = `${process.cwd()}/test.js`;
|
|
249
|
-
// console.debug("Will write to", fn);
|
|
250
|
-
// await writeFile(fn, result.outputFiles[0]!.contents, { encoding: 'utf-8' })
|
|
251
|
-
|
|
252
|
-
const context = vm.createContext({
|
|
253
|
-
Array,
|
|
254
|
-
Object,
|
|
255
|
-
crypto,
|
|
256
|
-
XPathResult,
|
|
257
|
-
document: getDoc(),
|
|
258
|
-
console,
|
|
259
|
-
Function,
|
|
260
|
-
setTimeout,
|
|
261
|
-
setInterval,
|
|
262
|
-
TextEncoder,
|
|
263
|
-
});
|
|
264
|
-
const mod = new vm.SourceTextModule(code, {
|
|
265
|
-
// TODO: Try moduleRef as VM module identifier?
|
|
266
|
-
// Take care of special characters, though.
|
|
267
|
-
identifier: 'anafero-dependency',
|
|
268
|
-
context,
|
|
269
|
-
});
|
|
270
326
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
return new vm.SourceTextModule(source, {
|
|
284
|
-
identifier: url,
|
|
285
|
-
context: referencingModule.context,
|
|
286
|
-
});
|
|
287
|
-
} else {
|
|
288
|
-
const madeAvailable = preloaded[specifier]
|
|
289
|
-
? preloaded[specifier]
|
|
290
|
-
// TODO: Don’t do the following
|
|
291
|
-
: await import(specifier);
|
|
292
|
-
const exportNames = Object.keys(madeAvailable);
|
|
293
|
-
// Construct a new module from the actual import
|
|
294
|
-
return new vm.SyntheticModule(
|
|
295
|
-
exportNames,
|
|
296
|
-
function () {
|
|
297
|
-
for (const name of exportNames) {
|
|
298
|
-
this.setExport(name, madeAvailable[name]);
|
|
299
|
-
}
|
|
300
|
-
},
|
|
301
|
-
{
|
|
302
|
-
identifier: specifier,
|
|
303
|
-
context: referencingModule.context,
|
|
304
|
-
},
|
|
305
|
-
);
|
|
327
|
+
async function readPreBuiltJSBundle(sourceDirPath: string):
|
|
328
|
+
Promise<Uint8Array> {
|
|
329
|
+
const bundlePath = join(
|
|
330
|
+
getPreBuiltRoot(sourceDirPath),
|
|
331
|
+
PRE_BUILT_JS_BUNDLE_FILENAME);
|
|
332
|
+
const bundleStat = await stat(bundlePath);
|
|
333
|
+
if (bundleStat.isFile()) {
|
|
334
|
+
return await readFile(bundlePath);
|
|
335
|
+
} else {
|
|
336
|
+
throw new Error("Pre-built entry point is not a file");
|
|
337
|
+
}
|
|
338
|
+
}
|
|
306
339
|
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
340
|
|
|
310
|
-
|
|
341
|
+
const PRE_BUILT_JS_BUNDLE_FILENAME = 'index.js';
|
|
342
|
+
|
|
311
343
|
|
|
312
|
-
|
|
344
|
+
async function readPreBuiltAssets(sourceDirPath: string):
|
|
345
|
+
Promise<Record<string, Uint8Array>> {
|
|
346
|
+
const distroot = getPreBuiltRoot(sourceDirPath);
|
|
347
|
+
const filenames = await readdir(distroot);
|
|
348
|
+
const assets = filenames.filter(fn => fn !== PRE_BUILT_JS_BUNDLE_FILENAME);
|
|
349
|
+
return (
|
|
350
|
+
(await Promise.all(
|
|
351
|
+
assets.map(async (fn) => ({ [fn]: await readFile(join(distroot, fn)) }))
|
|
352
|
+
)).
|
|
353
|
+
reduce((prev, curr) => ({ ...prev, ...curr }), {})
|
|
354
|
+
);
|
|
355
|
+
}
|
|
313
356
|
|
|
314
|
-
const defaultExport = (mod.namespace as any).default as any;
|
|
315
357
|
|
|
316
|
-
|
|
358
|
+
export async function writePreBuiltAssets(
|
|
359
|
+
sourceDirPath: string,
|
|
360
|
+
bundledCode: Uint8Array,
|
|
361
|
+
assets: Record<string, Uint8Array>,
|
|
362
|
+
) {
|
|
363
|
+
const distDir = getPreBuiltRoot(sourceDirPath);
|
|
364
|
+
await mkdir(distDir);
|
|
365
|
+
console.debug("Writing index.js");
|
|
366
|
+
await writeFile(join(distDir, PRE_BUILT_JS_BUNDLE_FILENAME), bundledCode);
|
|
367
|
+
for (const [fn, blob] of Object.entries(assets)) {
|
|
368
|
+
console.debug("Writing asset", fn);
|
|
369
|
+
await writeFile(join(distDir, fn), blob);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
317
372
|
|
|
318
|
-
await rmdir(buildDir, { recursive: true });
|
|
319
373
|
|
|
320
|
-
|
|
321
|
-
|
|
374
|
+
async function isPreBuilt(sourceDirPath: string): Promise<boolean> {
|
|
375
|
+
try {
|
|
376
|
+
// A pre-built dependency contains directory “dist”
|
|
377
|
+
// with index.js and possibly other assets.
|
|
378
|
+
// Subdirectories under dist are ignored.
|
|
379
|
+
await readPreBuiltJSBundle(sourceDirPath);
|
|
380
|
+
await readPreBuiltAssets(sourceDirPath);
|
|
381
|
+
return true;
|
|
382
|
+
} catch (e) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
322
386
|
|
|
323
|
-
return defaultExport;
|
|
324
387
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
//throw new Error("TODO");
|
|
337
|
-
//const data = await readFile(outfile, { encoding: 'utf-8' });
|
|
338
|
-
//return new Function(data);
|
|
339
|
-
}();
|
|
388
|
+
async function readPreBuiltDependency(
|
|
389
|
+
sourceDirPath: string,
|
|
390
|
+
): Promise<[
|
|
391
|
+
bundledCode: Uint8Array,
|
|
392
|
+
supportingAssets: Record<string, Uint8Array>,
|
|
393
|
+
]> {
|
|
394
|
+
return [
|
|
395
|
+
await readPreBuiltJSBundle(sourceDirPath),
|
|
396
|
+
await readPreBuiltAssets(sourceDirPath),
|
|
397
|
+
];
|
|
398
|
+
}
|
|
340
399
|
|
|
341
|
-
|
|
400
|
+
|
|
401
|
+
export async function buildDependency(
|
|
402
|
+
/**
|
|
403
|
+
* Acts only as cache key to avoid build directories clashing.
|
|
404
|
+
*/
|
|
405
|
+
moduleRef: string,
|
|
406
|
+
sourceDir: string,
|
|
407
|
+
onProgress: (progress: Progress) => void,
|
|
408
|
+
): Promise<[
|
|
409
|
+
bundledCode: Uint8Array,
|
|
410
|
+
supportingAssets: Record<string, Uint8Array>,
|
|
411
|
+
]> {
|
|
412
|
+
const buildDir = await mkdtemp(join(
|
|
413
|
+
tmpRoot,
|
|
414
|
+
`anafero-dist-${moduleRef.replace(/[^a-z0-9]/gi, '_')}-`,
|
|
415
|
+
));
|
|
416
|
+
|
|
417
|
+
onProgress({ state: `copying into build dir ${buildDir}` });
|
|
418
|
+
|
|
419
|
+
await cp(sourceDir, buildDir, { recursive: true });
|
|
420
|
+
|
|
421
|
+
//const outfile = join(buildDir, 'out.mjs');
|
|
422
|
+
//await esbuildTransform('oi');
|
|
423
|
+
|
|
424
|
+
onProgress({ state: "compiling" });
|
|
425
|
+
|
|
426
|
+
const result = await esbuild({
|
|
427
|
+
//entryPoints: [join(buildDir, 'index.mts')],
|
|
428
|
+
stdin: {
|
|
429
|
+
contents: await readFile(join(buildDir, 'index.mts')),
|
|
430
|
+
loader: 'ts',
|
|
431
|
+
|
|
432
|
+
// TODO: This means we use filesystem when resolving
|
|
433
|
+
// imports in the entry point. That’s not great, if we
|
|
434
|
+
// want to build e.g. in the browser.
|
|
435
|
+
// It may be possible to avoid this by writing a custom
|
|
436
|
+
// resolver plugin for esbuild. See
|
|
437
|
+
// - https://github.com/evanw/esbuild/issues/591#issuecomment-742962090
|
|
438
|
+
// - https://esbuild.github.io/plugins/#on-resolve-arguments
|
|
439
|
+
resolveDir: buildDir,
|
|
440
|
+
|
|
441
|
+
sourcefile: 'index.mts',
|
|
442
|
+
},
|
|
443
|
+
loader: {
|
|
444
|
+
'.mts': 'ts',
|
|
445
|
+
'.css': 'local-css',
|
|
446
|
+
},
|
|
447
|
+
entryNames: '[dir]/[name]',
|
|
448
|
+
assetNames: '[dir]/[name]',
|
|
449
|
+
format: 'esm',
|
|
450
|
+
target: ['es2022'],
|
|
451
|
+
tsconfigRaw: '{}',
|
|
452
|
+
//external: [],
|
|
453
|
+
packages: 'external',
|
|
454
|
+
//plugins: [{
|
|
455
|
+
// name: 'plugin-resolver',
|
|
456
|
+
// setup(build) {
|
|
457
|
+
// build.onLoad({ filter: /^prosemirror-model-metanorma/ }, args => {
|
|
458
|
+
// return import(args.path);
|
|
459
|
+
// });
|
|
460
|
+
// },
|
|
461
|
+
//}],
|
|
462
|
+
minify: false,
|
|
463
|
+
platform: 'browser',
|
|
464
|
+
write: false,
|
|
465
|
+
logLevel: 'silent',
|
|
466
|
+
//logLevel: 'info',
|
|
467
|
+
sourcemap: true,
|
|
468
|
+
bundle: true,
|
|
469
|
+
outfile: 'index.js',
|
|
470
|
+
treeShaking: true,
|
|
471
|
+
//outfile,
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
onProgress({ state: `removing build dir ${buildDir}` });
|
|
475
|
+
await rmdir(buildDir, { recursive: true });
|
|
476
|
+
|
|
477
|
+
const otherFiles: Record<string, Uint8Array> = {};
|
|
478
|
+
|
|
479
|
+
const mainOutput = result.outputFiles.
|
|
480
|
+
find(({ path }) => basename(path) === 'index.js')?.contents;
|
|
481
|
+
|
|
482
|
+
if (!mainOutput) {
|
|
483
|
+
throw new Error("Fetching dependency: no main output after building");
|
|
484
|
+
} else if (result.outputFiles.length > 1) {
|
|
485
|
+
for (const { path, contents } of result.outputFiles) {
|
|
486
|
+
if (!path.endsWith('/index.js')) {
|
|
487
|
+
otherFiles[basename(path)] = contents;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return [mainOutput, otherFiles];
|
|
342
493
|
}
|
|
@@ -42,6 +42,8 @@ import { type VersionBuildConfig, type VersionMeta } from 'anafero/index.mjs';
|
|
|
42
42
|
|
|
43
43
|
import {
|
|
44
44
|
fetchDependency,
|
|
45
|
+
buildDependency,
|
|
46
|
+
writePreBuiltAssets,
|
|
45
47
|
getDependencySources,
|
|
46
48
|
getDependencySupportingFiles,
|
|
47
49
|
} from './dependencies.mjs';
|
|
@@ -57,6 +59,10 @@ console.debug("Package root", PACKAGE_ROOT);
|
|
|
57
59
|
|
|
58
60
|
const decoder = new TextDecoder();
|
|
59
61
|
|
|
62
|
+
const cwd = process.cwd();
|
|
63
|
+
console.debug("Current working directory", cwd);
|
|
64
|
+
|
|
65
|
+
|
|
60
66
|
Effect.
|
|
61
67
|
suspend(() => main(process.argv)).
|
|
62
68
|
pipe(
|
|
@@ -64,13 +70,50 @@ Effect.
|
|
|
64
70
|
NodeRuntime.runMain,
|
|
65
71
|
);
|
|
66
72
|
|
|
73
|
+
|
|
67
74
|
function unpackOption<T>(opt: Option.Option<T>, df?: T): T | undefined {
|
|
68
75
|
return Option.isNone(opt) ? df : opt.value;
|
|
69
76
|
}
|
|
70
77
|
|
|
71
|
-
|
|
78
|
+
|
|
79
|
+
const dispatch = Command.
|
|
80
|
+
make(
|
|
81
|
+
"npx --node-options='--experimental-vm-modules' -y @riboseinc/anafero-cli",
|
|
82
|
+
{},
|
|
83
|
+
() => Effect.log("Pass --help for usage instructions")).
|
|
84
|
+
pipe(
|
|
85
|
+
Command.withDescription("Anafero builder. Use with a subcommand."),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const buildPackage = Command.
|
|
89
|
+
make('build-package', reportingOptions, () => Effect.gen(function * (_) {
|
|
90
|
+
const moduleRef = yield * _(
|
|
91
|
+
Effect.tryPromise(() => readFile(join(cwd, 'package.json'))),
|
|
92
|
+
Effect.flatMap(blob => Effect.try(() => decoder.decode(blob))),
|
|
93
|
+
Effect.flatMap(pkgRaw => Effect.try(() => JSON.parse(pkgRaw))),
|
|
94
|
+
Effect.flatMap(pkg => Effect.succeed(pkg['name'])),
|
|
95
|
+
);
|
|
96
|
+
yield * _(Effect.log(`Building package ${moduleRef}`));
|
|
97
|
+
const [bundledCode, assets] = yield * _(Effect.tryPromise(() => {
|
|
98
|
+
return buildDependency(
|
|
99
|
+
moduleRef,
|
|
100
|
+
cwd,
|
|
101
|
+
(progress) => console.debug(JSON.stringify(progress)),
|
|
102
|
+
);
|
|
103
|
+
}));
|
|
104
|
+
yield * _(Effect.tryPromise(() => writePreBuiltAssets(
|
|
105
|
+
cwd,
|
|
106
|
+
bundledCode,
|
|
107
|
+
assets,
|
|
108
|
+
)));
|
|
109
|
+
})).
|
|
110
|
+
pipe(
|
|
111
|
+
Command.withDescription("For developers: builds adapter in current directory into `./dist`."),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const buildSite = Command.
|
|
72
115
|
make(
|
|
73
|
-
'build',
|
|
116
|
+
'build-site',
|
|
74
117
|
{
|
|
75
118
|
|
|
76
119
|
targetDirectoryPath: Options.directory('target-dir'),
|
|
@@ -174,11 +217,11 @@ const build = Command.
|
|
|
174
217
|
),
|
|
175
218
|
).
|
|
176
219
|
pipe(
|
|
177
|
-
Command.withDescription(
|
|
220
|
+
Command.withDescription("Builds a website using current directory as source."),
|
|
178
221
|
);
|
|
179
222
|
|
|
180
223
|
|
|
181
|
-
const
|
|
224
|
+
const devSite = Command.
|
|
182
225
|
make(
|
|
183
226
|
'develop',
|
|
184
227
|
{
|
|
@@ -198,14 +241,14 @@ const dev = Command.
|
|
|
198
241
|
({ pkg, skipBuild }) =>
|
|
199
242
|
Effect.
|
|
200
243
|
gen(function * (_) {
|
|
201
|
-
const buildCfg = yield * _(
|
|
244
|
+
const buildCfg = yield * _(buildSite);
|
|
202
245
|
|
|
203
246
|
const { targetDirectoryPath } = buildCfg;
|
|
204
247
|
|
|
205
248
|
// Maybe build
|
|
206
249
|
|
|
207
250
|
if (!skipBuild) {
|
|
208
|
-
yield *
|
|
251
|
+
yield * buildSite.handler(buildCfg);
|
|
209
252
|
}
|
|
210
253
|
|
|
211
254
|
|
|
@@ -271,14 +314,17 @@ const dev = Command.
|
|
|
271
314
|
})
|
|
272
315
|
).
|
|
273
316
|
pipe(
|
|
274
|
-
Command.withDescription(
|
|
317
|
+
Command.withDescription("Build site in dev mode (watching for changes & copying client-side JS)."),
|
|
275
318
|
);
|
|
276
319
|
|
|
277
320
|
|
|
278
321
|
|
|
279
|
-
const main =
|
|
322
|
+
const main = dispatch.
|
|
280
323
|
pipe(
|
|
281
|
-
Command.withSubcommands([
|
|
324
|
+
Command.withSubcommands([
|
|
325
|
+
buildSite.pipe(Command.withSubcommands([devSite])),
|
|
326
|
+
buildPackage,
|
|
327
|
+
]),
|
|
282
328
|
Command.run({
|
|
283
329
|
name: "Anafero builder",
|
|
284
330
|
version: "N/A",
|
|
@@ -286,8 +332,8 @@ const main = build.
|
|
|
286
332
|
);
|
|
287
333
|
|
|
288
334
|
|
|
289
|
-
// TODO: Refactor gitdir handling, avoid the
|
|
290
|
-
const gitdir = join(
|
|
335
|
+
// TODO: Refactor gitdir handling, avoid the global
|
|
336
|
+
const gitdir = join(cwd, '.git');
|
|
291
337
|
|
|
292
338
|
async function areWeInGitRepoRoot(): Promise<boolean> {
|
|
293
339
|
const gitRepoStat = await stat(gitdir);
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|