elm-pages 3.0.0-beta.3 → 3.0.0-beta.31

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 (129) hide show
  1. package/README.md +10 -1
  2. package/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +2864 -2589
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmi +0 -0
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmo +0 -0
  6. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  7. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  9. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm.json +1 -1
  10. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1447 -342
  11. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +16458 -13724
  12. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  13. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
  14. package/generator/dead-code-review/elm.json +9 -7
  15. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +59 -10
  16. package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +52 -36
  17. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmi +0 -0
  18. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmo +0 -0
  19. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmi +0 -0
  20. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmo +0 -0
  21. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  22. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  23. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  24. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
  25. package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1447 -342
  26. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +24542 -21748
  27. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  28. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
  29. package/generator/review/elm.json +10 -10
  30. package/generator/src/RouteBuilder.elm +113 -107
  31. package/generator/src/SharedTemplate.elm +3 -2
  32. package/generator/src/SiteConfig.elm +3 -2
  33. package/generator/src/basepath-middleware.js +3 -3
  34. package/generator/src/build.js +125 -88
  35. package/generator/src/cli.js +273 -88
  36. package/generator/src/codegen.js +29 -27
  37. package/generator/src/compatibility-key.js +3 -0
  38. package/generator/src/compile-elm.js +43 -26
  39. package/generator/src/config.js +39 -0
  40. package/generator/src/copy-dir.js +2 -2
  41. package/generator/src/dev-server.js +150 -133
  42. package/generator/src/dir-helpers.js +9 -26
  43. package/generator/src/elm-codegen.js +5 -4
  44. package/generator/src/elm-file-constants.js +2 -3
  45. package/generator/src/error-formatter.js +12 -11
  46. package/generator/src/file-helpers.js +3 -4
  47. package/generator/src/generate-template-module-connector.js +23 -22
  48. package/generator/src/init.js +9 -8
  49. package/generator/src/pre-render-html.js +39 -28
  50. package/generator/src/render-test.js +109 -0
  51. package/generator/src/render-worker.js +25 -28
  52. package/generator/src/render.js +320 -142
  53. package/generator/src/request-cache.js +252 -163
  54. package/generator/src/resolve-elm-module.js +63 -0
  55. package/generator/src/rewrite-client-elm-json.js +6 -5
  56. package/generator/src/rewrite-elm-json-help.js +56 -0
  57. package/generator/src/rewrite-elm-json.js +17 -7
  58. package/generator/src/route-codegen-helpers.js +16 -31
  59. package/generator/src/seo-renderer.js +12 -7
  60. package/generator/src/vite-utils.js +77 -0
  61. package/generator/static-code/hmr.js +79 -13
  62. package/generator/template/app/Api.elm +6 -5
  63. package/generator/template/app/Effect.elm +123 -0
  64. package/generator/template/app/ErrorPage.elm +37 -6
  65. package/generator/template/app/Route/Index.elm +17 -10
  66. package/generator/template/app/Shared.elm +24 -47
  67. package/generator/template/app/Site.elm +19 -6
  68. package/generator/template/app/View.elm +1 -8
  69. package/generator/template/elm-tooling.json +0 -3
  70. package/generator/template/elm.json +32 -24
  71. package/generator/template/package.json +10 -4
  72. package/package.json +29 -27
  73. package/src/ApiRoute.elm +199 -61
  74. package/src/BackendTask/Custom.elm +325 -0
  75. package/src/BackendTask/Env.elm +90 -0
  76. package/src/{DataSource → BackendTask}/File.elm +128 -43
  77. package/src/{DataSource → BackendTask}/Glob.elm +136 -125
  78. package/src/BackendTask/Http.elm +673 -0
  79. package/src/{DataSource → BackendTask}/Internal/Glob.elm +1 -1
  80. package/src/BackendTask/Internal/Request.elm +28 -0
  81. package/src/BackendTask/Random.elm +79 -0
  82. package/src/BackendTask/Time.elm +47 -0
  83. package/src/BackendTask.elm +537 -0
  84. package/src/FatalError.elm +89 -0
  85. package/src/Form/Field.elm +21 -9
  86. package/src/Form/FieldView.elm +94 -14
  87. package/src/Form.elm +275 -400
  88. package/src/Head.elm +237 -7
  89. package/src/HtmlPrinter.elm +7 -3
  90. package/src/Internal/ApiRoute.elm +7 -5
  91. package/src/PageServerResponse.elm +6 -1
  92. package/src/Pages/FormState.elm +6 -5
  93. package/src/Pages/GeneratorProgramConfig.elm +15 -0
  94. package/src/Pages/Internal/FatalError.elm +5 -0
  95. package/src/Pages/Internal/Form.elm +21 -1
  96. package/src/Pages/{Msg.elm → Internal/Msg.elm} +26 -16
  97. package/src/Pages/Internal/Platform/Cli.elm +598 -763
  98. package/src/Pages/Internal/Platform/CompatibilityKey.elm +6 -0
  99. package/src/Pages/Internal/Platform/Effect.elm +1 -2
  100. package/src/Pages/Internal/Platform/GeneratorApplication.elm +373 -0
  101. package/src/Pages/Internal/Platform/StaticResponses.elm +73 -270
  102. package/src/Pages/Internal/Platform/ToJsPayload.elm +4 -7
  103. package/src/Pages/Internal/Platform.elm +216 -102
  104. package/src/Pages/Internal/Script.elm +17 -0
  105. package/src/Pages/Internal/StaticHttpBody.elm +35 -1
  106. package/src/Pages/Manifest.elm +29 -4
  107. package/src/Pages/PageUrl.elm +23 -9
  108. package/src/Pages/ProgramConfig.elm +14 -10
  109. package/src/Pages/Script.elm +109 -0
  110. package/src/Pages/SiteConfig.elm +3 -2
  111. package/src/Pages/StaticHttp/Request.elm +2 -2
  112. package/src/Pages/StaticHttpRequest.elm +23 -98
  113. package/src/PagesMsg.elm +92 -0
  114. package/src/Path.elm +16 -19
  115. package/src/QueryParams.elm +21 -172
  116. package/src/RequestsAndPending.elm +8 -19
  117. package/src/Result/Extra.elm +26 -0
  118. package/src/Scaffold/Form.elm +560 -0
  119. package/src/Scaffold/Route.elm +1388 -0
  120. package/src/Server/Request.elm +43 -37
  121. package/src/Server/Session.elm +62 -42
  122. package/src/Server/SetCookie.elm +12 -4
  123. package/src/Test/Html/Internal/ElmHtml/ToString.elm +8 -9
  124. package/src/DataSource/Env.elm +0 -38
  125. package/src/DataSource/Http.elm +0 -446
  126. package/src/DataSource/Internal/Request.elm +0 -20
  127. package/src/DataSource/Port.elm +0 -90
  128. package/src/DataSource.elm +0 -538
  129. package/src/Pages/Generate.elm +0 -800
