Skip to content

Commit

Permalink
chore: add some types
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Nov 28, 2024
1 parent 3c92dc0 commit 39847c5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
8 changes: 8 additions & 0 deletions generator/src/basepath-middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// this middleware is only active when (config.base !== '/')

/**
* @import { NextHandleFunction } from "connect";
*/

/**
* @param {string} base
* @returns {NextHandleFunction}
*/
export function baseMiddleware(base) {
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteBaseMiddleware(req, res, next) {
Expand Down
49 changes: 36 additions & 13 deletions generator/src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ import * as globby from "globby";
import { fileURLToPath } from "url";
import { copyFile } from "fs/promises";

/**
* @type {Promise<{ worker: Worker }>[]}
*/
let pool = [];
/**
* @type {(value: unknown) => void}
*/
let pagesReady;
/**
* @type {(reason: unknown) => void}
*/
let pagesErrored;
let pages = new Promise((resolve, reject) => {
pagesReady = resolve;
Expand Down Expand Up @@ -73,7 +82,11 @@ export async function run(options) {
// This is a temporary hack to avoid this warning. elm-pages manages compiling the Elm code without Vite's involvement, so it is external to Vite.
// There is a pending issue to allow having external scripts in Vite, once this issue is fixed we can remove this hack:
// https://github.com/vitejs/vite/issues/3533
if (messages && messages[0] && !messages[0].startsWith(`<script src="/elm.js">`)) {
if (
messages &&
messages[0] &&
!messages[0].startsWith(`<script src="/elm.js">`)
) {
console.info(...messages);
}
};
Expand All @@ -97,13 +110,11 @@ export async function run(options) {
configFile: false,
root: process.cwd(),
base: options.base,
assetsInclude: [
'/elm-pages.js'
],
assetsInclude: ["/elm-pages.js"],
ssr: false,

build: {
manifest: '___vite-manifest___.json',
manifest: "___vite-manifest___.json",
outDir: "dist",
rollupOptions: {
input: "elm-stuff/elm-pages/index.html",
Expand All @@ -126,7 +137,10 @@ export async function run(options) {
fullOutputPath,
withoutExtension
);
const assetManifestPath = path.join(process.cwd(), "dist/___vite-manifest___.json");
const assetManifestPath = path.join(
process.cwd(),
"dist/___vite-manifest___.json"
);
const manifest = JSON.parse(
await fsPromises.readFile(assetManifestPath, { encoding: "utf-8" })
);
Expand Down Expand Up @@ -303,6 +317,8 @@ export async function render(request) {

/**
* @param {string} basePath
* @param {() => void} whenDone
* @returns {Promise<{worker: Worker}>}
*/
function initWorker(basePath, whenDone) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -686,19 +702,26 @@ function _HtmlAsJson_toJson(html) {

await fsPromises.writeFile(
ELM_FILE_PATH().replace(/\.js$/, ".cjs"),
applyScriptPatches(options, elmFileContent
.replace(
/return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
`return ${forceThunksSource}
applyScriptPatches(
options,
elmFileContent
.replace(
/return \$elm\$json\$Json\$Encode\$string\(.REPLACE_ME_WITH_JSON_STRINGIFY.\)/g,
`return ${forceThunksSource}
return _Json_wrap(forceThunks(html));
`
)
.replace(`console.log('App dying')`, "")));
)
.replace(`console.log('App dying')`, "")
)
);
}

function applyScriptPatches(options, input) {
if (options.isScript) {
return input.replace(`_Debug_crash(8, moduleName, region, message)`, "console.error('TODO in module `' + moduleName + '` ' + _Debug_regionToString(region) + '\\n\\n' + message); process.exitCode = 1; debugger; throw 'CRASH!';");
return input.replace(
`_Debug_crash(8, moduleName, region, message)`,
"console.error('TODO in module `' + moduleName + '` ' + _Debug_regionToString(region) + '\\n\\n' + message); process.exitCode = 1; debugger; throw 'CRASH!';"
);
} else {
return input;
}
Expand Down

0 comments on commit 39847c5

Please sign in to comment.