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