@@ -1,21 +1,29 @@
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 "node:fs";
9
+ import * as path from "node:path";
10
+ import { restoreColorSafe } from "./error-formatter.js";
11
+ import * as renderer from "./render.js";
12
+ import * as globby from "globby";
13
+ import * as esbuild from "esbuild";
14
+ import { rewriteElmJson } from "./rewrite-elm-json.js";
15
+ import { ensureDirSync } from "./file-helpers.js";
16
+ import * as url from "url";
17
+ import { default as which } from "which";
18
+ import * as commander from "commander";
19
+ import { runElmCodegenInstall } from "./elm-codegen.js";
20
+ import { packageVersion } from "./compatibility-key.js";
21
+ import { resolveInputPathOrModuleName } from "./resolve-elm-module.js";
2
22
 
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
-
12
- const commander = require("commander");
13
- const { compileCliApp } = require("./compile-elm.js");
14
- const { runElmCodegenInstall } = require("./elm-codegen.js");
15
23
  const Argument = commander.Argument;
16
24
  const Option = commander.Option;
17
-
18
- const packageVersion = require("../../package.json").version;
25
+ const __filename = url.fileURLToPath(import.meta.url);
26
+ const __dirname = path.dirname(__filename);
19
27
 
20
28
  async function main() {
21
29
  const program = new commander.Command();
@@ -36,9 +44,6 @@ async function main() {
36
44
  )
37
45
  .description("run a full site build")
38
46
  .action(async (options) => {
39
- if (!options.keepCache) {
40
- clearHttpAndPortCache();
41
- }
42
47
  options.base = normalizeUrl(options.base);
43
48
  await build.run(options);
44
49
  });
@@ -69,9 +74,6 @@ async function main() {
69
74
  .option("--base <basePath>", "serve site under a base path", "/")
70
75
  .option("--https", "uses a https server")
71
76
  .action(async (options) => {
72
- if (!options.keepCache) {
73
- clearHttpAndPortCache();
74
- }
75
77
  options.base = normalizeUrl(options.base);
76
78
  await dev.start(options);
77
79
  });
@@ -84,66 +86,170 @@ async function main() {
84
86
  });
85
87
 
86
88
  program
87
- .command("codegen <moduleName>")
88
- .description("run a generator")
89
+ .command("run <elmModulePath>")
90
+ .description("run an elm-pages script")
89
91
  .allowUnknownOption()
90
92
  .allowExcessArguments()
91
- .action(async (moduleName, options, options2) => {
92
- if (!/^[A-Z][a-zA-Z0-9_]*(\.[A-Z][a-zA-Z0-9_]*)*$/.test(moduleName)) {
93
- throw `Invalid module name "${moduleName}", must be in the format of an Elm module`;
94
- }
95
- const splitModuleName = moduleName.split(".");
96
- const expectedFilePath = path.join(
97
- process.cwd(),
98
- "codegen",
99
- `${splitModuleName.join("/")}.elm`
93
+ .action(async (elmModulePath, options, options2) => {
94
+ const unprocessedCliOptions = options2.args.splice(
95
+ options2.processedArgs.length,
96
+ options2.args.length
100
97
  );
101
- if (!fs.existsSync(expectedFilePath)) {
102
- throw `I couldn't find a module named ${expectedFilePath}`;
103
- }
104
98
  try {
105
- await codegen.generate("");
106
- await runElmCodegenInstall();
107
- await compileCliApp(
108
- // { debug: true },
109
- {},
110
- `${splitModuleName.join("/")}.elm`,
111
- path.join(process.cwd(), "codegen/elm-stuff/scaffold.js"),
112
- "codegen",
113
-
114
- path.join(process.cwd(), "codegen/elm-stuff/scaffold.js")
99
+ await compileElmForScript(elmModulePath);
100
+
101
+ const { moduleName, projectDirectory, sourceDirectory } =
102
+ resolveInputPathOrModuleName(elmModulePath);
103
+
104
+ const portBackendTaskCompiled = esbuild
105
+ .build({
106
+ entryPoints: [path.join(projectDirectory, "./custom-backend-task")],
107
+ platform: "node",
108
+ outfile: path.join(
109
+ projectDirectory,
110
+ ".elm-pages/compiled-ports/custom-backend-task.mjs"
111
+ ),
112
+ assetNames: "[name]-[hash]",
113
+ chunkNames: "chunks/[name]-[hash]",
114
+ metafile: true,
115
+ bundle: true,
116
+ format: "esm",
117
+ packages: "external",
118
+ logLevel: "silent",
119
+ })
120
+ .then((result) => {
121
+ try {
122
+ return Object.keys(result.metafile.outputs)[0];
123
+ } catch (e) {
124
+ return null;
125
+ }
126
+ })
127
+ .catch((error) => {
128
+ const portBackendTaskFileFound =
129
+ globby.globbySync("./custom-backend-task.*").length > 0;
130
+ if (portBackendTaskFileFound) {
131
+ // don't present error if there are no files matching custom-backend-task
132
+ // if there are files matching custom-backend-task, warn the user in case something went wrong loading it
133
+ console.error(
134
+ "Failed to start custom-backend-task watcher",
135
+ error
136
+ );
137
+ }
138
+ });
139
+ const portsPath = await portBackendTaskCompiled;
140
+
141
+ const cwd = process.cwd();
142
+ process.chdir(projectDirectory);
143
+ // TODO have option for compiling with --debug or not (maybe allow running with elm-optimize-level-2 as well?)
144
+
145
+ let executableName = await lamderaOrElmFallback();
146
+ await build.compileCliApp({ debug: "debug", executableName });
147
+ fs.renameSync(
148
+ `${projectDirectory}/elm-stuff/elm-pages/elm.js`,
149
+ `${projectDirectory}/elm-stuff/elm-pages/elm.cjs`
150
+ );
151
+ process.chdir(cwd);
152
+ await renderer.runGenerator(
153
+ unprocessedCliOptions,
154
+ portsPath
155
+ ? await import(url.pathToFileURL(path.resolve(portsPath)).href)
156
+ : null,
157
+ await requireElm(`${projectDirectory}/elm-stuff/elm-pages/elm.cjs`),
158
+ moduleName
115
159
  );
116
160
  } catch (error) {
117
161
  console.log(restoreColorSafe(error));
118
162
  process.exit(1);
119
163
  }
164
+ });
165
+
166
+ program
167
+ .command("bundle-script <moduleName>")
168
+ .description("bundle an elm-pages script")
169
+ .option(
170
+ "--debug",
171
+ "Skip elm-optimize-level-2 and run elm make with --debug"
172
+ )
173
+ .option(
174
+ "--output <path>",
175
+ "Output path for compiled script",
176
+ "./myscript.mjs"
177
+ )
178
+ .option(
179
+ "--external <package-or-pattern>",
180
+ "build site to be served under a base path",
181
+ collect,
182
+ []
183
+ )
184
+ .action(async (elmModulePath, options, options2) => {
185
+ const { moduleName, projectDirectory, sourceDirectory } =
186
+ resolveInputPathOrModuleName(elmModulePath);
187
+ await compileElmForScript(elmModulePath);
120
188
 
121
- const elmScaffoldProgram = getAt(
122
- splitModuleName,
123
- require(path.join(process.cwd(), "./codegen/elm-stuff/scaffold.js")).Elm
189
+ const cwd = process.cwd();
190
+ process.chdir(projectDirectory);
191
+ // TODO have option for compiling with --debug or not (maybe allow running with elm-optimize-level-2 as well?)
192
+
193
+ let executableName = await lamderaOrElmFallback();
194
+ await build.compileCliApp({ debug: options.debug, executableName });
195
+ fs.renameSync(
196
+ `${projectDirectory}/elm-stuff/elm-pages/elm.js`,
197
+ `${projectDirectory}/elm-stuff/elm-pages/elm.cjs`
124
198
  );
125
- const program = elmScaffoldProgram.init({
126
- flags: { argv: ["", ...options2.args], versionMessage: "1.2.3" },
127
- });
199
+ process.chdir(cwd);
128
200
 
129
- safeSubscribe(program, "print", (message) => {
130
- console.log(message);
131
- });
132
- safeSubscribe(program, "printAndExitFailure", (message) => {
133
- console.log(message);
201
+ try {
202
+ const { moduleName, projectDirectory, sourceDirectory } =
203
+ resolveInputPathOrModuleName(elmModulePath);
204
+
205
+ const portBackendTaskFileFound =
206
+ globby.globbySync(
207
+ path.join(projectDirectory, "custom-backend-task.*")
208
+ ).length > 0;
209
+
210
+ const scriptRunner = `${
211
+ portBackendTaskFileFound
212
+ ? `import * as customBackendTask from "${path.join(
213
+ projectDirectory,
214
+ "./custom-backend-task"
215
+ )}";`
216
+ : "const customBackendTask = {};"
217
+ }
218
+ import * as renderer from "./render.js";
219
+ import { default as Elm } from "${path.join(
220
+ projectDirectory,
221
+ "elm-stuff/elm-pages/elm.cjs"
222
+ )}";
223
+
224
+ await renderer.runGenerator(
225
+ [...process.argv].splice(2),
226
+ customBackendTask,
227
+ Elm,
228
+ "${moduleName}"
229
+ );
230
+ `;
231
+ // source: https://github.com/evanw/esbuild/pull/2067#issuecomment-1073039746
232
+ const ESM_REQUIRE_SHIM = `
233
+ await(async()=>{let{dirname:e}=await import("path"),{fileURLToPath:i}=await import("url");if(typeof globalThis.__filename>"u"&&(globalThis.__filename=i(import.meta.url)),typeof globalThis.__dirname>"u"&&(globalThis.__dirname=e(globalThis.__filename)),typeof globalThis.require>"u"){let{default:a}=await import("module");globalThis.require=a.createRequire(import.meta.url)}})();
234
+ `;
235
+
236
+ await esbuild.build({
237
+ format: "esm",
238
+ platform: "node",
239
+ stdin: { contents: scriptRunner, resolveDir: __dirname },
240
+ bundle: true,
241
+ // TODO do I need to make the outfile joined with the current working directory?
242
+
243
+ outfile: path.resolve(cwd, options.output),
244
+ external: ["node:*", ...options.external],
245
+ minify: true,
246
+ absWorkingDir: projectDirectory,
247
+ banner: { js: `#!/usr/bin/env node\n\n${ESM_REQUIRE_SHIM}` },
248
+ });
249
+ } catch (error) {
250
+ console.log(restoreColorSafe(error));
134
251
  process.exit(1);
135
- });
136
- safeSubscribe(program, "printAndExitSuccess", (message) => {
137
- console.log(message);
138
- process.exit(0);
139
- });
140
- safeSubscribe(program, "writeFile", async (info) => {
141
- const filePath = path.join(process.cwd(), "app", info.path);
142
- await dirHelpers.tryMkdir(path.dirname(filePath));
143
- fs.writeFileSync(filePath, info.body);
144
- console.log("Success! Created file", filePath);
145
- process.exit(0);
146
- });
252
+ }
147
253
  });
