Skip to content

Commit f0832e3

Browse files
committed
Fix building and serving
Removed a few unneeded dependencies, created two separate files for different building actions, and serving now works
1 parent 43eb86f commit f0832e3

34 files changed

+2231
-10091
lines changed

.babelrc

-3
This file was deleted.

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
node_modules
2-
./bin/node_modules
3-
./static/dynamic/
2+
dist
43
.DS_Store
54
**/.DS_Store

dist/dynamic.client.js

-30
This file was deleted.

dist/dynamic.client.js.map

-7
This file was deleted.

dist/dynamic.handler.js

-30
This file was deleted.

dist/dynamic.handler.js.map

-7
This file was deleted.

dist/dynamic.html.js

-9
This file was deleted.

dist/dynamic.html.js.map

-7
This file was deleted.

dist/dynamic.worker.js

-30
This file was deleted.

dist/dynamic.worker.js.map

-7
This file was deleted.

esbuild.bundle.js

-73
This file was deleted.

esbuild.dev.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as esbuild from "esbuild";
2+
import { copyFile } from "fs/promises";
3+
4+
console.time("esbuild");
5+
6+
const worker = await esbuild.context({
7+
entryPoints: ["lib/worker/index.ts"],
8+
bundle: true,
9+
outfile: "dist/dynamic.worker.js",
10+
format: "iife",
11+
minify: true,
12+
platform: "browser",
13+
sourcemap: true,
14+
target: ["es2020"],
15+
plugins: [],
16+
metafile: true,
17+
});
18+
19+
worker.watch();
20+
21+
const handler = await esbuild.context({
22+
entryPoints: ["lib/handler/index.ts"],
23+
bundle: true,
24+
outfile: "dist/dynamic.handler.js",
25+
format: "iife",
26+
minify: true,
27+
platform: "browser",
28+
sourcemap: true,
29+
target: ["es2020"],
30+
plugins: [],
31+
metafile: true,
32+
});
33+
34+
handler.watch();
35+
36+
const client = await esbuild.context({
37+
entryPoints: ["lib/client/index.ts"],
38+
bundle: true,
39+
outfile: "dist/dynamic.client.js",
40+
format: "iife",
41+
minify: true,
42+
platform: "browser",
43+
sourcemap: true,
44+
target: ["es2020"],
45+
plugins: [],
46+
metafile: true,
47+
});
48+
49+
client.watch();
50+
51+
const html = await esbuild.context({
52+
entryPoints: ["lib/html/index.ts"],
53+
bundle: true,
54+
outfile: "dist/dynamic.html.js",
55+
format: "iife",
56+
minify: true,
57+
platform: "browser",
58+
sourcemap: true,
59+
target: ["es2020"],
60+
plugins: [],
61+
metafile: true,
62+
});
63+
64+
html.watch();
65+
66+
await copyFile("./lib/dynamic.config.js", "./dist/dynamic.config.js");
67+
68+
console.log(await esbuild.analyzeMetafile((await worker.rebuild()).metafile));
69+
70+
console.timeEnd("esbuild");

esbuild.prod.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as esbuild from "esbuild";
2+
import { copyFile } from "fs/promises";
3+
4+
console.time("esbuild");
5+
6+
const worker = await esbuild.build({
7+
entryPoints: ["lib/worker/index.ts"],
8+
bundle: true,
9+
outfile: "dist/dynamic.worker.js",
10+
format: "iife",
11+
minify: true,
12+
platform: "browser",
13+
sourcemap: true,
14+
target: ["es2020"],
15+
plugins: [],
16+
metafile: true,
17+
});
18+
19+
const handler = await esbuild.build({
20+
entryPoints: ["lib/handler/index.ts"],
21+
bundle: true,
22+
outfile: "dist/dynamic.handler.js",
23+
format: "iife",
24+
minify: true,
25+
platform: "browser",
26+
sourcemap: true,
27+
target: ["es2020"],
28+
plugins: [],
29+
metafile: true,
30+
});
31+
32+
const client = await esbuild.build({
33+
entryPoints: ["lib/client/index.ts"],
34+
bundle: true,
35+
outfile: "dist/dynamic.client.js",
36+
format: "iife",
37+
minify: true,
38+
platform: "browser",
39+
sourcemap: true,
40+
target: ["es2020"],
41+
plugins: [],
42+
metafile: true,
43+
});
44+
45+
const html = await esbuild.build({
46+
entryPoints: ["lib/html/index.ts"],
47+
bundle: true,
48+
outfile: "dist/dynamic.html.js",
49+
format: "iife",
50+
minify: true,
51+
platform: "browser",
52+
sourcemap: true,
53+
target: ["es2020"],
54+
plugins: [],
55+
metafile: true,
56+
});
57+
58+
await copyFile("./lib/dynamic.config.js", "./dist/dynamic.config.js");
59+
60+
console.log(await esbuild.analyzeMetafile((worker.metafile)));
61+
62+
console.timeEnd("esbuild");

index.js

