elm-pages 3.0.0-beta.18 → 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.
- package/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +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/js/node_runner.js +1 -1
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
- 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/js/node_runner.js +1 -1
- package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
- package/generator/src/basepath-middleware.js +3 -3
- package/generator/src/build.js +36 -30
- package/generator/src/cli.js +30 -23
- package/generator/src/codegen.js +19 -18
- package/generator/src/compatibility-key.js +1 -1
- package/generator/src/compile-elm.js +20 -22
- package/generator/src/config.js +2 -4
- package/generator/src/dev-server.js +47 -30
- package/generator/src/dir-helpers.js +9 -25
- package/generator/src/elm-codegen.js +2 -4
- package/generator/src/elm-file-constants.js +2 -3
- package/generator/src/error-formatter.js +5 -5
- package/generator/src/file-helpers.js +3 -4
- package/generator/src/generate-template-module-connector.js +14 -15
- package/generator/src/init.js +8 -7
- package/generator/src/pre-render-html.js +11 -12
- package/generator/src/render-worker.js +21 -26
- package/generator/src/render.js +29 -40
- package/generator/src/request-cache.js +13 -8
- package/generator/src/rewrite-client-elm-json.js +5 -5
- package/generator/src/rewrite-elm-json.js +5 -5
- package/generator/src/route-codegen-helpers.js +16 -31
- package/generator/src/seo-renderer.js +1 -3
- package/generator/src/vite-utils.js +1 -2
- package/package.json +9 -8
- package/src/BackendTask/File.elm +1 -1
- package/src/Form.elm +26 -47
- package/src/Pages/Generate.elm +42 -13
- package/src/Pages/Internal/Form.elm +14 -1
- package/src/Server/Request.elm +21 -13
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import * as util from "util";
|
|
2
|
+
import * as fsSync from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
|
|
3
5
|
const fs = {
|
|
4
6
|
writeFile: util.promisify(fsSync.writeFile),
|
|
5
7
|
writeFileSync: fsSync.writeFileSync,
|
|
@@ -15,14 +17,14 @@ const fs = {
|
|
|
15
17
|
/**
|
|
16
18
|
* @param {import("fs").PathLike} dirName
|
|
17
19
|
*/
|
|
18
|
-
async function tryMkdir(dirName) {
|
|
20
|
+
export async function tryMkdir(dirName) {
|
|
19
21
|
const exists = await fs.exists(dirName);
|
|
20
22
|
if (!exists) {
|
|
21
23
|
await fs.mkdir(dirName, { recursive: true });
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
function fileExists(file) {
|
|
27
|
+
export function fileExists(file) {
|
|
26
28
|
return fsSync.promises
|
|
27
29
|
.access(file, fsSync.constants.F_OK)
|
|
28
30
|
.then(() => true)
|
|
@@ -33,18 +35,16 @@ function fileExists(file) {
|
|
|
33
35
|
* @param {string} filePath
|
|
34
36
|
* @param {string} data
|
|
35
37
|
*/
|
|
36
|
-
function writeFileSyncSafe(filePath, data) {
|
|
38
|
+
export function writeFileSyncSafe(filePath, data) {
|
|
37
39
|
fsSync.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
38
40
|
fs.writeFileSync(filePath, data);
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
const path = require("path");
|
|
42
|
-
|
|
43
43
|
/**
|
|
44
44
|
* @param {string} srcDirectory
|
|
45
45
|
* @param {string} destDir
|
|
46
46
|
*/
|
|
47
|
-
async function copyDirFlat(srcDirectory, destDir) {
|
|
47
|
+
export async function copyDirFlat(srcDirectory, destDir) {
|
|
48
48
|
const items = await fs.readdir(srcDirectory);
|
|
49
49
|
items.forEach(function (childItemName) {
|
|
50
50
|
copyDirNested(
|
|
@@ -58,7 +58,7 @@ async function copyDirFlat(srcDirectory, destDir) {
|
|
|
58
58
|
* @param {string} src
|
|
59
59
|
* @param {string} dest
|
|
60
60
|
*/
|
|
61
|
-
async function copyDirNested(src, dest) {
|
|
61
|
+
export async function copyDirNested(src, dest) {
|
|
62
62
|
var exists = fsSync.existsSync(src);
|
|
63
63
|
var stats = exists && fsSync.statSync(src);
|
|
64
64
|
var isDirectory = exists && stats.isDirectory();
|
|
@@ -76,19 +76,3 @@ async function copyDirNested(src, dest) {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
module.exports = {
|
|
80
|
-
writeFile: fs.writeFile,
|
|
81
|
-
writeFileSync: fs.writeFileSync,
|
|
82
|
-
readFile: fs.readFile,
|
|
83
|
-
readFileSync: fsSync.readFileSync,
|
|
84
|
-
copyFile: fs.copyFile,
|
|
85
|
-
exists: fs.exists,
|
|
86
|
-
writeFileSyncSafe,
|
|
87
|
-
tryMkdir,
|
|
88
|
-
copyDirFlat,
|
|
89
|
-
copyDirNested,
|
|
90
|
-
rmSync: fs.rm,
|
|
91
|
-
rm: fs.rm,
|
|
92
|
-
existsSync: fs.existsSync,
|
|
93
|
-
fileExists: fileExists,
|
|
94
|
-
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { spawn as spawnCallback } from "cross-spawn";
|
|
2
2
|
|
|
3
|
-
function runElmCodegenInstall() {
|
|
3
|
+
export function runElmCodegenInstall() {
|
|
4
4
|
return new Promise(async (resolve, reject) => {
|
|
5
5
|
const subprocess = spawnCallback(`elm-codegen`, ["install"], {
|
|
6
6
|
// ignore stdout
|
|
@@ -33,5 +33,3 @@ function runElmCodegenInstall() {
|
|
|
33
33
|
});
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
module.exports = { runElmCodegenInstall };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function elmPagesUiFile() {
|
|
1
|
+
export function elmPagesUiFile() {
|
|
2
2
|
return `module Pages exposing (builtAt)
|
|
3
3
|
|
|
4
4
|
import Time
|
|
@@ -12,7 +12,6 @@ builtAt =
|
|
|
12
12
|
`;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function elmPagesCliFile() {
|
|
15
|
+
export function elmPagesCliFile() {
|
|
16
16
|
return elmPagesUiFile();
|
|
17
17
|
}
|
|
18
|
-
module.exports = { elmPagesUiFile, elmPagesCliFile };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import * as kleur from "kleur/colors";
|
|
2
4
|
|
|
3
5
|
/* Thanks to elm-live for this code!
|
|
4
6
|
https://github.com/wking-io/elm-live/blob/e317b4914c471addea7243c47f28dcebe27a5d36/lib/src/build.js#L65
|
|
@@ -82,7 +84,7 @@ function toKleurColor(color) {
|
|
|
82
84
|
/**
|
|
83
85
|
* @param {RootObject} error
|
|
84
86
|
* */
|
|
85
|
-
const restoreColor = (error) => {
|
|
87
|
+
export const restoreColor = (error) => {
|
|
86
88
|
try {
|
|
87
89
|
if (error.type === "compile-errors") {
|
|
88
90
|
return error.errors
|
|
@@ -111,7 +113,7 @@ const restoreColor = (error) => {
|
|
|
111
113
|
* @param {string} error
|
|
112
114
|
* @returns {string}
|
|
113
115
|
*/
|
|
114
|
-
function restoreColorSafe(error) {
|
|
116
|
+
export function restoreColorSafe(error) {
|
|
115
117
|
try {
|
|
116
118
|
if (typeof error === "string") {
|
|
117
119
|
const asJson = JSON.parse(error);
|
|
@@ -149,8 +151,6 @@ const restoreProblem =
|
|
|
149
151
|
}
|
|
150
152
|
};
|
|
151
153
|
|
|
152
|
-
module.exports = { restoreColor, restoreColorSafe };
|
|
153
|
-
|
|
154
154
|
/** @typedef { CompilerError | ReportError | IElmReviewError } RootObject */
|
|
155
155
|
|
|
156
156
|
/** @typedef { { type: "compile-errors"; errors: Error_[]; } } CompilerError */
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
module.exports = { ensureDirSync, deleteIfExists };
|
|
1
|
+
import * as fs from "fs";
|
|
3
2
|
|
|
4
|
-
function ensureDirSync(dirpath) {
|
|
3
|
+
export function ensureDirSync(dirpath) {
|
|
5
4
|
try {
|
|
6
5
|
fs.mkdirSync(dirpath, { recursive: true });
|
|
7
6
|
} catch (err) {
|
|
@@ -9,7 +8,7 @@ function ensureDirSync(dirpath) {
|
|
|
9
8
|
}
|
|
10
9
|
}
|
|
11
10
|
|
|
12
|
-
function deleteIfExists(/** @type string */ filePath) {
|
|
11
|
+
export function deleteIfExists(/** @type string */ filePath) {
|
|
13
12
|
if (fs.existsSync(filePath)) {
|
|
14
13
|
fs.unlinkSync(filePath);
|
|
15
14
|
}
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const { restoreColorSafe } = require("./error-formatter");
|
|
1
|
+
import { globbySync } from "globby";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { default as mm } from "micromatch";
|
|
4
|
+
import * as routeHelpers from "./route-codegen-helpers.js";
|
|
5
|
+
import { restoreColorSafe } from "./error-formatter.js";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* @param {string} basePath
|
|
11
10
|
* @param {'browser' | 'cli'} phase
|
|
12
11
|
*/
|
|
13
|
-
async function generateTemplateModuleConnector(basePath, phase) {
|
|
14
|
-
const templates =
|
|
12
|
+
export async function generateTemplateModuleConnector(basePath, phase) {
|
|
13
|
+
const templates = globbySync(["app/Route/**/*.elm"], {}).map((file) => {
|
|
15
14
|
const captures = mm.capture("app/Route/**/*.elm", file);
|
|
16
15
|
if (captures) {
|
|
17
16
|
return path.join(captures[0], captures[1]).split(path.sep);
|
|
@@ -63,10 +62,12 @@ async function generateTemplateModuleConnector(basePath, phase) {
|
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
async function runElmCodegenCli(templates, basePath, phase) {
|
|
66
|
-
const
|
|
65
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
66
|
+
const __dirname = path.dirname(__filename);
|
|
67
|
+
const filePath = path.join(__dirname, `../../codegen/elm-pages-codegen.cjs`);
|
|
67
68
|
|
|
68
|
-
const promise = new Promise((resolve, reject) => {
|
|
69
|
-
const elmPagesCodegen =
|
|
69
|
+
const promise = new Promise(async (resolve, reject) => {
|
|
70
|
+
const elmPagesCodegen = (await import(filePath)).default.Elm.Generate;
|
|
70
71
|
|
|
71
72
|
const app = elmPagesCodegen.init({
|
|
72
73
|
flags: { templates: templates, basePath, phase },
|
|
@@ -91,7 +92,7 @@ async function runElmCodegenCli(templates, basePath, phase) {
|
|
|
91
92
|
* @param {string[][]} templates
|
|
92
93
|
* @returns
|
|
93
94
|
*/
|
|
94
|
-
function sortTemplates(templates) {
|
|
95
|
+
export function sortTemplates(templates) {
|
|
95
96
|
return templates.sort((first, second) => {
|
|
96
97
|
const a = sortScore(first);
|
|
97
98
|
const b = sortScore(second);
|
|
@@ -230,5 +231,3 @@ submit toMsg options =
|
|
|
230
231
|
function camelToKebab(input) {
|
|
231
232
|
return input.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
232
233
|
}
|
|
233
|
-
|
|
234
|
-
module.exports = { generateTemplateModuleConnector, sortTemplates };
|
package/generator/src/init.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import { copySync } from "fs-extra/esm";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as kleur from "kleur/colors";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = path.dirname(__filename);
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* @param {string} name
|
|
8
11
|
*/
|
|
9
|
-
async function run(name) {
|
|
12
|
+
export async function run(name) {
|
|
10
13
|
console.log("Creating " + name + " project...");
|
|
11
14
|
|
|
12
15
|
const appRoot = path.resolve(name.toString());
|
|
@@ -36,5 +39,3 @@ async function run(name) {
|
|
|
36
39
|
kleur.green("Project is successfully created in `" + appRoot + "`.")
|
|
37
40
|
);
|
|
38
41
|
}
|
|
39
|
-
|
|
40
|
-
module.exports = { run };
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import * as seo from "./seo-renderer.js";
|
|
2
|
+
import * as packageJson from "../../package.json" assert { type: "json" };
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
|
|
6
|
+
const cliVersion = packageJson.version;
|
|
4
7
|
|
|
5
8
|
/** @typedef { { head: any[]; errors: any[]; html: string; route: string; title: string; } } Arg */
|
|
6
9
|
/** @typedef { { tag : 'PageProgress'; args : Arg[] } } PageProgress */
|
|
7
10
|
|
|
8
|
-
function wrapHtml(basePath, fromElm, contentDatPayload) {
|
|
11
|
+
export function wrapHtml(basePath, fromElm, contentDatPayload) {
|
|
9
12
|
const seoData = seo.gather(fromElm.head);
|
|
10
13
|
return {
|
|
11
14
|
kind: "html-template",
|
|
@@ -21,7 +24,9 @@ function wrapHtml(basePath, fromElm, contentDatPayload) {
|
|
|
21
24
|
* @param {boolean} devMode
|
|
22
25
|
* @param {(context: {cliVersion: string;}) => string} userHeadTagsTemplate
|
|
23
26
|
*/
|
|
24
|
-
function templateHtml(devMode, userHeadTagsTemplate) {
|
|
27
|
+
export function templateHtml(devMode, userHeadTagsTemplate) {
|
|
28
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
29
|
+
const __dirname = path.dirname(__filename);
|
|
25
30
|
return /* html */ `<!DOCTYPE html>
|
|
26
31
|
<!-- ROOT --><html lang="en">
|
|
27
32
|
<head>
|
|
@@ -65,7 +70,7 @@ function indent(snippet) {
|
|
|
65
70
|
/**
|
|
66
71
|
* @param {string} processedTemplate
|
|
67
72
|
*/
|
|
68
|
-
function replaceTemplate(processedTemplate, info) {
|
|
73
|
+
export function replaceTemplate(processedTemplate, info) {
|
|
69
74
|
return processedTemplate
|
|
70
75
|
.replace(
|
|
71
76
|
/<!--\s*PLACEHOLDER_HEAD_AND_DATA\s*-->/,
|
|
@@ -76,9 +81,3 @@ function replaceTemplate(processedTemplate, info) {
|
|
|
76
81
|
.replace(/<!--\s*PLACEHOLDER_HTML\s* -->/, info.html)
|
|
77
82
|
.replace(/<!-- ROOT -->\S*<html lang="en">/m, info.rootElement);
|
|
78
83
|
}
|
|
79
|
-
|
|
80
|
-
module.exports = {
|
|
81
|
-
wrapHtml,
|
|
82
|
-
templateHtml,
|
|
83
|
-
replaceTemplate,
|
|
84
|
-
};
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
global.staticHttpCache = {};
|
|
1
|
+
import * as renderer from "../../generator/src/render.js";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import * as fs from "./dir-helpers.js";
|
|
4
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
5
|
+
import { stat } from "fs/promises";
|
|
6
|
+
import { parentPort, threadId, workerData } from "worker_threads";
|
|
9
7
|
|
|
10
8
|
async function run({ mode, pathname, serverRequest, portsFilePath }) {
|
|
11
9
|
console.time(`${threadId} ${pathname}`);
|
|
@@ -13,7 +11,7 @@ async function run({ mode, pathname, serverRequest, portsFilePath }) {
|
|
|
13
11
|
const renderResult = await renderer.render(
|
|
14
12
|
portsFilePath,
|
|
15
13
|
workerData.basePath,
|
|
16
|
-
requireElm(mode),
|
|
14
|
+
await requireElm(mode),
|
|
17
15
|
mode,
|
|
18
16
|
pathname,
|
|
19
17
|
serverRequest,
|
|
@@ -42,20 +40,17 @@ async function run({ mode, pathname, serverRequest, portsFilePath }) {
|
|
|
42
40
|
console.timeEnd(`${threadId} ${pathname}`);
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
function requireElm(mode) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
delete require.cache[require.resolve(compiledElmPath)];
|
|
57
|
-
return require(compiledElmPath);
|
|
58
|
-
}
|
|
43
|
+
async function requireElm(mode) {
|
|
44
|
+
const compiledElmPath = path.join(
|
|
45
|
+
process.cwd(),
|
|
46
|
+
"elm-stuff/elm-pages/elm.cjs"
|
|
47
|
+
);
|
|
48
|
+
let elmImportPath = compiledElmPath;
|
|
49
|
+
const warnOriginal = console.warn;
|
|
50
|
+
console.warn = function () {};
|
|
51
|
+
const Elm = (await import(elmImportPath)).default;
|
|
52
|
+
console.warn = warnOriginal;
|
|
53
|
+
return Elm;
|
|
59
54
|
}
|
|
60
55
|
|
|
61
56
|
async function outputString(
|
|
@@ -67,13 +62,13 @@ async function outputString(
|
|
|
67
62
|
const args = fromElm;
|
|
68
63
|
const normalizedRoute = args.route.replace(/index$/, "");
|
|
69
64
|
await fs.tryMkdir(`./dist/${normalizedRoute}`);
|
|
70
|
-
const template =
|
|
71
|
-
|
|
65
|
+
const template = readFileSync("./dist/template.html", "utf8");
|
|
66
|
+
writeFileSync(
|
|
72
67
|
`dist/${normalizedRoute}/index.html`,
|
|
73
68
|
renderTemplate(template, fromElm)
|
|
74
69
|
);
|
|
75
70
|
args.contentDatPayload &&
|
|
76
|
-
|
|
71
|
+
writeFileSync(
|
|
77
72
|
`dist/${normalizedRoute}/content.dat`,
|
|
78
73
|
Buffer.from(args.contentDatPayload.buffer)
|
|
79
74
|
);
|
package/generator/src/render.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
19
|
|
|
20
|
-
module.exports = { render, runGenerator };
|
|
21
|
-
|
|
22
20
|
/**
|
|
23
21
|
*
|
|
24
22
|
* @param {string} basePath
|
|
@@ -29,7 +27,7 @@ module.exports = { render, runGenerator };
|
|
|
29
27
|
* @param {boolean} hasFsAccess
|
|
30
28
|
* @returns
|
|
31
29
|
*/
|
|
32
|
-
async function render(
|
|
30
|
+
export async function render(
|
|
33
31
|
portsFile,
|
|
34
32
|
basePath,
|
|
35
33
|
elmModule,
|
|
@@ -39,12 +37,12 @@ async function render(
|
|
|
39
37
|
addBackendTaskWatcher,
|
|
40
38
|
hasFsAccess
|
|
41
39
|
) {
|
|
42
|
-
const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(hasFsAccess);
|
|
43
|
-
resetInMemoryFs();
|
|
40
|
+
// const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(hasFsAccess);
|
|
41
|
+
// resetInMemoryFs();
|
|
44
42
|
foundErrors = false;
|
|
45
43
|
// since init/update are never called in pre-renders, and BackendTask.Http is called using pure NodeJS HTTP fetching
|
|
46
44
|
// we can provide a fake HTTP instead of xhr2 (which is otherwise needed for Elm HTTP requests from Node)
|
|
47
|
-
XMLHttpRequest = {};
|
|
45
|
+
global.XMLHttpRequest = {};
|
|
48
46
|
const result = await runElmApp(
|
|
49
47
|
portsFile,
|
|
50
48
|
basePath,
|
|
@@ -53,7 +51,6 @@ async function render(
|
|
|
53
51
|
path,
|
|
54
52
|
request,
|
|
55
53
|
addBackendTaskWatcher,
|
|
56
|
-
fs,
|
|
57
54
|
hasFsAccess
|
|
58
55
|
);
|
|
59
56
|
return result;
|
|
@@ -64,23 +61,29 @@ async function render(
|
|
|
64
61
|
* @returns
|
|
65
62
|
* @param {string[]} cliOptions
|
|
66
63
|
* @param {any} portsFile
|
|
64
|
+
* @param {string} scriptModuleName
|
|
67
65
|
*/
|
|
68
|
-
async function runGenerator(
|
|
66
|
+
export async function runGenerator(
|
|
67
|
+
cliOptions,
|
|
68
|
+
portsFile,
|
|
69
|
+
elmModule,
|
|
70
|
+
scriptModuleName
|
|
71
|
+
) {
|
|
69
72
|
global.isRunningGenerator = true;
|
|
70
|
-
const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(true);
|
|
71
|
-
resetInMemoryFs();
|
|
73
|
+
// const { fs, resetInMemoryFs } = require("./request-cache-fs.js")(true);
|
|
74
|
+
// resetInMemoryFs();
|
|
72
75
|
foundErrors = false;
|
|
73
76
|
// since init/update are never called in pre-renders, and BackendTask.Http is called using pure NodeJS HTTP fetching
|
|
74
77
|
// we can provide a fake HTTP instead of xhr2 (which is otherwise needed for Elm HTTP requests from Node)
|
|
75
|
-
XMLHttpRequest = {};
|
|
78
|
+
global.XMLHttpRequest = {};
|
|
76
79
|
const result = await runGeneratorAppHelp(
|
|
77
80
|
cliOptions,
|
|
78
81
|
portsFile,
|
|
79
82
|
"",
|
|
80
83
|
elmModule,
|
|
84
|
+
scriptModuleName,
|
|
81
85
|
"production",
|
|
82
86
|
"",
|
|
83
|
-
fs,
|
|
84
87
|
true
|
|
85
88
|
);
|
|
86
89
|
return result;
|
|
@@ -95,15 +98,16 @@ async function runGenerator(cliOptions, portsFile, elmModule) {
|
|
|
95
98
|
* @param {any} portsFile
|
|
96
99
|
* @param {typeof import("fs") | import("memfs").IFs} fs
|
|
97
100
|
* @param {boolean} hasFsAccess
|
|
101
|
+
* @param {string} scriptModuleName
|
|
98
102
|
*/
|
|
99
103
|
function runGeneratorAppHelp(
|
|
100
104
|
cliOptions,
|
|
101
105
|
portsFile,
|
|
102
106
|
basePath,
|
|
103
107
|
elmModule,
|
|
108
|
+
scriptModuleName,
|
|
104
109
|
mode,
|
|
105
110
|
pagePath,
|
|
106
|
-
fs,
|
|
107
111
|
hasFsAccess
|
|
108
112
|
) {
|
|
109
113
|
const isDevServer = mode !== "build";
|
|
@@ -116,7 +120,7 @@ function runGeneratorAppHelp(
|
|
|
116
120
|
app = elmModule.Elm.Main.init({
|
|
117
121
|
flags: {
|
|
118
122
|
compatibilityKey,
|
|
119
|
-
argv: ["",
|
|
123
|
+
argv: ["", `elm-pages run ${scriptModuleName}`, ...cliOptions],
|
|
120
124
|
versionMessage: "1.2.3",
|
|
121
125
|
},
|
|
122
126
|
});
|
|
@@ -173,7 +177,6 @@ function runGeneratorAppHelp(
|
|
|
173
177
|
app,
|
|
174
178
|
mode,
|
|
175
179
|
requestToPerform,
|
|
176
|
-
fs,
|
|
177
180
|
hasFsAccess,
|
|
178
181
|
patternsToWatch
|
|
179
182
|
);
|
|
@@ -184,7 +187,6 @@ function runGeneratorAppHelp(
|
|
|
184
187
|
app,
|
|
185
188
|
mode,
|
|
186
189
|
requestToPerform,
|
|
187
|
-
fs,
|
|
188
190
|
hasFsAccess,
|
|
189
191
|
requestToPerform
|
|
190
192
|
);
|
|
@@ -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",
|
|
@@ -322,7 +315,6 @@ function runElmApp(
|
|
|
322
315
|
app,
|
|
323
316
|
mode,
|
|
324
317
|
requestToPerform,
|
|
325
|
-
fs,
|
|
326
318
|
hasFsAccess,
|
|
327
319
|
patternsToWatch
|
|
328
320
|
);
|
|
@@ -333,7 +325,6 @@ function runElmApp(
|
|
|
333
325
|
app,
|
|
334
326
|
mode,
|
|
335
327
|
requestToPerform,
|
|
336
|
-
fs,
|
|
337
328
|
hasFsAccess,
|
|
338
329
|
requestToPerform
|
|
339
330
|
);
|
|
@@ -406,7 +397,6 @@ async function runHttpJob(
|
|
|
406
397
|
app,
|
|
407
398
|
mode,
|
|
408
399
|
requestToPerform,
|
|
409
|
-
fs,
|
|
410
400
|
hasFsAccess,
|
|
411
401
|
useCache
|
|
412
402
|
) {
|
|
@@ -465,7 +455,6 @@ async function runInternalJob(
|
|
|
465
455
|
app,
|
|
466
456
|
mode,
|
|
467
457
|
requestToPerform,
|
|
468
|
-
fs,
|
|
469
458
|
hasFsAccess,
|
|
470
459
|
patternsToWatch
|
|
471
460
|
) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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(
|
|
17
|
-
|
|
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 =
|
|
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
|
-
|
|
1
|
+
import * as fs from "fs";
|
|
2
2
|
|
|
3
|
-
|
|
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(
|
|
12
|
+
JSON.stringify(rewriteClientElmJsonHelp(elmJson))
|
|
13
13
|
);
|
|
14
|
-
}
|
|
14
|
+
}
|
|
15
15
|
|
|
16
|
-
function
|
|
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
|
-
|
|
1
|
+
import * as fs from "fs";
|
|
2
2
|
|
|
3
|
-
|
|
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(
|
|
12
|
+
JSON.stringify(rewriteElmJsonHelp(elmJson))
|
|
13
13
|
);
|
|
14
|
-
}
|
|
14
|
+
}
|
|
15
15
|
|
|
16
|
-
function
|
|
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
|