Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous Internal Changes #499

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Empty file.
5 changes: 0 additions & 5 deletions generator/src/hello.ts

This file was deleted.

8 changes: 4 additions & 4 deletions generator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"types": ["node"],
Copy link
Author

@lishaduck lishaduck Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevented @types packages from working.

"moduleResolution": "node",
"module": "NodeNext",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The node value is deprecated.

"isolatedModules": true,
"checkJs": true,
"allowJs": true,
"noEmit": true,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, I get an error about it can't output .js files because they're already authored in js.

"strict": true,
"resolveJsonModule": true,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't look like this was needed anymore, and latest ts5.7 makes backcompat a pain, so I removed it.

"lib": ["es2015", "es2017.object", "esnext", "dom"]
"lib": ["esnext", "dom"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting an error about bad targets & stuff, and this seemed to resolve it. The es* libs implicitly include all the previous versions.

},
"exclude": ["node_modules", "generated"]
}
12 changes: 0 additions & 12 deletions jsconfig.json

This file was deleted.

44 changes: 44 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"type": "module",
"version": "3.0.17",
"homepage": "https://elm-pages.com",
"moduleResolution": "node",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a thing.

"description": "Type-safe static sites, written in pure elm with your own custom elm-markup syntax.",
"description": "Hybrid Elm framework with full-stack and static routes.",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just took it from the repo description.

"main": "index.js",
"scripts": {
"start": "cd examples/end-to-end && npm i && npx elm-pages dev",
Expand Down Expand Up @@ -50,12 +49,17 @@
"which": "^5.0.0"
},
"devDependencies": {
"@types/busboy": "~1.5.4",
"@types/connect": "~3.4.38",
"@types/cookie-signature": "~1.1.2",
"@types/cross-spawn": "^6.0.6",
"@types/fs-extra": "^11.0.4",
"@types/jsesc": "~3.0.3",
"@types/make-fetch-happen": "^10.0.4",
"@types/micromatch": "^4.0.9",
"@types/node": "^22.10.0",
"@types/serve-static": "^1.15.7",
"@types/which": "~3.0.4",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS was complaining about the lack of types for which, so I ran typesync.
Note that it uses a tilde, as DefinitelyTyped keeps majors and minors in sync with the upstream. Now, there aren't super recent types for which, so it's a bit pointless, but it just did it automatically.

"cypress": "^13.16.0",
"elm-codegen": "^0.6.1",
"elm-optimize-level-2": "^0.3.5",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"strict": true,
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext"
"moduleResolution": "NodeNext",
"checkJs": true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that you want ts checking of js files, given the jsconfig.

}
}
Loading