Move to esbuild

master
Kuba Orlik 3 years ago
parent b24d6d1a0b
commit c64a950746

@ -0,0 +1,40 @@
const { build } = require("esbuild");
const { sassPlugin } = require("esbuild-sass-plugin");
const glob = require("tiny-glob");
const watch = process.argv.at(-1) === "--watch";
(async () => {
let entryPoints = Object.fromEntries(
(await glob("./src/back/**/*.ts")).map((e) => [
e.replace(/\.ts$/, ""),
e,
])
);
build({
entryPoints,
sourcemap: true,
outdir: "./dist",
logLevel: "info",
platform: "node",
watch,
target: "node16",
format: "cjs",
});
build({
entryPoints: ["./src/front/main.scss"],
sourcemap: true,
outfile: "./public/dist/style.css",
logLevel: "info",
watch,
plugins: [sassPlugin()],
});
build({
entryPoints: ["./src/front/index.ts"],
sourcemap: true,
outfile: "./public/dist/bundle.js",
logLevel: "info",
bundle: true,
watch,
});
})();

5716
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -2,17 +2,15 @@
"name": "sealious-playground",
"version": "1.0.1",
"description": "",
"main": "./dist/index.js",
"main": "./dist/src/back/index.js",
"scripts": {
"start": "docker-compose up -d db && node .",
"test-cmd": "node test.js",
"test": "./npm.sh run test-cmd -- ",
"build:back": "tsc -p src/back",
"build:front": "webpack",
"build": "npm run build:back && npm run build:front",
"watch:back": "tsc --watch -p src/back",
"watch:front": "webpack --watch",
"watch": "multiple-scripts-tmux \"npm run watch:back\" \"SEALIOUS_PORT=$SEALIOUS_PORT SEALIOUS_BASE_URL=$SEALIOUS_BASE_URL nodemon --enable-source-maps .\" \"npm run watch:front\"",
"typecheck:back": "tsc --noEmit -p src/back",
"typecheck:front": "tsc --noEmit -p src/front",
"build": "node ./esbuild.js",
"watch": "multiple-scripts-tmux \"npm run typecheck:back --watch\" \"SEALIOUS_PORT=$SEALIOUS_PORT SEALIOUS_BASE_URL=$SEALIOUS_BASE_URL nodemon --enable-source-maps .\" \"npm run build -- --watch\" \"npm run typecheck:front --watch\" ",
"test-reports": "npm run build && rm -fr .xunit coverage && docker-compose up -d db mailcatcher && npm run test -- --cover --test-report",
"cover-html": "npm run test-reports -- --cover-html && xdg-open coverage/lcov-report/index.html"
},
@ -20,8 +18,9 @@
"license": "ISC",
"dependencies": {
"@babel/core": "^7.12.10",
"@hotwired/turbo": "^7.0.0-beta.3",
"@hotwired/turbo": "^7.1.0",
"@koa/router": "^10.0.0",
"esbuild-node-tsc": "^1.8.2",
"multiple-scripts-tmux": "^1.0.4",
"nodemon": "^2.0.7",
"sealious": "^0.13.52",
@ -31,8 +30,8 @@
"devDependencies": {
"@sealcode/ansi-html-stream": "^1.0.1",
"@types/koa__router": "^8.0.4",
"babel-loader": "^8.2.2",
"concurrently": "^5.3.0",
"esbuild": "^0.14.10",
"esbuild-sass-plugin": "^2.0.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
@ -42,9 +41,8 @@
"mri": "^1.1.6",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"tiny-glob": "^0.2.9",
"ts-loader": "^8.0.14",
"typescript": "^4.1.3",
"webpack": "^5.12.2",
"webpack-cli": "^4.3.1"
"typescript": "^4.1.3"
}
}