+47-40
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
1+
import Fastify from 'fastify';
2+
import fastifyStatic from '@fastify/static';
13
import { createBareServer } from '@tomphttp/bare-server-node';
2-
import http from 'http';
3-
import { dirname, join } from 'path';
4-
import { fileURLToPath } from 'url';
5-
import nodeStatic from 'node-static';
4+
import { createServer } from 'http';
65
import chalk from 'chalk';
76
import open from 'open';
8-
import { readFile } from 'fs/promises';
7+
import { existsSync } from 'fs';
8+
import { join, dirname } from 'path';
9+
import { fileURLToPath } from 'url';
10+
11+
if (!existsSync("./dist")) await import("./esbuild.prod.js");
12+
13+
const __dirname = dirname(fileURLToPath(import.meta.url));
914

10-
const __filename = fileURLToPath(import.meta.url);
11-
const __dirname = dirname(__filename);
1215
const port = process.env.PORT || 3000;
1316
const _v = process.env.npm_package_version;
1417

1518
const bare = createBareServer('/bare/');
16-
const serve = new nodeStatic.Server('static/');
17-
18-
const server = http.createServer();
19-
20-
server.on('request', (req, res) => {
21-
if (req.url === '/version') {
22-
// Handle version endpoint
23-
const versionResponse = {
24-
version: _v,
25-
};
26-
res.writeHead(200, { 'Content-Type': 'application/json' });
27-
res.end(JSON.stringify(versionResponse));
28-
} else if (bare.shouldRoute(req)) {
29-
bare.routeRequest(req, res);
30-
} else {
31-
serve.serve(req, res);
32-
}
19+
const serverFactory = (handler, opts) => {
20+
return createServer()
21+
.on("request", (req, res) => {
22+
if (req.url === "/version") {
23+
const versionResponse = {
24+
version: _v,
25+
};
26+
res.writeHead(200, { 'Content-Type': 'application/json' });
27+
res.end(JSON.stringify(versionResponse));
28+
} else if (bare.shouldRoute(req)) {
29+
bare.routeRequest(req, res);
30+
} else {
31+
handler(req, res)
32+
}
33+
}).on("upgrade", (req, socket, head) => {
34+
if (bare.shouldRoute(req)) {
35+
bare.routeUpgrade(req, socket, head);
36+
} else {
37+
socket.end();
38+
}
39+
});
40+
}
41+
const fastify = Fastify({ serverFactory });
42+
fastify.register(fastifyStatic, {
43+
root: join(__dirname, "./static"),
44+
decorateReply: false
3345
});
34-
35-
server.on('upgrade', (req, socket, head) => {
36-
if (bare.shouldRoute(req, socket, head)) {
37-
bare.routeUpgrade(req, socket, head);
38-
} else {
39-
socket.end();
40-
}
46+
fastify.register(fastifyStatic, {
47+
root: join(__dirname, "./dist"),
48+
prefix: "/dynamic/",
49+
decorateReply: false
4150
});
4251

4352
const URL = `http://localhost:${port}/`;
44-
server.listen(port, () => {
53+
fastify.listen({ port }, async () => {
4554
console.log(chalk.bold('Thanks for using Dynamic!'), chalk.red(`Please notice that ${chalk.red.bold('dynamic is currently in public BETA')}. please report all issues to the GitHub page. `))
4655
console.log(chalk.green.bold(`Dynamic ${_v} `) + "live at port " + chalk.bold.green(port));
47-
(async () => {
48-
try {
49-
await open(URL);
50-
} catch (ERR) {
51-
console.error(ERR);
52-
}
53-
})();
54-
});
56+
try {
57+
await open(URL);
58+
} catch (ERR) {
59+
console.error(ERR);
60+
}
61+
});
File renamed without changes.

lib/global/codec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as b64 from '@dynamic-pkg/base64';
2-
31
const xor = {
42
encode: (str: string | undefined, key: number = 2) => {
53
if (!str) return str;
@@ -35,12 +33,12 @@ const base64 = {
3533
encode: (str: string | undefined) => {
3634
if (!str) return str;
3735

38-
return decodeURIComponent(b64.encode(str));
36+
return decodeURIComponent(btoa(str));
3937
},
4038
decode: (str: string | undefined) => {
4139
if (!str) return str;
4240

43-
return b64.decode(str);
41+
return atob(str);
4442
}
4543
}
4644

lib/global/modules.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
import mime from '@dynamic-pkg/mime';
22
import * as path from 'path-browserify';
33
import * as idb from 'idb';
4-
import * as base64 from '@dynamic-pkg/base64';
54
import { parse } from '@dynamic-pkg/acorn';
65
import { BareClient, createBareClient } from '@tomphttp/bare-client';
76
import * as cookie from '@dynamic-pkg/cookie';
8-
import * as setCookieParser from 'set-cookie-parser'
7+
import { parse as cookieParser } from 'set-cookie-parser'
98
import { generate } from '@dynamic-pkg/astring';
10-
//import * as Bowser from 'bowser';
11-
//import mutation from '@dynamic-pkg/mutation';
129

1310
class DynamicModules {
1411
mime = mime;
1512
idb = idb;
1613
path = path;
1714
acorn = { parse };
1815
bare = {createBareClient, BareClient};
19-
base64 = base64;
16+
base64 = { encode: btoa, decode: atob };
2017
estree = { generate };
2118
cookie = {...cookie, serialize: (...args: any) => { try {return cookie.serialize.apply({}, args)} catch(e) {console.log(e);}}};
22-
setCookieParser = setCookieParser.parse;
23-
//bowser = Bowser;
24-
19+
setCookieParser = cookieParser;
20+
2521
ctx;
2622

2723
constructor(ctx:any) {

0 commit comments

Comments
 (0)