Run entrypoint directly with generated code instead of wiring it in with a Main.elm.

This commit is contained in:
Dillon Kearns 2021-04-05 13:59:21 -07:00
parent 80e0f8db66
commit 5d496c3834
6 changed files with 67 additions and 29 deletions

View File

@ -14,6 +14,24 @@ import Pages.StaticFile as StaticFile
import Pages.StaticHttp as StaticHttp
-- TODO wire this in as part of the config
generateFiles :
StaticHttp.Request
(List
(Result
String
{ path : List String
, content : String
}
)
)
generateFiles =
StaticHttp.succeed []
type alias StaticData =
{ siteName : String
}
@ -25,6 +43,7 @@ config =
, canonicalUrl = canonicalUrl
, manifest = manifest
, head = head
, generateFiles = generateFiles
}

View File

@ -60,7 +60,7 @@ function runElmApp() {
return new Promise((resolve, _) => {
const mode /** @type { "dev" | "prod" } */ = "elm-to-html-beta";
const staticHttpCache = {};
const app = require(ELM_FILE_PATH).Elm.Main.init({
const app = require(ELM_FILE_PATH).Elm.TemplateModulesBeta.init({
flags: { secrets: process.env, mode, staticHttpCache },
});
@ -189,7 +189,7 @@ async function outputString(/** @type { PageProgress } */ fromElm) {
async function compileElm() {
const outputPath = `dist/elm.js`;
await spawnElmMake("src/Main.elm", outputPath);
await spawnElmMake("gen/TemplateModulesBeta.elm", outputPath);
const elmEsmContent = await elmToEsm(path.join(process.cwd(), outputPath));
if (debug) {
@ -306,7 +306,11 @@ async function copyAssets() {
}
async function compileCliApp() {
await spawnElmMake("../../src/Main.elm", "elm.js", "./elm-stuff/elm-pages");
await spawnElmMake(
"TemplateModulesBeta.elm",
"elm.js",
"./elm-stuff/elm-pages"
);
const elmFileContent = await fs.readFile(ELM_FILE_PATH, "utf-8");
await fs.writeFile(

View File

@ -58,8 +58,6 @@ async function writeFiles(markdownContent) {
deleteIfExists("./elm-stuff/elm-pages/Pages/Platform.elm");
const uiFileContent = elmPagesUiFile(staticRoutes, markdownContent);
const templateConnectorFile = generateTemplateModuleConnector();
fs.writeFileSync("./gen/Pages.elm", uiFileContent);
// write `Pages.elm` with cli interface
@ -69,9 +67,12 @@ async function writeFiles(markdownContent) {
);
fs.writeFileSync(
"./elm-stuff/elm-pages/TemplateModulesBeta.elm",
templateConnectorFile
generateTemplateModuleConnector("cli")
);
fs.writeFileSync(
"./gen/TemplateModulesBeta.elm",
generateTemplateModuleConnector("browser")
);
fs.writeFileSync("./gen/TemplateModulesBeta.elm", templateConnectorFile);
// write modified elm.json to elm-stuff/elm-pages/
copyModifiedElmJson();

View File

@ -2,7 +2,10 @@ const globby = require("globby");
const path = require("path");
const mm = require("micromatch");
function generateTemplateModuleConnector() {
/**
* @param {'browser' | 'cli'} phase
*/
function generateTemplateModuleConnector(phase) {
const templates = globby.sync(["src/Template/**/*.elm"], {}).map((file) => {
const captures = mm.capture("src/Template/**/*.elm", file);
if (captures) {
@ -15,8 +18,11 @@ function generateTemplateModuleConnector() {
return `module TemplateModulesBeta exposing (..)
import Browser
import Pages.Internal.Platform
import Pages.Internal.Platform.ToJsPayload
import Pages.Manifest as Manifest
import Shared
import Site
import NoMetadata exposing (NoMetadata(..))
import Head
import Html exposing (Html)
@ -326,35 +332,31 @@ templateSubscriptions route path model =
Sub.none
mainTemplate { site } =
Pages.Platform.init
main : Pages.Internal.Platform.Program Model Msg (Maybe Route) Pages.PathKey
main =
Pages.Internal.Platform.${
phase === "browser" ? "application" : "cliApplication"
}
{ init = init Nothing
, urlToRoute = urlToRoute
, routeToPath = routeToPath
, site = Site.config
, getStaticRoutes =
StaticHttp.combine
[ StaticHttp.succeed
[ ${templates
.filter((name) => !isParameterizedRoute(name))
.map((name) => `${routeVariant(name)} {}`)
.join("\n , ")}
[ RouteBlog {}
, RouteDocumentation {}
, RoutePage {}
, RouteShowcase {}
]
, ${templates
.filter((name) => isParameterizedRoute(name))
.map(
(name) =>
`Template.${moduleName(
name
)}.template.staticRoutes |> StaticHttp.map (List.map Route${pathNormalizedName(
name
)})`
)
.join("\n , ")}
, Template.Blog.Slug_.template.staticRoutes |> StaticHttp.map (List.map RouteBlog__Slug_)
, Template.Hello.Name_.template.staticRoutes |> StaticHttp.map (List.map RouteHello__Name_)
]
|> StaticHttp.map List.concat
|> StaticHttp.map (List.map Just)
, view = \\_ -> view
, update = update
, manifest = Pages.Internal.Platform.ToJsPayload.stubManifest
, subscriptions =
\\metadata path model ->
Sub.batch
@ -362,9 +364,11 @@ mainTemplate { site } =
, templateSubscriptions (RouteBlog {}) path model
]
, onPageChange = Just OnPageChange
, manifest = site.manifest
, canonicalSiteUrl = site.canonicalUrl
, internals = Pages.internals
, canonicalSiteUrl = "TODO"
, toJsPort = Pages.internals.toJsPort
, fromJsPort = Pages.internals.fromJsPort
, generateFiles = Site.config.generateFiles
, pathKey = Pages.internals.pathKey
}

View File

@ -174,7 +174,7 @@ function httpGet(/** @type string */ theUrl) {
userInit(
pagesInit({
mainElmModule: Elm.Main,
mainElmModule: Elm.TemplateModulesBeta,
})
);
`;

View File

@ -12,4 +12,14 @@ type alias SiteConfig staticData pathKey =
, head :
staticData
-> List (Head.Tag pathKey)
, generateFiles :
StaticHttp.Request
(List
(Result
String
{ path : List String
, content : String
}
)
)
}