148
254
 
149
255
  program
@@ -152,7 +258,7 @@ async function main() {
152
258
  .option("--port <number>", "serve site at localhost:<port>", "8000")
153
259
  .action(async (options) => {
154
260
  await codegen.generate("/");
155
- const DocServer = require("elm-doc-preview");
261
+ const DocServer = (await import("elm-doc-preview")).default;
156
262
  const server = new DocServer({
157
263
  port: options.port,
158
264
  browser: true,
@@ -180,29 +286,11 @@ function getAt(properties, object) {
180
286
  }
181
287
 
182
288
  function safeSubscribe(program, portName, subscribeFunction) {
183
- program.ports[portName] &&
289
+ program.ports &&
290
+ program.ports[portName] &&
184
291
  program.ports[portName].subscribe(subscribeFunction);
185
292
  }
186
293
 
187
- function clearHttpAndPortCache() {
188
- const directory = ".elm-pages/http-response-cache";
189
- if (fs.existsSync(directory)) {
190
- fs.readdir(directory, (err, files) => {
191
- if (err) {
192
- throw err;
193
- }
194
-
195
- for (const file of files) {
196
- fs.unlink(path.join(directory, file), (err) => {
197
- if (err) {
198
- throw err;
199
- }
200
- });
201
- }
202
- });
203
- }
204
- }
205
-
206
294
  /**
207
295
  * @param {string} rawPagePath
208
296
  */
@@ -218,5 +306,102 @@ function normalizeUrl(rawPagePath) {
218
306
  // with detecting whether the path contains the base.
219
307
  return `/${segments.join("/")}`;
220
308
  }
309
+ /**
310
+ * @param {string} compiledElmPath
311
+ */
312
+ async function requireElm(compiledElmPath) {
313
+ const warnOriginal = console.warn;
314
+ console.warn = function () {};
315
+
316
+ let Elm = (await import(path.resolve(compiledElmPath))).default;
317
+ console.warn = warnOriginal;
318
+ return Elm;
319
+ }
320
+
321
+ /**
322
+ * @param {string} moduleName
323
+ */
324
+ function generatorWrapperFile(moduleName) {
325
+ return `port module Main exposing (main)
326
+
327
+ import BackendTask exposing (BackendTask)
328
+ import FatalError
329
+ import Cli.Program as Program
330
+ import Json.Decode as Decode
331
+ import Json.Encode as Encode
332
+ import Pages.Internal.Platform.GeneratorApplication
333
+ import ${moduleName}
334
+
335
+
336
+ main : Program.StatefulProgram Pages.Internal.Platform.GeneratorApplication.Model Pages.Internal.Platform.GeneratorApplication.Msg (BackendTask FatalError.FatalError ()) Pages.Internal.Platform.GeneratorApplication.Flags
337
+ main =
338
+ Pages.Internal.Platform.GeneratorApplication.app
339
+ { data = ${moduleName}.run
340
+ , toJsPort = toJsPort
341
+ , fromJsPort = fromJsPort identity
342
+ , gotBatchSub = gotBatchSub identity
343
+ , sendPageData = \\_ -> Cmd.none
344
+ }
345
+
346
+
347
+ port toJsPort : Encode.Value -> Cmd msg
348
+
349
+
350
+ port fromJsPort : (Decode.Value -> msg) -> Sub msg
351
+
352
+
353
+ port gotBatchSub : (Decode.Value -> msg) -> Sub msg
354
+ `;
355
+ }
356
+ function collect(value, previous) {
357
+ return previous.concat([value]);
358
+ }
359
+
360
+ async function compileElmForScript(elmModulePath) {
361
+ const { moduleName, projectDirectory, sourceDirectory } =
362
+ resolveInputPathOrModuleName(elmModulePath);
363
+ const splitModuleName = moduleName.split(".");
364
+ const expectedFilePath = path.join(
365
+ sourceDirectory,
366
+ `${splitModuleName.join("/")}.elm`
367
+ );
368
+ if (!fs.existsSync(expectedFilePath)) {
369
+ throw `I couldn't find a module named ${expectedFilePath}`;
370
+ }
371
+ // await codegen.generate("");
372
+ ensureDirSync(path.join(process.cwd(), ".elm-pages", "http-response-cache"));
373
+ if (fs.existsSync("./codegen/")) {
374
+ await runElmCodegenInstall();
375
+ }
376
+
377
+ ensureDirSync(`${projectDirectory}/elm-stuff`);
378
+ ensureDirSync(`${projectDirectory}/elm-stuff/elm-pages/.elm-pages`);
379
+ await fs.promises.writeFile(
380
+ path.join(`${projectDirectory}/elm-stuff/elm-pages/.elm-pages/Main.elm`),
381
+ generatorWrapperFile(moduleName)
382
+ );
383
+ let executableName = await lamderaOrElmFallback();
384
+ try {
385
+ await which("lamdera");
386
+ } catch (error) {
387
+ await which("elm");
388
+ executableName = "elm";
389
+ }
390
+ await rewriteElmJson(
391
+ `${projectDirectory}/elm.json`,
392
+ `${projectDirectory}/elm-stuff/elm-pages/elm.json`,
393
+ { executableName }
394
+ );
395
+ }
396
+
397
+ async function lamderaOrElmFallback() {
398
+ try {
399
+ await which("lamdera");
400
+ return "lamdera";
401
+ } catch (error) {
402
+ await which("elm");
403
+ return "elm";
404
+ }
405
+ }
221
406
 
222
407
  main();
@@ -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 "node:fs";
2
+ import * as fsExtra from "fs-extra";
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(),
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
  });
@@ -174,15 +177,16 @@ async function copyToBoth(moduleToCopy) {
174
177
  copyFileEnsureDir(
175
178
  path.join(__dirname, moduleToCopy),
176
179
  path.join(`./.elm-pages/`, moduleToCopy)
177
- ),
178
- copyFileEnsureDir(
180
+ ),
181
+ copyFileEnsureDir(
179
182
  path.join(__dirname, moduleToCopy),
180
183
  path.join(`./elm-stuff/elm-pages/client/.elm-pages`, moduleToCopy)
181
- ),
182
- copyFileEnsureDir(
184
+ ),
185
+ copyFileEnsureDir(
183
186
  path.join(__dirname, moduleToCopy),
184
187
  path.join(`./elm-stuff/elm-pages/.elm-pages/`, moduleToCopy)
185
- )]);
188
+ ),
189
+ ]);
186
190
  }
