elm-pages 3.0.0-beta.17 → 3.0.0-beta.19

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 (46) hide show
  1. package/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +0 -0
  2. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  5. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  6. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  7. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  8. package/generator/src/basepath-middleware.js +3 -3
  9. package/generator/src/build.js +36 -30
  10. package/generator/src/cli.js +30 -23
  11. package/generator/src/codegen.js +19 -18
  12. package/generator/src/compatibility-key.js +1 -1
  13. package/generator/src/compile-elm.js +20 -22
  14. package/generator/src/config.js +2 -4
  15. package/generator/src/dev-server.js +47 -30
  16. package/generator/src/dir-helpers.js +9 -25
  17. package/generator/src/elm-codegen.js +2 -4
  18. package/generator/src/elm-file-constants.js +2 -3
  19. package/generator/src/error-formatter.js +5 -5
  20. package/generator/src/file-helpers.js +3 -4
  21. package/generator/src/generate-template-module-connector.js +14 -15
  22. package/generator/src/init.js +8 -7
  23. package/generator/src/pre-render-html.js +11 -12
  24. package/generator/src/render-worker.js +21 -26
  25. package/generator/src/render.js +122 -162
  26. package/generator/src/request-cache.js +13 -8
  27. package/generator/src/rewrite-client-elm-json.js +5 -5
  28. package/generator/src/rewrite-elm-json.js +5 -5
  29. package/generator/src/route-codegen-helpers.js +16 -31
  30. package/generator/src/seo-renderer.js +1 -3
  31. package/generator/src/vite-utils.js +1 -2
  32. package/package.json +9 -8
  33. package/src/BackendTask/Custom.elm +1 -1
  34. package/src/BackendTask/File.elm +1 -1
  35. package/src/BackendTask/Glob.elm +4 -2
  36. package/src/BackendTask/Http.elm +6 -6
  37. package/src/FatalError.elm +2 -14
  38. package/src/Form.elm +26 -47
  39. package/src/Pages/Generate.elm +42 -13
  40. package/src/Pages/Internal/Form.elm +14 -1
  41. package/src/Pages/Internal/Platform/Cli.elm +8 -6
  42. package/src/Pages/Internal/Platform/Effect.elm +1 -1
  43. package/src/Pages/Internal/Platform/GeneratorApplication.elm +8 -6
  44. package/src/Pages/Internal/Platform/ToJsPayload.elm +4 -7
  45. package/src/Pages/Script.elm +2 -2
  46. package/src/Server/Request.elm +21 -13
@@ -1,25 +1,21 @@
1
1
  // @ts-check
2
2
 
3
- const path = require("path");
4
- const mm = require("micromatch");
5
- const matter = require("gray-matter");
6
- const globby = require("globby");
7
- const fsPromises = require("fs").promises;
8
- const preRenderHtml = require("./pre-render-html.js");
9
- const { lookupOrPerform } = require("./request-cache.js");
10
- const kleur = require("kleur");
11
- const cookie = require("cookie-signature");
12
- const { compatibilityKey } = require("./compatibility-key.js");
13
- kleur.enabled = true;
3
+ import * as path from "path";
4
+ import { default as mm } from "micromatch";
5
+ import { default as matter } from "gray-matter";
6
+ import { globby } from "globby";
7
+ import * as fsPromises from "fs/promises";
8
+ import * as preRenderHtml from "./pre-render-html.js";
9
+ import { lookupOrPerform } from "./request-cache.js";
10
+ import * as kleur from "kleur/colors";
11
+ import * as cookie from "cookie-signature";
12
+ import { compatibilityKey } from "./compatibility-key.js";
13
+ import * as fs from "fs";
14
14
 
15
15
  process.on("unhandledRejection", (error) => {
16
16
  console.error(error);
17
17
  });
18
18
  let foundErrors;
19
- let pendingBackendTaskResponses = new Map();
20
- let pendingBackendTaskCount;
21
-
22
- module.exports = { render, runGenerator };
23
19
 
