Run esbuild to compile ports file from build command.

This commit is contained in:
Dillon Kearns 2022-03-02 14:35:48 -08:00
parent 6a006cd9bc
commit f572ee7218
2 changed files with 41 additions and 4 deletions

View File

@ -2,9 +2,12 @@ module Page.Index exposing (Data, Model, Msg, page)
import DataSource exposing (DataSource) import DataSource exposing (DataSource)
import DataSource.File import DataSource.File
import DataSource.Port
import Head import Head
import Head.Seo as Seo import Head.Seo as Seo
import Html.Styled exposing (text) import Html.Styled exposing (div, text)
import Json.Decode as Decode
import Json.Encode as Encode
import Page exposing (Page, PageWithState, StaticPayload) import Page exposing (Page, PageWithState, StaticPayload)
import Pages.PageUrl exposing (PageUrl) import Pages.PageUrl exposing (PageUrl)
import Pages.Url import Pages.Url
@ -34,12 +37,16 @@ page =
type alias Data = type alias Data =
String { greeting : String
, portGreeting : String
}
data : DataSource Data data : DataSource Data
data = data =
DataSource.File.rawFile "greeting.txt" DataSource.map2 Data
(DataSource.File.rawFile "greeting.txt")
(DataSource.Port.get "hello" (Encode.string "Jane") Decode.string)
head : head :
@ -71,6 +78,7 @@ view maybeUrl sharedModel static =
{ title = "Index page" { title = "Index page"
, body = , body =
[ text "This is the index page." [ text "This is the index page."
, text <| "Greeting: " ++ static.data , div [] [ text <| "Greeting: " ++ static.data.greeting ]
, div [] [ text <| "Greeting: " ++ static.data.portGreeting ]
] ]
} }

View File

@ -14,6 +14,7 @@ const { generateClientFolder } = require("./codegen.js");
const which = require("which"); const which = require("which");
const { build } = require("vite"); const { build } = require("vite");
const preRenderHtml = require("./pre-render-html.js"); const preRenderHtml = require("./pre-render-html.js");
const esbuild = require("esbuild");
let pool = []; let pool = [];
let pagesReady; let pagesReady;
@ -107,6 +108,31 @@ async function run(options) {
"dist/template.html" "dist/template.html"
); );
const portDataSourceCompiled = esbuild
.build({
entryPoints: ["./port-data-source"],
entryNames: "[dir]/[name]-[hash]",
outdir: ".elm-pages/compiled-ports",
assetNames: "[name]-[hash]",
chunkNames: "chunks/[name]-[hash]",
outExtension: { ".js": ".mjs" },
metafile: true,
bundle: false,
watch: false,
})
.then((result) => {
global.portsFilePath = Object.keys(result.metafile.outputs)[0];
console.log("Watching port-data-source...");
})
.catch((error) => {
console.error("Failed to start port-data-source watcher", error);
});
// TODO run esbuild for ports file
// TODO extract common code for compiling ports file?
// TODO set global.portsFilePath
XMLHttpRequest = {}; XMLHttpRequest = {};
const compileCli = compileCliApp(options); const compileCli = compileCliApp(options);
try { try {
@ -116,6 +142,7 @@ async function run(options) {
console.error(cliError); console.error(cliError);
process.exit(1); process.exit(1);
} }
await portDataSourceCompiled;
const cliDone = runCli(options); const cliDone = runCli(options);
await cliDone; await cliDone;
} catch (error) { } catch (error) {
@ -178,6 +205,7 @@ function initWorker(basePath) {
*/ */
function prepareStaticPathsNew(thread) { function prepareStaticPathsNew(thread) {
thread.worker.postMessage({ thread.worker.postMessage({
portsFilePath: global.portsFilePath,
mode: "build", mode: "build",
tag: "render", tag: "render",
pathname: "/all-paths.json", pathname: "/all-paths.json",
@ -188,6 +216,7 @@ async function buildNextPage(thread) {
let nextPage = (await pages).pop(); let nextPage = (await pages).pop();
if (nextPage) { if (nextPage) {
thread.worker.postMessage({ thread.worker.postMessage({
portsFilePath: global.portsFilePath,
mode: "build", mode: "build",
tag: "render", tag: "render",
pathname: nextPage, pathname: nextPage,