187
191
 
188
192
  /**
@@ -190,10 +194,10 @@ async function copyToBoth(moduleToCopy) {
190
194
  * @param {string} to
191
195
  */
192
196
  async function copyFileEnsureDir(from, to) {
193
- await fs.promises.mkdir(path.dirname(to), {
194
- recursive: true,
195
- })
196
- await fs.promises.copyFile(from, to)
197
+ await fs.promises.mkdir(path.dirname(to), {
198
+ recursive: true,
199
+ });
200
+ await fs.promises.copyFile(from, to);
197
201
  }
198
202
 
199
203
  /**
@@ -229,5 +233,3 @@ async function listFiles(dir) {
229
233
  function merge(arrays) {
230
234
  return [].concat.apply([], arrays);
231
235
  }
232
-
233
- module.exports = { generate, generateClientFolder };
@@ -0,0 +1,3 @@
1
+ export const compatibilityKey = 12;
2
+
3
+ export const packageVersion = "3.0.0-beta.31";
@@ -1,18 +1,39 @@
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
+ import { rewriteElmJson } from "./rewrite-elm-json-help.js";
10
+ import { ensureDirSync } from "./file-helpers.js";
11
+ const __filename = fileURLToPath(import.meta.url);
12
+ const __dirname = path.dirname(__filename);
13
13
 
14
- async function compileElmForBrowser(options) {
15
- await runElm(options, "./.elm-pages/Main.elm", pathToClientElm);
14
+ export async function compileElmForBrowser(options) {
15
+ // 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.
16
+ const pathToClientElm = path.join(
17
+ process.cwd(),
18
+ "elm-stuff/elm-pages/",
19
+ "browser-elm.js"
20
+ );
21
+ const secretDir = path.join(process.cwd(), "elm-stuff/elm-pages/browser-elm");
22
+ await fsHelpers.tryMkdir(secretDir);
23
+ rewriteElmJson(process.cwd(), secretDir, function (elmJson) {
24
+ elmJson["source-directories"] = elmJson["source-directories"].map(
25
+ (item) => {
26
+ return "../../../" + item;
27
+ }
28
+ );
29
+ return elmJson;
30
+ });
31
+ await runElm(
32
+ options,
33
+ "../../../.elm-pages/Main.elm",
34
+ pathToClientElm,
35
+ secretDir
36
+ );
16
37
  return fs.promises.writeFile(
17
38
  "./.elm-pages/cache/elm.js",
18
39
  inject(await fs.promises.readFile(pathToClientElm, "utf-8")).replace(
@@ -26,7 +47,7 @@ async function compileElmForBrowser(options) {
26
47
  );
27
48
  }
28
49
 
29
- async function compileCliApp(
50
+ export async function compileCliApp(
30
51
  options,
31
52
  elmEntrypointPath,
32
53
  outputPath,
@@ -79,7 +100,7 @@ function _HtmlAsJson_toJson(html) {
79
100
  `;
80
101
 
81
102
  await fsPromises.writeFile(
82
- readFrom,
103
+ readFrom.replace(/\.js$/, ".cjs"),
83
104
  elmFileContent
84
105
  .replace(
85
106
  /return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
@@ -132,9 +153,11 @@ function spawnElmMake(options, elmEntrypointPath, outputPath, cwd) {
132
153
  }
133
154
  );
134
155
  if (await fsHelpers.fileExists(outputPath)) {
135
- await fsPromises.unlink(outputPath, {
136
- force: true /* ignore errors if file doesn't exist */,
137
- });
156
+ try {
157
+ await fsPromises.unlink(outputPath, {
158
+ force: true /* ignore errors if file doesn't exist */,
159
+ });
160
+ } catch (e) {}
138
161
  }