24
20
  /**
25
21
  *
@@ -31,7 +27,7 @@ module.exports = { render, runGenerator };
31
27
  * @param {boolean} hasFsAccess
32
28
  * @returns
33
29
  */
34
- async function render(
30
+ export async function render(
35
31
  portsFile,
36
32
  basePath,
37
33
  elmModule,
@@ -41,14 +37,12 @@ async function render(
41
37
  addBackendTaskWatcher,
42
38
  hasFsAccess
43
39
  ) {
44
- const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(hasFsAccess);
45
- resetInMemoryFs();
40
+ // const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(hasFsAccess);
41
+ // resetInMemoryFs();
46
42
  foundErrors = false;
47
- pendingBackendTaskResponses = new Map();
48
- pendingBackendTaskCount = 0;
49
43
  // since init/update are never called in pre-renders, and BackendTask.Http is called using pure NodeJS HTTP fetching
50
44
  // we can provide a fake HTTP instead of xhr2 (which is otherwise needed for Elm HTTP requests from Node)
51
- XMLHttpRequest = {};
45
+ global.XMLHttpRequest = {};
52
46
  const result = await runElmApp(
53
47
  portsFile,
54
48
  basePath,
@@ -57,7 +51,6 @@ async function render(
57
51
  path,
58
52
  request,
59
53
  addBackendTaskWatcher,
60
- fs,
61
54
  hasFsAccess
62
55
  );
63
56
  return result;
@@ -68,25 +61,29 @@ async function render(
68
61
  * @returns
69
62
  * @param {string[]} cliOptions
70
63
  * @param {any} portsFile
64
+ * @param {string} scriptModuleName
71
65
  */
72
- async function runGenerator(cliOptions, portsFile, elmModule) {
66
+ export async function runGenerator(
67
+ cliOptions,
68
+ portsFile,
69
+ elmModule,
70
+ scriptModuleName
71
+ ) {
73
72
  global.isRunningGenerator = true;
74
- const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(true);
75
- resetInMemoryFs();
73
+ // const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(true);
74
+ // resetInMemoryFs();
76
75
  foundErrors = false;
77
- pendingBackendTaskResponses = new Map();
78
- pendingBackendTaskCount = 0;
79
76
  // since init/update are never called in pre-renders, and BackendTask.Http is called using pure NodeJS HTTP fetching
80
77
  // we can provide a fake HTTP instead of xhr2 (which is otherwise needed for Elm HTTP requests from Node)
81
- XMLHttpRequest = {};
78
+ global.XMLHttpRequest = {};
82
79
  const result = await runGeneratorAppHelp(
83
80
  cliOptions,
84
81
  portsFile,
85
82
  "",
86
83
  elmModule,
84
+ scriptModuleName,
87
85
  "production",
88
86
  "",
89
- fs,
90
87
  true
91
88
  );
92
89
  return result;
@@ -101,15 +98,16 @@ async function runGenerator(cliOptions, portsFile, elmModule) {
101
98
  * @param {any} portsFile
102
99
  * @param {typeof import("fs") | import("memfs").IFs} fs
103
100
  * @param {boolean} hasFsAccess
101
+ * @param {string} scriptModuleName
104
102
  */
105
103
  function runGeneratorAppHelp(
106
104
  cliOptions,
107
105
  portsFile,
108
106
  basePath,
109
107
  elmModule,
108
+ scriptModuleName,
110
109
  mode,
111
110
  pagePath,
112
- fs,
113
111
  hasFsAccess
114
112
  ) {
115
113
  const isDevServer = mode !== "build";
@@ -122,7 +120,7 @@ function runGeneratorAppHelp(
122
120
  app = elmModule.Elm.Main.init({
123
121
  flags: {
124
122
  compatibilityKey,
125
- argv: ["", "", ...cliOptions],
123
+ argv: ["", `elm-pages run ${scriptModuleName}`, ...cliOptions],
126
124
  versionMessage: "1.2.3",
127
125
  },
128
126
  });
@@ -166,33 +164,37 @@ function runGeneratorAppHelp(
166
164
  );
167
165
  }
168
166
  } else if (fromElm.tag === "DoHttp") {
169
- const requestHash = fromElm.args[0];
170
- const requestToPerform = fromElm.args[1];
171
- if (
172
- requestToPerform.url !== "elm-pages-internal://port" &&
173
- requestToPerform.url.startsWith("elm-pages-internal://")
174
- ) {
175
- runInternalJob(
176
- requestHash,
177
- app,
178
- mode,
179
- requestToPerform,
180
- fs,
181
- hasFsAccess,
182
- patternsToWatch
183
- );
184
- } else {
185
- runHttpJob(
186
- requestHash,
187
- portsFile,
188
- app,
189
- mode,
190
- requestToPerform,
191
- fs,
192
- hasFsAccess,
193
- fromElm.args[1]
194
- );
195
- }
167
+ app.ports.gotBatchSub.send(
168
+ Object.fromEntries(
169
+ await Promise.all(
170
+ fromElm.args[0].map(([requestHash, requestToPerform]) => {
171
+ if (
172
+ requestToPerform.url !== "elm-pages-internal://port" &&
173
+ requestToPerform.url.startsWith("elm-pages-internal://")
174
+ ) {
175
+ return runInternalJob(
176
+ requestHash,
177
+ app,
178
+ mode,
179
+ requestToPerform,
180
+ hasFsAccess,
181
+ patternsToWatch
182
+ );
183
+ } else {
184
+ return runHttpJob(
185
+ requestHash,
186
+ portsFile,
187
+ app,
188
+ mode,
189
+ requestToPerform,
190
+ hasFsAccess,
191
+ requestToPerform
192
+ );
193
+ }
194
+ })
195
+ )
196
+ )
197
+ );
196
198
  } else if (fromElm.tag === "Errors") {
197
199
  foundErrors = true;
198
200
  reject(fromElm.args[0].errorsJson);
@@ -227,7 +229,6 @@ function runElmApp(
227
229
  pagePath,
228
230
  request,
229
231
  addBackendTaskWatcher,
230
- fs,
231
232
  hasFsAccess
232
233
  ) {
233
234
  const isDevServer = mode !== "build";
@@ -241,7 +242,6 @@ function runElmApp(
241
242
  .replace(/content\.dat\/?$/, "");
242
243
 
243
244
  const modifiedRequest = { ...request, path: route };
244
- // console.log("StaticHttp cache keys", Object.keys(global.staticHttpCache));
245
245
  app = elmModule.Elm.Main.init({
246
246
  flags: {
247
247
  mode,
@@ -275,9 +275,6 @@ function runElmApp(
275
275
  console.log(fromElm.value);
276
276
  } else if (fromElm.tag === "ApiResponse") {
277
277
  const args = fromElm.args[0];
278
- if (mode === "build") {
279
- global.staticHttpCache = args.staticHttpCache;
280
- }
281
278
 
282
279
  resolve({
283
280
  kind: "api-response",
@@ -287,10 +284,6 @@ function runElmApp(
287
284
  });
288
285
  } else if (fromElm.tag === "PageProgress") {
289
286
  const args = fromElm.args[0];
290
- if (mode === "build") {
291
- global.staticHttpCache = args.staticHttpCache;
292
- }
293
-
294
287
  if (isBytes) {
295
288
  resolve({
296
289
  kind: "bytes",
@@ -309,33 +302,37 @@ function runElmApp(
309
302
  );
310
303
  }
311
304
  } else if (fromElm.tag === "DoHttp") {
312
- const requestHash = fromElm.args[0];
313
- const requestToPerform = fromElm.args[1];
314
- if (
315
- requestToPerform.url !== "elm-pages-internal://port" &&
316
- requestToPerform.url.startsWith("elm-pages-internal://")
317
- ) {
318
- runInternalJob(
319
- requestHash,
320
- app,
321
- mode,
322
- requestToPerform,
323
- fs,
324
- hasFsAccess,
325
- patternsToWatch
326
- );
327
- } else {
328
- runHttpJob(
329
- requestHash,
330
- portsFile,
331
- app,
332
- mode,
333
- requestToPerform,
334
- fs,
335
- hasFsAccess,
336
- fromElm.args[1]
337
- );
338
- }
305
+ app.ports.gotBatchSub.send(
306
+ Object.fromEntries(
307
+ await Promise.all(
308
+ fromElm.args[0].map(([requestHash, requestToPerform]) => {
309
+ if (
310
+ requestToPerform.url !== "elm-pages-internal://port" &&
311
+ requestToPerform.url.startsWith("elm-pages-internal://")
312
+ ) {
313
+ return runInternalJob(
314
+ requestHash,
315
+ app,
316
+ mode,
317
+ requestToPerform,
318
+ hasFsAccess,
319
+ patternsToWatch
320
+ );
321
+ } else {
322
+ return runHttpJob(
323
+ requestHash,
324
+ portsFile,
325
+ app,
326
+ mode,
327
+ requestToPerform,
328
+ hasFsAccess,
329
+ requestToPerform
330
+ );
331
+ }
332
+ })
333
+ )
334
+ )
335
+ );
339
336
  } else if (fromElm.tag === "Errors") {
340
337
  foundErrors = true;
341
338
  reject(fromElm.args[0].errorsJson);
@@ -400,11 +397,9 @@ async function runHttpJob(
400
397
  app,
401
398
  mode,
402
399
  requestToPerform,
403
- fs,
404
400
  hasFsAccess,
405
401
  useCache
406
402
  ) {
407
- pendingBackendTaskCount += 1;
408
403
  try {
409
404
  const lookupResponse = await lookupOrPerform(
410
405
  portsFile,
@@ -416,25 +411,29 @@ async function runHttpJob(
416
411
 
417
412
  if (lookupResponse.kind === "cache-response-path") {
418
413
  const responseFilePath = lookupResponse.value;
419
- pendingBackendTaskResponses.set(requestHash, {
420
- request: requestToPerform,
421
- response: JSON.parse(
422
- (await fs.promises.readFile(responseFilePath, "utf8")).toString()
423
- ),
424
- });
414
+ return [
415
+ requestHash,
416
+ {
417
+ request: requestToPerform,
418
+ response: JSON.parse(
419
+ (await fs.promises.readFile(responseFilePath, "utf8")).toString()
420
+ ),
421
+ },
422
+ ];
425
423
  } else if (lookupResponse.kind === "response-json") {
426
- pendingBackendTaskResponses.set(requestHash, {
427
- request: requestToPerform,
428
- response: lookupResponse.value,
429
- });
424
+ return [
425
+ requestHash,
426
+ {
427
+ request: requestToPerform,
428
+ response: lookupResponse.value,
429
+ },
430
+ ];
430
431
  } else {
431
432
  throw `Unexpected kind ${lookupResponse}`;
432
433
  }
433
434
  } catch (error) {
434
- sendError(app, error);
435
- } finally {
436
- pendingBackendTaskCount -= 1;
437
- flushIfDone(app);
435
+ console.log("@@@ERROR", error);
436
+ // sendError(app, error);
438
437
  }
439
438
  }
440
439
 
@@ -456,48 +455,33 @@ async function runInternalJob(
456
455
  app,
457
456
  mode,
458
457
  requestToPerform,
459
- fs,
460
458
  hasFsAccess,
461
459
  patternsToWatch
462
460
  ) {
463
461
  try {
464
- pendingBackendTaskCount += 1;
465
-
466
462
  if (requestToPerform.url === "elm-pages-internal://log") {
467
- pendingBackendTaskResponses.set(
468
- requestHash,
469
- await runLogJob(requestToPerform)
470
- );
463
+ return [requestHash, await runLogJob(requestToPerform)];
471
464
  } else if (requestToPerform.url === "elm-pages-internal://read-file") {
472
- pendingBackendTaskResponses.set(
465
+ return [
473
466
  requestHash,
474
- await readFileJobNew(requestToPerform, patternsToWatch)
475
- );
467
+ await readFileJobNew(requestToPerform, patternsToWatch),
468
+ ];
476
469
  } else if (requestToPerform.url === "elm-pages-internal://glob") {
477
- pendingBackendTaskResponses.set(
478
- requestHash,
479
- await runGlobNew(requestToPerform, patternsToWatch)
480
- );
470
+ return [requestHash, await runGlobNew(requestToPerform, patternsToWatch)];
481
471
  } else if (requestToPerform.url === "elm-pages-internal://env") {
482
- pendingBackendTaskResponses.set(
483
- requestHash,
484
- await runEnvJob(requestToPerform, patternsToWatch)
485
- );
472
+ return [requestHash, await runEnvJob(requestToPerform, patternsToWatch)];
486
473
  } else if (requestToPerform.url === "elm-pages-internal://encrypt") {
487
- pendingBackendTaskResponses.set(
474
+ return [
488
475
  requestHash,
489
- await runEncryptJob(requestToPerform, patternsToWatch)
490
- );
476
+ await runEncryptJob(requestToPerform, patternsToWatch),
477
+ ];
491
478
  } else if (requestToPerform.url === "elm-pages-internal://decrypt") {
492
- pendingBackendTaskResponses.set(
479
+ return [
493
480
  requestHash,
494
- await runDecryptJob(requestToPerform, patternsToWatch)
495
- );
481
+ await runDecryptJob(requestToPerform, patternsToWatch),
482
+ ];
496
483
  } else if (requestToPerform.url === "elm-pages-internal://write-file") {
497
- pendingBackendTaskResponses.set(
498
- requestHash,
499
- await runWriteFileJob(requestToPerform)
500
- );
484
+ return [requestHash, await runWriteFileJob(requestToPerform)];
501
485
  } else {
502
486
  throw `Unexpected internal BackendTask request format: ${kleur.yellow(
503
487
  JSON.stringify(2, null, requestToPerform)
@@ -505,9 +489,6 @@ async function runInternalJob(
505
489
  }
506
490
  } catch (error) {
507
491
  sendError(app, error);
508
- } finally {
509
- pendingBackendTaskCount -= 1;
510
- flushIfDone(app);
511
492
  }
512
493
  }
513
494
 
@@ -628,27 +609,6 @@ async function runDecryptJob(req, patternsToWatch) {
628
609
  }
629
610
  }
630
611
 
631
- function flushIfDone(app) {
632
- if (foundErrors) {
633
- pendingBackendTaskResponses = new Map();
634
- } else if (pendingBackendTaskCount === 0) {
635
- // console.log(
636
- // `Flushing ${pendingBackendTaskResponses.length} items in ${timeUntilThreshold}ms`
637
- // );
638
-
639
- flushQueue(app);
640
- }
641
- }
642
-
643
- function flushQueue(app) {
644
- // TODO - could the case where flush is called with size 0 be avoided on the Elm side?
645
- // if (pendingBackendTaskResponses.size > 0) {
646
- // console.log("@@@ FLUSHING", pendingBackendTaskResponses.size);
647
- app.ports.gotBatchSub.send(Object.fromEntries(pendingBackendTaskResponses));
648
- pendingBackendTaskResponses = new Map();
649
- // }
650
- }
651
-
652
612
  /**
653
613
  * @param {{ ports: { fromJsPort: { send: (arg0: { tag: string; data: any; }) => void; }; }; }} app
654
614
  * @param {{ message: string; title: string; }} error
@@ -1,6 +1,7 @@
1
- const path = require("path");
2
- const kleur = require("kleur");
3
- const fsPromises = require("fs/promises");
1
+ import * as path from "path";
2
+ import * as fsPromises from "fs/promises";
3
+ import * as kleur from "kleur/colors";
4
+ import { default as makeFetchHappenOriginal } from "make-fetch-happen";
4
5
 
5
6
  const defaultHttpCachePath = "./.elm-pages/http-cache";
6
7
 
@@ -13,8 +14,14 @@ const defaultHttpCachePath = "./.elm-pages/http-cache";
13
14
  * @param {boolean} hasFsAccess
14
15
  * @returns {Promise<Response>}
15
16
  */
16
- function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess, useCache) {
17
- const makeFetchHappen = require("make-fetch-happen").defaults({
17
+ export function lookupOrPerform(
18
+ portsFile,
19
+ mode,
20
+ rawRequest,
21
+ hasFsAccess,
22
+ useCache
23
+ ) {
24
+ const makeFetchHappen = makeFetchHappenOriginal.defaults({
18
25
  cache: mode === "build" ? "no-cache" : "default",
19
26
  });
20
27
  return new Promise(async (resolve, reject) => {
@@ -29,7 +36,7 @@ function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess, useCache) {
29
36
  const portBackendTaskPath = path.resolve(portsFile);
30
37
  // On Windows, we need cannot use paths directly and instead must use a file:// URL.
31
38
  // portBackendTask = await require(url.pathToFileURL(portBackendTaskPath).href);
32
- portBackendTask = require(portBackendTaskPath);
39
+ portBackendTask = await import(portBackendTaskPath);
33
40
  } catch (e) {
34
41
  portBackendTaskImportError = e;
35
42
  }
@@ -270,5 +277,3 @@ async function canAccess(filePath) {
270
277
  return false;
271
278
  }
272
279
  }
273
-
274
- module.exports = { lookupOrPerform };
@@ -1,6 +1,6 @@
1
- const fs = require("fs");
1
+ import * as fs from "fs";
2
2
 
3
- module.exports = async function () {
3
+ export async function rewriteClientElmJson() {
4
4
  var elmJson = JSON.parse(
5
5
  (await fs.promises.readFile("./elm.json")).toString()
6
6
  );
@@ -9,11 +9,11 @@ module.exports = async function () {
9
9
 
10
10
  await writeFileIfChanged(
11
11
  "./elm-stuff/elm-pages/client/elm.json",
12
- JSON.stringify(rewriteElmJson(elmJson))
12
+ JSON.stringify(rewriteClientElmJsonHelp(elmJson))
13
13
  );
14
- };
14
+ }
15
15
 
16
- function rewriteElmJson(elmJson) {
16
+ function rewriteClientElmJsonHelp(elmJson) {
17
17
  // The internal generated file will be at:
18
18
  // ./elm-stuff/elm-pages/
19
19
  // So, we need to take the existing elmJson and
@@ -1,6 +1,6 @@
1
- const fs = require("fs");
1
+ import * as fs from "fs";
2
2
 
3
- module.exports = async function (sourceElmJsonPath, targetElmJsonPath) {
3
+ export async function rewriteElmJson(sourceElmJsonPath, targetElmJsonPath) {
4
4
  var elmJson = JSON.parse(
5
5
  (await fs.promises.readFile(sourceElmJsonPath)).toString()
6
6
  );
@@ -9,11 +9,11 @@ module.exports = async function (sourceElmJsonPath, targetElmJsonPath) {
9
9
 
10
10
  await writeFileIfChanged(
11
11
  targetElmJsonPath,
12
- JSON.stringify(rewriteElmJson(elmJson))
12
+ JSON.stringify(rewriteElmJsonHelp(elmJson))
13
13
  );
14
- };
14
+ }
15
15
 
16
- function rewriteElmJson(elmJson) {
16
+ function rewriteElmJsonHelp(elmJson) {
17
17
  // The internal generated file will be at:
18
18
  // ./elm-stuff/elm-pages/
19
19
  // So, we need to take the existing elmJson and