mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-25 04:43:03 +03:00
Run adapter script from build step (if configured).
This commit is contained in:
parent
3e12b48112
commit
25972e5724
@ -1,11 +1,12 @@
|
|||||||
const fs = require("fs");
|
import fs from "fs";
|
||||||
|
|
||||||
async function run({
|
export default async function run({
|
||||||
renderFunctionFilePath,
|
renderFunctionFilePath,
|
||||||
routePatterns,
|
routePatterns,
|
||||||
apiRoutePatterns,
|
apiRoutePatterns,
|
||||||
portsFilePath,
|
portsFilePath,
|
||||||
}) {
|
}) {
|
||||||
|
console.log("Running adapter script");
|
||||||
ensureDirSync("functions/render");
|
ensureDirSync("functions/render");
|
||||||
ensureDirSync("functions/server-render");
|
ensureDirSync("functions/server-render");
|
||||||
|
|
||||||
@ -100,26 +101,6 @@ function isServerSide(route) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
(async function () {
|
|
||||||
try {
|
|
||||||
await run({
|
|
||||||
renderFunctionFilePath: "./elm-stuff/elm-pages/elm.js",
|
|
||||||
routePatterns: JSON.parse(fs.readFileSync("dist/route-patterns.json")),
|
|
||||||
apiRoutePatterns: JSON.parse(fs.readFileSync("dist/api-patterns.json")),
|
|
||||||
portsFilePath: "./.elm-pages/compiled-ports/port-data-source.mjs",
|
|
||||||
});
|
|
||||||
console.log("Success - Adapter script complete");
|
|
||||||
} catch (error) {
|
|
||||||
console.error("ERROR - Adapter script failed");
|
|
||||||
try {
|
|
||||||
console.error(JSON.stringify(error));
|
|
||||||
} catch (parsingError) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {boolean} isOnDemand
|
* @param {boolean} isOnDemand
|
||||||
* @param {string} processedHtml
|
* @param {string} processedHtml
|
@ -1,5 +1,8 @@
|
|||||||
import { defineConfig } from "vite";
|
import { defineConfig } from "vite";
|
||||||
|
|
||||||
|
import adapter from "./adapter.mjs";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
vite: defineConfig({}),
|
vite: defineConfig({}),
|
||||||
|
adapter,
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
[build]
|
[build]
|
||||||
functions = "functions/"
|
functions = "functions/"
|
||||||
publish = "dist/"
|
publish = "dist/"
|
||||||
command = "mkdir bin && export PATH=\"/opt/build/repo/examples/pokedex/bin:$PATH\" && echo $PATH && curl https://static.lamdera.com/bin/linux/lamdera -o bin/lamdera && chmod a+x bin/lamdera && export ELM_HOME=\"$NETLIFY_BUILD_BASE/cache/elm\" && (cd ../../ && npm install --no-optional && npx --no-install elm-tooling install) && npm install && npm run generate:tailwind && npm run generate:graphql && npm run build && node adapter.js && cp secret-note.txt functions/server-render/"
|
command = "mkdir bin && export PATH=\"/opt/build/repo/examples/pokedex/bin:$PATH\" && echo $PATH && curl https://static.lamdera.com/bin/linux/lamdera -o bin/lamdera && chmod a+x bin/lamdera && export ELM_HOME=\"$NETLIFY_BUILD_BASE/cache/elm\" && (cd ../../ && npm install --no-optional && npx --no-install elm-tooling install) && npm install && npm run generate:tailwind && npm run generate:graphql && npm run build && cp secret-note.txt functions/server-render/"
|
||||||
|
|
||||||
[dev]
|
[dev]
|
||||||
command = "npm start"
|
command = "npm start"
|
||||||
|
@ -21,6 +21,7 @@ let pagesReady;
|
|||||||
let pages = new Promise((resolve, reject) => {
|
let pages = new Promise((resolve, reject) => {
|
||||||
pagesReady = resolve;
|
pagesReady = resolve;
|
||||||
});
|
});
|
||||||
|
let activeWorkers = 0;
|
||||||
let buildError = false;
|
let buildError = false;
|
||||||
|
|
||||||
const DIR_PATH = process.cwd();
|
const DIR_PATH = process.cwd();
|
||||||
@ -75,17 +76,17 @@ async function run(options) {
|
|||||||
preRenderHtml.templateHtml()
|
preRenderHtml.templateHtml()
|
||||||
);
|
);
|
||||||
|
|
||||||
const viteConfig = await import(
|
const config = await import(
|
||||||
path.join(process.cwd(), "elm-pages.config.mjs")
|
path.join(process.cwd(), "elm-pages.config.mjs")
|
||||||
)
|
)
|
||||||
.then(async (elmPagesConfig) => {
|
.then(async (elmPagesConfig) => {
|
||||||
return elmPagesConfig.default.vite || {};
|
return elmPagesConfig.default || {};
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.warn("Using default config.", error);
|
console.warn("Using default config.", error);
|
||||||
return {};
|
return {};
|
||||||
});
|
});
|
||||||
console.log("@@@1");
|
const viteConfig = config.vite || {};
|
||||||
|
|
||||||
const buildComplete = build({
|
const buildComplete = build({
|
||||||
configFile: false,
|
configFile: false,
|
||||||
@ -123,7 +124,6 @@ async function run(options) {
|
|||||||
})
|
})
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
global.portsFilePath = Object.keys(result.metafile.outputs)[0];
|
global.portsFilePath = Object.keys(result.metafile.outputs)[0];
|
||||||
console.log("Watching port-data-source...");
|
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
if (
|
if (
|
||||||
@ -151,6 +151,12 @@ async function run(options) {
|
|||||||
await portDataSourceCompiled;
|
await portDataSourceCompiled;
|
||||||
const cliDone = runCli(options);
|
const cliDone = runCli(options);
|
||||||
await cliDone;
|
await cliDone;
|
||||||
|
await runAdapter(
|
||||||
|
config.adapter ||
|
||||||
|
function () {
|
||||||
|
console.log("No adapter configured. Skipping adapter step.");
|
||||||
|
}
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
buildError = true;
|
buildError = true;
|
||||||
@ -175,8 +181,9 @@ async function run(options) {
|
|||||||
/**
|
/**
|
||||||
* @param {string} basePath
|
* @param {string} basePath
|
||||||
*/
|
*/
|
||||||
function initWorker(basePath) {
|
function initWorker(basePath, whenDone) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
activeWorkers += 1;
|
||||||
let newWorker = {
|
let newWorker = {
|
||||||
worker: new Worker(path.join(__dirname, "./render-worker.js"), {
|
worker: new Worker(path.join(__dirname, "./render-worker.js"), {
|
||||||
env: SHARE_ENV,
|
env: SHARE_ENV,
|
||||||
@ -190,9 +197,9 @@ function initWorker(basePath) {
|
|||||||
} else if (message.tag === "error") {
|
} else if (message.tag === "error") {
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
console.error(restoreColorSafe(message.data));
|
console.error(restoreColorSafe(message.data));
|
||||||
buildNextPage(newWorker);
|
buildNextPage(newWorker, whenDone);
|
||||||
} else if (message.tag === "done") {
|
} else if (message.tag === "done") {
|
||||||
buildNextPage(newWorker);
|
buildNextPage(newWorker, whenDone);
|
||||||
} else {
|
} else {
|
||||||
throw `Unhandled tag ${message.tag}`;
|
throw `Unhandled tag ${message.tag}`;
|
||||||
}
|
}
|
||||||
@ -200,7 +207,7 @@ function initWorker(basePath) {
|
|||||||
newWorker.worker.on("error", (error) => {
|
newWorker.worker.on("error", (error) => {
|
||||||
console.error("Unhandled worker exception", error);
|
console.error("Unhandled worker exception", error);
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
buildNextPage(newWorker);
|
buildNextPage(newWorker, whenDone);
|
||||||
});
|
});
|
||||||
resolve(newWorker);
|
resolve(newWorker);
|
||||||
});
|
});
|
||||||
@ -218,7 +225,7 @@ function prepareStaticPathsNew(thread) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildNextPage(thread) {
|
async function buildNextPage(thread, allComplete) {
|
||||||
let nextPage = (await pages).pop();
|
let nextPage = (await pages).pop();
|
||||||
if (nextPage) {
|
if (nextPage) {
|
||||||
thread.worker.postMessage({
|
thread.worker.postMessage({
|
||||||
@ -229,22 +236,33 @@ async function buildNextPage(thread) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
thread.worker.terminate();
|
thread.worker.terminate();
|
||||||
|
activeWorkers -= 1;
|
||||||
|
allComplete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runCli(options) {
|
function runCli(options) {
|
||||||
const cpuCount = os.cpus().length;
|
return new Promise((resolve) => {
|
||||||
console.log("Threads: ", cpuCount);
|
const whenDone = () => {
|
||||||
|
if (activeWorkers === 0) {
|
||||||
|
// wait for the remaining tasks in the pool to complete once the pages queue is emptied
|
||||||
|
Promise.all(pool).then(resolve);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const cpuCount = os.cpus().length;
|
||||||
|
// const cpuCount = 1;
|
||||||
|
console.log("Threads: ", cpuCount);
|
||||||
|
|
||||||
const getPathsWorker = initWorker(options.base);
|
const getPathsWorker = initWorker(options.base, whenDone);
|
||||||
getPathsWorker.then(prepareStaticPathsNew);
|
getPathsWorker.then(prepareStaticPathsNew);
|
||||||
const threadsToCreate = Math.max(1, cpuCount - 1);
|
const threadsToCreate = Math.max(1, cpuCount - 1);
|
||||||
pool.push(getPathsWorker);
|
pool.push(getPathsWorker);
|
||||||
for (let index = 0; index < threadsToCreate - 1; index++) {
|
for (let index = 0; index < threadsToCreate - 1; index++) {
|
||||||
pool.push(initWorker(options.base));
|
pool.push(initWorker(options.base, whenDone));
|
||||||
}
|
}
|
||||||
pool.forEach((threadPromise) => {
|
pool.forEach((threadPromise) => {
|
||||||
threadPromise.then(buildNextPage);
|
threadPromise.then((thread) => buildNextPage(thread, whenDone));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -500,6 +518,33 @@ return forceThunks(html);
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function runAdapter(adaptFn) {
|
||||||
|
try {
|
||||||
|
await adaptFn({
|
||||||
|
renderFunctionFilePath: "./elm-stuff/elm-pages/elm.js",
|
||||||
|
routePatterns: JSON.parse(
|
||||||
|
await fsPromises.readFile(
|
||||||
|
path.join(process.cwd(), "./dist/route-patterns.json"),
|
||||||
|
"utf-8"
|
||||||
|
)
|
||||||
|
),
|
||||||
|
apiRoutePatterns: JSON.parse(
|
||||||
|
await fsPromises.readFile("./dist/api-patterns.json", "utf-8")
|
||||||
|
),
|
||||||
|
portsFilePath: "./.elm-pages/compiled-ports/port-data-source.mjs",
|
||||||
|
});
|
||||||
|
console.log("Success - Adapter script complete");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("ERROR - Adapter script failed");
|
||||||
|
try {
|
||||||
|
console.error(JSON.stringify(error));
|
||||||
|
} catch (parsingError) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @typedef { { route : string; contentJson : string; head : SeoTag[]; html: string; body: string; } } FromElm */
|
/** @typedef { { route : string; contentJson : string; head : SeoTag[]; html: string; body: string; } } FromElm */
|
||||||
/** @typedef {HeadTag | JsonLdTag} SeoTag */
|
/** @typedef {HeadTag | JsonLdTag} SeoTag */
|
||||||
/** @typedef {{ name: string; attributes: string[][]; type: 'head' }} HeadTag */
|
/** @typedef {{ name: string; attributes: string[][]; type: 'head' }} HeadTag */
|
||||||
|
Loading…
Reference in New Issue
Block a user