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
@@ -75,7 +75,7 @@ console.elmlog = (str) => logs.push(str + "\n");
75
75
  const { Elm } = require("./Runner.elm.js");
76
76
 
77
77
  // Start the Elm app
78
- const flags = { initialSeed: 4015861069, fuzzRuns: 100, filter: null };
78
+ const flags = { initialSeed: 3718366887, fuzzRuns: 100, filter: null };
79
79
  const app = Elm.Runner.init({ flags: flags });
80
80
 
81
81
  // Record the timing at which we received the last "runTest" message
@@ -82,7 +82,7 @@ const verbosity = 0;
82
82
  // Create a long lived reporter worker
83
83
  const { Elm } = require("./Reporter.elm.js");
84
84
  const flags = {
85
- initialSeed: 4015861069,
85
+ initialSeed: 3718366887,
86
86
  fuzzRuns: 100,
87
87
  mode: "consoleNoColor",
88
88
  verbosity: verbosity,
@@ -75,7 +75,7 @@ console.elmlog = (str) => logs.push(str + "\n");
75
75
  const { Elm } = require("./Runner.elm.js");
76
76
 
77
77
  // Start the Elm app
78
- const flags = { initialSeed: 1374716823, fuzzRuns: 100, filter: null };
78
+ const flags = { initialSeed: 1328864607, fuzzRuns: 100, filter: null };
79
79
  const app = Elm.Runner.init({ flags: flags });
80
80
 
81
81
  // Record the timing at which we received the last "runTest" message
@@ -82,7 +82,7 @@ const verbosity = 0;
82
82
  // Create a long lived reporter worker
83
83
  const { Elm } = require("./Reporter.elm.js");
84
84
  const flags = {
85
- initialSeed: 1374716823,
85
+ initialSeed: 1328864607,
86
86
  fuzzRuns: 100,
87
87
  mode: "consoleNoColor",
88
88
  verbosity: verbosity,
@@ -1,6 +1,6 @@
1
1
  // this middleware is only active when (config.base !== '/')
2
2
 
3
- module.exports = function baseMiddleware(base) {
3
+ export function baseMiddleware(base) {
4
4
  // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
5
5
  return function viteBaseMiddleware(req, res, next) {
6
6
  // `req.url` only contains the path, since that is what gets passed in the HTTP request.
@@ -34,11 +34,11 @@ module.exports = function baseMiddleware(base) {
34
34
  const suggestionUrl = `${base}/${path.slice(1)}`;
35
35
  res.end(
36
36
  `The server is configured with a public base URL of ${base} - ` +
37
- `did you mean to visit ${suggestionUrl} instead?`
37
+ `did you mean to visit ${suggestionUrl} instead?`
38
38
  );
39
39
  return;
40
40
  }
41
41
 
42
42
  next();
43
43
  };
44
- };
44
+ }
@@ -1,24 +1,25 @@
1
- const fs = require("./dir-helpers.js");
2
- const fsPromises = require("fs").promises;
3
-
4
- const { runElmReview } = require("./compile-elm.js");
5
- const { restoreColorSafe } = require("./error-formatter");
6
- const path = require("path");
7
- const spawnCallback = require("cross-spawn").spawn;
8
- const codegen = require("./codegen.js");
9
- const terser = require("terser");
10
- const os = require("os");
11
- const { Worker, SHARE_ENV } = require("worker_threads");
12
- const { ensureDirSync } = require("./file-helpers.js");
13
- const { generateClientFolder } = require("./codegen.js");
14
- const which = require("which");
15
- const { build } = require("vite");
16
- const preRenderHtml = require("./pre-render-html.js");
17
- const esbuild = require("esbuild");
18
- const { createHash } = require("crypto");
19
- const { merge_vite_configs } = require("./vite-utils.js");
20
- const { resolveConfig } = require("./config.js");
21
- const globby = require("globby");
1
+ import * as fs from "./dir-helpers.js";
2
+ import * as fsPromises from "fs/promises";
3
+ import { runElmReview } from "./compile-elm.js";
4
+ import { restoreColorSafe } from "./error-formatter.js";
5
+ import * as path from "path";
6
+ import { spawn as spawnCallback } from "cross-spawn";
7
+ import * as codegen from "./codegen.js";
8
+ import * as terser from "terser";
9
+ import * as os from "os";
10
+ import { Worker, SHARE_ENV } from "worker_threads";
11
+ import { ensureDirSync } from "./file-helpers.js";
12
+ import { generateClientFolder } from "./codegen.js";
13
+ import { default as which } from "which";
14
+ import { build } from "vite";
15
+ import * as preRenderHtml from "./pre-render-html.js";
16
+ import * as esbuild from "esbuild";
17
+ import { createHash } from "crypto";
18
+ import { merge_vite_configs } from "./vite-utils.js";
19
+ import { resolveConfig } from "./config.js";
20
+ import { globbySync } from "globby";
21
+ import { fileURLToPath } from "url";
22
+ import { copyFile } from "fs/promises";
22
23
 
23
24
  let pool = [];
24
25
  let pagesReady;
@@ -65,7 +66,7 @@ async function ensureRequiredExecutables() {
65
66
  }
66
67
  }
67
68
 
68
- async function run(options) {
69
+ export async function run(options) {
69
70
  try {
70
71
  await ensureRequiredDirs();
71
72
  await ensureRequiredExecutables();
@@ -110,7 +111,11 @@ async function run(options) {
110
111
  withoutExtension
111
112
  );
112
113
  const assetManifestPath = path.join(process.cwd(), "dist/manifest.json");
113
- const manifest = require(assetManifestPath);
114
+ const manifest = (
115
+ await import(assetManifestPath, {
116
+ assert: { type: "json" },
117
+ })
118
+ ).default;
114
119
  const indexTemplate = await fsPromises.readFile(
115
120
  "dist/elm-stuff/elm-pages/index.html",
116
121
  "utf-8"
@@ -161,7 +166,7 @@ async function run(options) {
161
166
  })
162
167
  .catch((error) => {
163
168
  const portBackendTaskFileFound =
164
- globby.sync("./custom-backend-task.*").length > 0;
169
+ globbySync("./custom-backend-task.*").length > 0;
165
170
  if (portBackendTaskFileFound) {
166
171
  // don't present error if there are no files matching custom-backend-task
167
172
  // if there are files matching custom-backend-task, warn the user in case something went wrong loading it
@@ -170,7 +175,7 @@ async function run(options) {
170
175
  });
171
176
  // TODO extract common code for compiling ports file?
172
177
 
173
- XMLHttpRequest = {};
178
+ global.XMLHttpRequest = {};
174
179
  const compileCli = compileCliApp(options);
175
180
  try {
176
181
  await compileCli;
@@ -207,6 +212,7 @@ async function run(options) {
207
212
  processedIndexTemplate
208
213
  );
209
214
  } catch (error) {
215
+ console.trace(error);
210
216
  if (error) {
211
217
  console.error(restoreColorSafe(error));
212
218
  }
@@ -220,6 +226,8 @@ async function run(options) {
220
226
  */
221
227
  function initWorker(basePath, whenDone) {
222
228
  return new Promise((resolve, reject) => {
229
+ const __filename = fileURLToPath(import.meta.url);
230
+ const __dirname = path.dirname(__filename);
223
231
  activeWorkers += 1;
224
232
  let newWorker = {
225
233
  worker: new Worker(path.join(__dirname, "./render-worker.js"), {
@@ -369,7 +377,7 @@ function elmOptimizeLevel2(outputPath, cwd) {
369
377
 
370
378
  subprocess.on("close", async (code) => {
371
379
  if (code === 0) {
372
- await fs.copyFile(optimizedOutputPath, outputPath);
380
+ await copyFile(optimizedOutputPath, outputPath);
373
381
  resolve();
374
382
  } else {
375
383
  if (!buildError) {
@@ -539,7 +547,7 @@ async function runTerser(filePath) {
539
547
  }
540
548
  }
541
549
 
542
- async function compileCliApp(options) {
550
+ export async function compileCliApp(options) {
543
551
  await spawnElmMake(
544
552
  // TODO should be --optimize, but there seems to be an issue with the html to JSON with --optimize
545
553
  options.debug ? "debug" : "optimize",
@@ -595,7 +603,7 @@ function _HtmlAsJson_toJson(html) {
595
603
  `;
596
604
 
597
605
  await fsPromises.writeFile(
598
- ELM_FILE_PATH(),
606
+ ELM_FILE_PATH().replace(/\.js$/, ".cjs"),
599
607
  elmFileContent
600
608
  .replace(
601
609
  /return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
@@ -671,5 +679,3 @@ function defaultPreloadForFile(file) {
671
679
  * @param {string} contentJsonString
672
680
  * @returns {string}
673
681
  */
674
-
675
- module.exports = { run, compileCliApp };
@@ -1,25 +1,27 @@
1
1
  #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ import * as build from "./build.js";
5
+ import * as dev from "./dev-server.js";
6
+ import * as init from "./init.js";
7
+ import * as codegen from "./codegen.js";
8
+ import * as fs from "fs";
9
+ import * as path from "path";
10
+ import { restoreColorSafe } from "./error-formatter.js";
11
+ import * as renderer from "./render.js";
12
+ import { globbySync } from "globby";
13
+ import * as esbuild from "esbuild";
14
+ import { rewriteElmJson } from "./rewrite-elm-json.js";
15
+ import { ensureDirSync, deleteIfExists } from "./file-helpers.js";
16
+
17
+ import * as commander from "commander";
18
+ import { runElmCodegenInstall } from "./elm-codegen.js";
2
19
 
3
- const build = require("./build.js");
4
- const dirHelpers = require("./dir-helpers.js");
5
- const dev = require("./dev-server.js");
6
- const init = require("./init.js");
7
- const codegen = require("./codegen.js");
8
- const fs = require("fs");
9
- const path = require("path");
10
- const { restoreColorSafe } = require("./error-formatter");
11
- const renderer = require("../../generator/src/render");
12
- const globby = require("globby");
13
- const esbuild = require("esbuild");
14
- const copyModifiedElmJson = require("./rewrite-elm-json.js");
15
- const { ensureDirSync, deleteIfExists } = require("./file-helpers.js");
16
-
17
- const commander = require("commander");
18
- const { runElmCodegenInstall } = require("./elm-codegen.js");
19
20
  const Argument = commander.Argument;
20
21
  const Option = commander.Option;
21
22
 
22
- const packageVersion = require("../../package.json").version;
23
+ import * as packageJson from "../../package.json" assert { type: "json" };
24
+ const packageVersion = packageJson.version;
23
25
 
24
26
  async function main() {
25
27
  const program = new commander.Command();
@@ -124,7 +126,7 @@ async function main() {
124
126
  path.join("./script/elm-stuff/elm-pages/.elm-pages/Main.elm"),
125
127
  generatorWrapperFile(moduleName)
126
128
  );
127
- await copyModifiedElmJson(
129
+ await rewriteElmJson(
128
130
  "./script/elm.json",
129
131
  "./script/elm-stuff/elm-pages/elm.json"
130
132
  );
@@ -151,7 +153,7 @@ async function main() {
151
153
  })
152
154
  .catch((error) => {
153
155
  const portBackendTaskFileFound =
154
- globby.sync("./custom-backend-task.*").length > 0;
156
+ globbySync("./custom-backend-task.*").length > 0;
155
157
  if (portBackendTaskFileFound) {
156
158
  // don't present error if there are no files matching custom-backend-task
157
159
  // if there are files matching custom-backend-task, warn the user in case something went wrong loading it
@@ -169,10 +171,15 @@ async function main() {
169
171
  // TODO have option for compiling with --debug or not (maybe allow running with elm-optimize-level-2 as well?)
170
172
  await build.compileCliApp({ debug: "debug" });
171
173
  process.chdir("../");
174
+ fs.renameSync(
175
+ "./script/elm-stuff/elm-pages/elm.js",
176
+ "./script/elm-stuff/elm-pages/elm.cjs"
177
+ );
172
178
  await renderer.runGenerator(
173
179
  unprocessedCliOptions,
174
180
  resolvedPortsPath,
175
- requireElm("./script/elm-stuff/elm-pages/elm.js")
181
+ await requireElm("./script/elm-stuff/elm-pages/elm.cjs"),
182
+ moduleName
176
183
  );
177
184
  } catch (error) {
178
185
  console.log(restoreColorSafe(error));
@@ -186,7 +193,7 @@ async function main() {
186
193
  .option("--port <number>", "serve site at localhost:<port>", "8000")
187
194
  .action(async (options) => {
188
195
  await codegen.generate("/");
189
- const DocServer = require("elm-doc-preview");
196
+ const DocServer = (await import("elm-doc-preview")).default;
190
197
  const server = new DocServer({
191
198
  port: options.port,
192
199
  browser: true,
@@ -256,11 +263,11 @@ function normalizeUrl(rawPagePath) {
256
263
  /**
257
264
  * @param {string} compiledElmPath
258
265
  */
259
- function requireElm(compiledElmPath) {
266
+ async function requireElm(compiledElmPath) {
260
267
  const warnOriginal = console.warn;
261
268
  console.warn = function () {};
262
269
 
263
- Elm = require(path.resolve(compiledElmPath));
270
+ let Elm = (await import(path.resolve(compiledElmPath))).default;
264
271
  console.warn = warnOriginal;
265
272
  return Elm;
266
273
  }
@@ -1,21 +1,24 @@
1
- const fs = require("fs");
2
- const fsExtra = require("fs-extra");
3
- const copyModifiedElmJson = require("./rewrite-elm-json.js");
4
- const copyModifiedElmJsonClient = require("./rewrite-client-elm-json.js");
5
- const { elmPagesCliFile, elmPagesUiFile } = require("./elm-file-constants.js");
6
- const spawnCallback = require("cross-spawn").spawn;
7
- const which = require("which");
8
- const {
9
- generateTemplateModuleConnector,
10
- } = require("./generate-template-module-connector.js");
11
- const path = require("path");
12
- const { ensureDirSync, deleteIfExists } = require("./file-helpers.js");
1
+ import * as fs from "fs";
2
+ import * as fsExtra from "fs-extra/esm";
3
+ import { rewriteElmJson } from "./rewrite-elm-json.js";
4
+ import { rewriteClientElmJson } from "./rewrite-client-elm-json.js";
5
+ import { elmPagesCliFile, elmPagesUiFile } from "./elm-file-constants.js";
6
+ import { spawn as spawnCallback } from "cross-spawn";
7
+ import { default as which } from "which";
8
+ import { generateTemplateModuleConnector } from "./generate-template-module-connector.js";
9
+
10
+ import * as path from "path";
11
+ import { ensureDirSync, deleteIfExists } from "./file-helpers.js";
12
+ import { fileURLToPath } from "url";
13
13
  global.builtAt = new Date();
14
14
 
15
+ const __filename = fileURLToPath(import.meta.url);
16
+ const __dirname = path.dirname(__filename);
17
+
15
18
  /**
16
19
  * @param {string} basePath
17
20
  */
18
- async function generate(basePath) {
21
+ export async function generate(basePath) {
19
22
  const cliCode = await generateTemplateModuleConnector(basePath, "cli");
20
23
  const browserCode = await generateTemplateModuleConnector(
21
24
  basePath,
@@ -63,7 +66,7 @@ async function generate(basePath) {
63
66
  browserCode.fetcherModules
64
67
  ),
65
68
  // write modified elm.json to elm-stuff/elm-pages/
66
- copyModifiedElmJson("./elm.json", "./elm-stuff/elm-pages/elm.json"),
69
+ rewriteElmJson("./elm.json", "./elm-stuff/elm-pages/elm.json"),
67
70
  // ...(await listFiles("./Pages/Internal")).map(copyToBoth),
68
71
  ]);
69
72
  }
@@ -85,7 +88,7 @@ async function newCopyBoth(modulePath) {
85
88
  );
86
89
  }
87
90
 
88
- async function generateClientFolder(basePath) {
91
+ export async function generateClientFolder(basePath) {
89
92
  const browserCode = await generateTemplateModuleConnector(
90
93
  basePath,
91
94
  "browser"
@@ -97,7 +100,7 @@ async function generateClientFolder(basePath) {
97
100
  await newCopyBoth("SharedTemplate.elm");
98
101
  await newCopyBoth("SiteConfig.elm");
99
102
 
100
- await copyModifiedElmJsonClient();
103
+ await rewriteClientElmJson();
101
104
  await fsExtra.copy("./app", "./elm-stuff/elm-pages/client/app", {
102
105
  recursive: true,
103
106
  });
@@ -230,5 +233,3 @@ async function listFiles(dir) {
230
233
  function merge(arrays) {
231
234
  return [].concat.apply([], arrays);
232
235
  }
233
-
234
- module.exports = { generate, generateClientFolder };
@@ -1 +1 @@
1
- module.exports = { compatibilityKey: 6 };
1
+ export const compatibilityKey = 6;
@@ -1,17 +1,21 @@
1
- const spawnCallback = require("cross-spawn").spawn;
2
- const fs = require("fs");
3
- const fsHelpers = require("./dir-helpers.js");
4
- const fsPromises = require("fs").promises;
5
- const path = require("path");
6
- const kleur = require("kleur");
7
- const { inject } = require("elm-hot");
8
- const pathToClientElm = path.join(
9
- process.cwd(),
10
- "elm-stuff/elm-pages/",
11
- "browser-elm.js"
12
- );
1
+ import { spawn as spawnCallback } from "cross-spawn";
2
+ import * as fs from "fs";
3
+ import * as fsHelpers from "./dir-helpers.js";
4
+ import * as fsPromises from "fs/promises";
5
+ import * as path from "path";
6
+ import * as kleur from "kleur/colors";
7
+ import { inject } from "elm-hot";
8
+ import { fileURLToPath } from "url";
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = path.dirname(__filename);
13
11
 
14
- async function compileElmForBrowser(options) {
12
+ export async function compileElmForBrowser(options) {
13
+ // TODO do I need to make sure this is run from the right cwd? Before it was run outside of this function in the global scope, need to make sure that doesn't change semantics.
14
+ const pathToClientElm = path.join(
15
+ process.cwd(),
16
+ "elm-stuff/elm-pages/",
17
+ "browser-elm.js"
18
+ );
15
19
  await runElm(options, "./.elm-pages/Main.elm", pathToClientElm);
16
20
  return fs.promises.writeFile(
17
21
  "./.elm-pages/cache/elm.js",
@@ -26,7 +30,7 @@ async function compileElmForBrowser(options) {
26
30
  );
27
31
  }
28
32
 
29
- async function compileCliApp(
33
+ export async function compileCliApp(
30
34
  options,
31
35
  elmEntrypointPath,
32
36
  outputPath,
@@ -79,7 +83,7 @@ function _HtmlAsJson_toJson(html) {
79
83
  `;
80
84
 
81
85
  await fsPromises.writeFile(
82
- readFrom,
86
+ readFrom.replace(/\.js$/, ".cjs"),
83
87
  elmFileContent
84
88
  .replace(
85
89
  /return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
@@ -211,7 +215,7 @@ async function runElm(options, elmEntrypointPath, outputPath, cwd) {
211
215
  /**
212
216
  * @param {string} [ cwd ]
213
217
  */
214
- async function runElmReview(cwd) {
218
+ export async function runElmReview(cwd) {
215
219
  const startTime = Date.now();
216
220
  return new Promise((resolve, reject) => {
217
221
  const child = spawnCallback(
@@ -284,12 +288,6 @@ function elmOptimizeLevel2(outputPath, cwd) {
284
288
  });
285
289
  }
286
290
 
287
- module.exports = {
288
- compileElmForBrowser,
289
- runElmReview,
290
- compileCliApp,
291
- };
292
-
293
291
  /**
294
292
  * @param {number} start
295
293
  * @param {number} subtract
@@ -1,6 +1,6 @@
1
- const path = require("path");
1
+ import * as path from "path";
2
2
 
3
- async function resolveConfig() {
3
+ export async function resolveConfig() {
4
4
  const initialConfig = await await import(
5
5
  path.join(process.cwd(), "elm-pages.config.mjs")
6
6
  )
@@ -37,5 +37,3 @@ function defaultHeadTagsTemplate(context) {
37
37
  <meta name="generator" content="elm-pages v${context.cliVersion}" />
38
38
  `;
39
39
  }
40
-
41
- module.exports = { resolveConfig };
@@ -1,40 +1,56 @@
1
- const path = require("path");
2
- const fs = require("fs");
3
- const which = require("which");
4
- const chokidar = require("chokidar");
5
- const { URL } = require("url");
6
- const {
1
+ import * as path from "path";
2
+ import * as fs from "fs";
3
+ import { default as which } from "which";
4
+ import * as chokidar from "chokidar";
5
+ import { URL } from "url";
6
+ import {
7
7
  compileElmForBrowser,
8
8
  runElmReview,
9
9
  compileCliApp,
10
- } = require("./compile-elm.js");
11
- const http = require("http");
12
- const https = require("https");
13
- const codegen = require("./codegen.js");
14
- const kleur = require("kleur");
15
- const serveStatic = require("serve-static");
16
- const connect = require("connect");
17
- const { restoreColorSafe } = require("./error-formatter");
18
- const { Worker, SHARE_ENV } = require("worker_threads");
19
- const os = require("os");
20
- const { ensureDirSync } = require("./file-helpers.js");
21
- const baseMiddleware = require("./basepath-middleware.js");
22
- const devcert = require("devcert");
23
- const busboy = require("busboy");
24
- const { createServer: createViteServer } = require("vite");
25
- const cliVersion = require("../../package.json").version;
26
- const esbuild = require("esbuild");
27
- const { merge_vite_configs } = require("./vite-utils.js");
28
- const { templateHtml } = require("./pre-render-html.js");
29
- const { resolveConfig } = require("./config.js");
30
- const globby = require("globby");
10
+ } from "./compile-elm.js";
11
+ import * as http from "http";
12
+ import * as https from "https";
13
+ import * as codegen from "./codegen.js";
14
+ import * as kleur from "kleur/colors";
15
+ import { default as serveStatic } from "serve-static";
16
+ import { default as connect } from "connect";
17
+ import { restoreColorSafe } from "./error-formatter.js";
18
+ import { Worker, SHARE_ENV } from "worker_threads";
19
+ import * as os from "os";
20
+ import { ensureDirSync } from "./file-helpers.js";
21
+ import { baseMiddleware } from "./basepath-middleware.js";
22
+ import * as devcert from "devcert";
23
+ import * as busboy from "busboy";
24
+ import { createServer as createViteServer } from "vite";
25
+ import * as esbuild from "esbuild";
26
+ import { merge_vite_configs } from "./vite-utils.js";
27
+ import { templateHtml } from "./pre-render-html.js";
28
+ import { resolveConfig } from "./config.js";
29
+ import { globbySync } from "globby";
30
+ import * as packageJson from "../../package.json" assert { type: "json" };
31
+ import { fileURLToPath } from "url";
32
+
33
+ const cliVersion = packageJson.version;
34
+ const __filename = fileURLToPath(import.meta.url);
35
+ const __dirname = path.dirname(__filename);
31
36
 
32
37
  /**
33
38
  * @param {{ port: string; base: string; https: boolean; debug: boolean; }} options
34
39
  */
35
- async function start(options) {
40
+ export async function start(options) {
36
41
  let threadReadyQueue = [];
37
42
  let pool = [];
43
+
44
+ function restartPool() {
45
+ pool.forEach((thread) => thread.worker.terminate());
46
+ const poolSize = Math.max(1, cpuCount / 2 - 1);
47
+ pool = [];
48
+
49
+ for (let index = 0; index < poolSize; index++) {
50
+ pool.push(initWorker(options.base));
51
+ }
52
+ }
53
+
38
54
  ensureDirSync(path.join(process.cwd(), ".elm-pages", "http-response-cache"));
39
55
  const cpuCount = os.cpus().length;
40
56
 
@@ -46,6 +62,7 @@ async function start(options) {
46
62
  const serveCachedFiles = serveStatic(".elm-pages/cache", { index: false });
47
63
  const generatedFilesDirectory = "elm-stuff/elm-pages/generated-files";
48
64
  fs.mkdirSync(generatedFilesDirectory, { recursive: true });
65
+
49
66
  const serveStaticCode = serveStatic(
50
67
  path.join(__dirname, "../static-code"),
51
68
  {}
@@ -175,7 +192,7 @@ async function start(options) {
175
192
  })
176
193
  .catch((error) => {
177
194
  const portBackendTaskFileFound =
178
- globby.sync("./custom-backend-task.*").length > 0;
195
+ globbySync("./custom-backend-task.*").length > 0;
179
196
  if (portBackendTaskFileFound) {
180
197
  // don't present error if there are no files matching custom-backend-task
181
198
  // if there are files matching custom-backend-task, warn the user in case something went wrong loading it
@@ -246,6 +263,7 @@ async function start(options) {
246
263
  }
247
264
  }
248
265
  elmMakeRunning = true;
266
+ restartPool();
249
267
  if (codegenError) {
250
268
  const errorJson = JSON.stringify({
251
269
  type: "compile-errors",
@@ -784,4 +802,3 @@ function paramsToObject(entries) {
784
802
  }
785
803
  return result;
786
804
  }
787
- module.exports = { start };