139
162
  let commandOutput = "";
140
163
 
@@ -211,7 +234,7 @@ async function runElm(options, elmEntrypointPath, outputPath, cwd) {
211
234
  /**
212
235
  * @param {string} [ cwd ]
213
236
  */
214
- async function runElmReview(cwd) {
237
+ export async function runElmReview(cwd) {
215
238
  const startTime = Date.now();
216
239
  return new Promise((resolve, reject) => {
217
240
  const child = spawnCallback(
@@ -284,12 +307,6 @@ function elmOptimizeLevel2(outputPath, cwd) {
284
307
  });
285
308
  }
286
309
 
287
- module.exports = {
288
- compileElmForBrowser,
289
- runElmReview,
290
- compileCliApp,
291
- };
292
-
293
310
  /**
294
311
  * @param {number} start
295
312
  * @param {number} subtract
@@ -0,0 +1,39 @@
1
+ import * as path from "path";
2
+
3
+ export async function resolveConfig() {
4
+ const initialConfig = await await import(
5
+ path.join(process.cwd(), "elm-pages.config.mjs")
6
+ )
7
+ .then(async (elmPagesConfig) => {
8
+ return (
9
+ elmPagesConfig.default || {
10
+ headTagsTemplate: defaultHeadTagsTemplate,
11
+ }
12
+ );
13
+ })
14
+ .catch((error) => {
15
+ console.warn(
16
+ "No `elm-pages.config.mjs` file found. Using default config."
17
+ );
18
+ return {
19
+ headTagsTemplate: defaultHeadTagsTemplate,
20
+ vite: {},
21
+ };
22
+ });
23
+
24
+ return {
25
+ preloadTagForFile: function () {
26
+ return true;
27
+ },
28
+ headTagsTemplate: defaultHeadTagsTemplate,
29
+ vite: {},
30
+ ...initialConfig,
31
+ };
32
+ }
33
+
34
+ function defaultHeadTagsTemplate(context) {
35
+ return `
36
+ <link rel="stylesheet" href="/style.css" />
37
+ <meta name="generator" content="elm-pages v${context.cliVersion}" />
38
+ `;
39
+ }