@ -8,9 +8,9 @@ export default function html(ctx: BaseContext, body: Templatable): Readable {
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="/dist/bundle.js"></script>
<script async src="/dist/bundle.js"></script>
<link href="/dist/style.css" rel="stylesheet" type="text/css" />
</head>
<link href="/style.css" rel="stylesheet" type="text/css" />
${body}
</html>`;
}

@ -1,6 +1,7 @@
import _locreq from "locreq";
import Sealious from "sealious";
import TheApp from "./app";
import { mainRouter } from "./routes";
const locreq = _locreq(__dirname);
declare module "koa" {
@ -11,15 +12,16 @@ declare module "koa" {
}
}
export const app = new TheApp();
void app
.start()
.then(() => {
const app = new TheApp();
app.start()
.then(async () => {
//populate scripts go here
if (process.env.SEALIOUS_SANITY === "true") {
console.log("Exiting with error code 0");
process.exit(0);
}
mainRouter(app.HTTPServer.router);
})
.catch((error) => {
console.error(error);
@ -29,7 +31,4 @@ void app
}
});
export const router = app.HTTPServer.router;
require("./routes/index");
app.HTTPServer.addStaticRoute("/", locreq.resolve("public"));

@ -1,25 +1,14 @@
import Router from "@koa/router";
import { Middlewares } from "sealious";
import html from "../html";
import { NewTask, TaskList } from "../views/tasks";
import { BaseContext } from "koa";
import { Readable } from "stream";
import { tempstream } from "tempstream";
import { router } from "..";
import { loginRouter } from "./login/index.js";
import { MainView } from "./main-view.js";
import { tasksRouter } from "./tasks/index.js";
export function MainView(ctx: BaseContext): Readable {
return html(
ctx,
/* HTML */ tempstream` <title>My ToDo App</title>
<body>
<h1>My ToDo App</h1>
${TaskList(ctx.$context)} ${NewTask()}
</body>`
);
}
export const mainRouter = (router: Router): void => {
router.get("/", Middlewares.extractContext(), async (ctx) => {
ctx.body = MainView(ctx);
});
router.get("/", Middlewares.extractContext(), async (ctx) => {
ctx.body = MainView(ctx);
});
require("./login/index");
require("./tasks/index");
loginRouter(router);
tasksRouter(router);
};

@ -1,36 +1,41 @@
import Router from "@koa/router";
import { Middlewares } from "sealious";
import { router } from "../..";
import html from "../../html";
router.get("/login", Middlewares.extractContext(), async (ctx) => {
ctx.body = html(ctx, LoginForm());
});
export const loginRouter = (router: Router): void => {
router.get("/login", Middlewares.extractContext(), async (ctx) => {
ctx.body = html(ctx, LoginForm());
});
router.post(
"/login",
Middlewares.extractContext(),
Middlewares.parseBody(),
async (ctx) => {
try {
const session_id = await ctx.$app.collections.sessions.login(
ctx.$body.username as string,
ctx.$body.password as string
);
ctx.cookies.set("sealious-session", session_id, {
maxAge: 1000 * 60 * 60 * 24 * 7,
secure: ctx.request.protocol === "https",
overwrite: true,
});
ctx.redirect("/user");
} catch (e) {
ctx.status = 422;
ctx.body = html(
ctx,
LoginForm(ctx.$body.username as string, (e as Error).message)
);
router.post(
"/login",
Middlewares.extractContext(),
Middlewares.parseBody(),
async (ctx) => {
try {
const session_id = await ctx.$app.collections.sessions.login(
ctx.$body.username as string,
ctx.$body.password as string
);
ctx.cookies.set("sealious-session", session_id, {
maxAge: 1000 * 60 * 60 * 24 * 7,
secure: ctx.request.protocol === "https",
overwrite: true,
});
ctx.redirect("/user");
} catch (e) {
ctx.status = 422;
ctx.body = html(
ctx,
LoginForm(
ctx.$body.username as string,
(e as Error).message
)
);
}
}
}
);
);
};
function LoginForm(username = "", error_message?: string): string {
if (error_message) {

@ -0,0 +1,17 @@
import html from "../html";
import { BaseContext } from "koa";
import { Readable } from "stream";
import { tempstream } from "tempstream";
import { NewTask, TaskList } from "../views/tasks";
export function MainView(ctx: BaseContext): Readable {
return html(
ctx,
tempstream/* HTML */ ` <title>My Own ToDo App</title>
<body>
<h1>My ToDo App (with esbuild!)</h1>
${TaskList(ctx.$context)} ${NewTask()}
</body>`
);
}

@ -1,27 +1,33 @@
import Router from "@koa/router";
import { Middlewares } from "sealious";
import { MainView } from "..";
import { router } from "../..";
import { MainView } from "../main-view";
router.post(
"/tasks",
Middlewares.extractContext(),
Middlewares.parseBody(),
async (ctx) => {
await ctx.$app.collections.tasks
.make({
title: ctx.$body.title as string,
done: false,
})
.save(ctx.$context);
ctx.body = MainView(ctx);
}
);
export const tasksRouter = (router: Router): void => {
router.post(
"/tasks",
Middlewares.extractContext(),
Middlewares.parseBody(),
async (ctx) => {
await ctx.$app.collections.tasks
.make({
title: ctx.$body.title as string,
done: false,
})
.save(ctx.$context);
ctx.body = MainView(ctx);
}
);
router.delete("/tasks/:task_id", Middlewares.extractContext(), async (ctx) => {
const task = await ctx.$app.collections.tasks.getByID(
ctx.$context,
ctx.params.task_id
router.delete(
"/tasks/:task_id",
Middlewares.extractContext(),
async (ctx) => {
const task = await ctx.$app.collections.tasks.getByID(
ctx.$context,
ctx.params.task_id
);
await task.remove(ctx.$context);
ctx.body = MainView(ctx);
}
);
await task.remove(ctx.$context);
ctx.body = MainView(ctx);
});
};

@ -25,5 +25,4 @@ body {
padding: 0;
line-height: 0;
padding: 0.5rem;
margin-left: 0.5rem;
}
Loading…
Cancel
Save