@titanpl/packet 2.0.1 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/index.js +83 -83
- package/js/titan/builder.js +50 -50
- package/js/titan/bundle.js +164 -164
- package/js/titan/dev.js +53 -19
- package/js/titan/error-box.js +277 -277
- package/package.json +1 -1
- package/ts/titan/bundle.js +227 -227
- package/ts/titan/dev.js +53 -20
- package/ts/titan/error-box.js +277 -277
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# @titanpl/packet
|
|
2
|
-
|
|
3
|
-
The internal bundler and module packer for Titan Planet.
|
|
4
|
-
|
|
5
|
-
## What it works (What it does)
|
|
6
|
-
This library parses all your app routes, components, middleware, and logic paths, then packages them into a static `build` output layer consisting of metadata files and JavaScript assets.
|
|
7
|
-
|
|
8
|
-
## How it works
|
|
9
|
-
This package is triggered whenever you run `titan init` or `titan build` from the `@titanpl/cli`. It hooks into tools like `esbuild` to optimize your scripts efficiently. It strips unnecessary pieces out so that only explicit route actions make their way aggressively over to the Titan runtime engine.
|
|
10
|
-
|
|
11
|
-
**Important Note:** Currently, Titan Planet and its entire package ecosystem are only for Windows. The Linux version is in development (dev only) for the new architecture and will be launched later.
|
|
1
|
+
# @titanpl/packet
|
|
2
|
+
|
|
3
|
+
The internal bundler and module packer for Titan Planet.
|
|
4
|
+
|
|
5
|
+
## What it works (What it does)
|
|
6
|
+
This library parses all your app routes, components, middleware, and logic paths, then packages them into a static `build` output layer consisting of metadata files and JavaScript assets.
|
|
7
|
+
|
|
8
|
+
## How it works
|
|
9
|
+
This package is triggered whenever you run `titan init` or `titan build` from the `@titanpl/cli`. It hooks into tools like `esbuild` to optimize your scripts efficiently. It strips unnecessary pieces out so that only explicit route actions make their way aggressively over to the Titan runtime engine.
|
|
10
|
+
|
|
11
|
+
**Important Note:** Currently, Titan Planet and its entire package ecosystem are only for Windows. The Linux version is in development (dev only) for the new architecture and will be launched later.
|
package/index.js
CHANGED
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Get project type (js or ts)
|
|
6
|
-
*/
|
|
7
|
-
function getProjectType(root) {
|
|
8
|
-
try {
|
|
9
|
-
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
|
10
|
-
if (pkg.titan && pkg.titan.template && pkg.titan.template.includes("ts")) return "ts";
|
|
11
|
-
} catch (e) { }
|
|
12
|
-
|
|
13
|
-
if (fs.existsSync(path.join(root, "tsconfig.json"))) return "ts";
|
|
14
|
-
return "js";
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Ensure dist directory exists
|
|
19
|
-
*/
|
|
20
|
-
function ensureDist(root) {
|
|
21
|
-
const dist = path.join(root, "dist");
|
|
22
|
-
const actions = path.join(dist, "actions");
|
|
23
|
-
|
|
24
|
-
if (!fs.existsSync(dist)) {
|
|
25
|
-
fs.mkdirSync(dist, { recursive: true });
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (!fs.existsSync(actions)) {
|
|
29
|
-
fs.mkdirSync(actions, { recursive: true });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return dist;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Production build
|
|
37
|
-
*/
|
|
38
|
-
export async function build(root = process.cwd()) {
|
|
39
|
-
const type = getProjectType(root);
|
|
40
|
-
const { bundle: bundleActions } = await import(`./${type}/titan/bundle.js`);
|
|
41
|
-
const { buildMetadata } = await import(`./${type}/titan/builder.js`);
|
|
42
|
-
|
|
43
|
-
const dist = ensureDist(root);
|
|
44
|
-
|
|
45
|
-
await buildMetadata(root, dist);
|
|
46
|
-
await bundleActions({
|
|
47
|
-
root,
|
|
48
|
-
outDir: dist,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
return dist;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Dev mode build
|
|
56
|
-
*/
|
|
57
|
-
export async function dev(options = {}) {
|
|
58
|
-
const root = typeof options === 'string' ? options : (options.root || process.cwd());
|
|
59
|
-
const type = getProjectType(root);
|
|
60
|
-
const { dev: devServer } = await import(`./${type}/titan/dev.js`);
|
|
61
|
-
|
|
62
|
-
const dist = ensureDist(root);
|
|
63
|
-
|
|
64
|
-
await devServer({
|
|
65
|
-
root,
|
|
66
|
-
outDir: dist,
|
|
67
|
-
onRebuild: options.onRebuild
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
return dist;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Direct export of current project tools
|
|
75
|
-
*/
|
|
76
|
-
export async function getTools(root = process.cwd()) {
|
|
77
|
-
const type = getProjectType(root);
|
|
78
|
-
const bundleModule = await import(`./${type}/titan/bundle.js`);
|
|
79
|
-
const builderModule = await import(`./${type}/titan/builder.js`);
|
|
80
|
-
return {
|
|
81
|
-
bundleActions: bundleModule.bundle,
|
|
82
|
-
buildMetadata: builderModule.buildMetadata
|
|
83
|
-
};
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get project type (js or ts)
|
|
6
|
+
*/
|
|
7
|
+
function getProjectType(root) {
|
|
8
|
+
try {
|
|
9
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
|
10
|
+
if (pkg.titan && pkg.titan.template && pkg.titan.template.includes("ts")) return "ts";
|
|
11
|
+
} catch (e) { }
|
|
12
|
+
|
|
13
|
+
if (fs.existsSync(path.join(root, "tsconfig.json"))) return "ts";
|
|
14
|
+
return "js";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Ensure dist directory exists
|
|
19
|
+
*/
|
|
20
|
+
function ensureDist(root) {
|
|
21
|
+
const dist = path.join(root, "dist");
|
|
22
|
+
const actions = path.join(dist, "actions");
|
|
23
|
+
|
|
24
|
+
if (!fs.existsSync(dist)) {
|
|
25
|
+
fs.mkdirSync(dist, { recursive: true });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!fs.existsSync(actions)) {
|
|
29
|
+
fs.mkdirSync(actions, { recursive: true });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return dist;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Production build
|
|
37
|
+
*/
|
|
38
|
+
export async function build(root = process.cwd()) {
|
|
39
|
+
const type = getProjectType(root);
|
|
40
|
+
const { bundle: bundleActions } = await import(`./${type}/titan/bundle.js`);
|
|
41
|
+
const { buildMetadata } = await import(`./${type}/titan/builder.js`);
|
|
42
|
+
|
|
43
|
+
const dist = ensureDist(root);
|
|
44
|
+
|
|
45
|
+
await buildMetadata(root, dist);
|
|
46
|
+
await bundleActions({
|
|
47
|
+
root,
|
|
48
|
+
outDir: dist,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return dist;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Dev mode build
|
|
56
|
+
*/
|
|
57
|
+
export async function dev(options = {}) {
|
|
58
|
+
const root = typeof options === 'string' ? options : (options.root || process.cwd());
|
|
59
|
+
const type = getProjectType(root);
|
|
60
|
+
const { dev: devServer } = await import(`./${type}/titan/dev.js`);
|
|
61
|
+
|
|
62
|
+
const dist = ensureDist(root);
|
|
63
|
+
|
|
64
|
+
await devServer({
|
|
65
|
+
root,
|
|
66
|
+
outDir: dist,
|
|
67
|
+
onRebuild: options.onRebuild
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return dist;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Direct export of current project tools
|
|
75
|
+
*/
|
|
76
|
+
export async function getTools(root = process.cwd()) {
|
|
77
|
+
const type = getProjectType(root);
|
|
78
|
+
const bundleModule = await import(`./${type}/titan/bundle.js`);
|
|
79
|
+
const builderModule = await import(`./${type}/titan/builder.js`);
|
|
80
|
+
return {
|
|
81
|
+
bundleActions: bundleModule.bundle,
|
|
82
|
+
buildMetadata: builderModule.buildMetadata
|
|
83
|
+
};
|
|
84
84
|
}
|
package/js/titan/builder.js
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { pathToFileURL } from "url";
|
|
4
|
-
|
|
5
|
-
export async function buildMetadata(root, dist) {
|
|
6
|
-
const appDir = path.join(root, "app");
|
|
7
|
-
const appFile = "app.js";
|
|
8
|
-
const appPath = path.join(appDir, appFile);
|
|
9
|
-
|
|
10
|
-
if (!fs.existsSync(appPath)) {
|
|
11
|
-
console.error(`\x1b[31m❌ app/${appFile} not found.\x1b[0m`);
|
|
12
|
-
process.exit(1);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
const appUrl = pathToFileURL(appPath).href;
|
|
17
|
-
const cacheBuster = `?t=${Date.now()}`;
|
|
18
|
-
await import(appUrl + cacheBuster);
|
|
19
|
-
|
|
20
|
-
const config = globalThis.__TITAN_CONFIG__ || { port: 3000, threads: 4, stack_mb: 8 };
|
|
21
|
-
const routes = globalThis.__TITAN_ROUTES_MAP__ || {};
|
|
22
|
-
const dynamicRoutes = globalThis.__TITAN_DYNAMIC_ROUTES__ || {};
|
|
23
|
-
const actionMap = globalThis.__TITAN_ACTION_MAP__ || {};
|
|
24
|
-
|
|
25
|
-
const routesPath = path.join(dist, "routes.json");
|
|
26
|
-
const actionMapPath = path.join(dist, "action_map.json");
|
|
27
|
-
|
|
28
|
-
fs.writeFileSync(
|
|
29
|
-
routesPath,
|
|
30
|
-
JSON.stringify(
|
|
31
|
-
{
|
|
32
|
-
__config: config,
|
|
33
|
-
routes,
|
|
34
|
-
__dynamic_routes: Object.values(dynamicRoutes).flat()
|
|
35
|
-
},
|
|
36
|
-
null,
|
|
37
|
-
2
|
|
38
|
-
)
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
fs.writeFileSync(
|
|
42
|
-
actionMapPath,
|
|
43
|
-
JSON.stringify(actionMap, null, 2)
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
} catch (err) {
|
|
47
|
-
console.error("\x1b[31m❌ Failed to parse routes from app.js\x1b[0m", err);
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { pathToFileURL } from "url";
|
|
4
|
+
|
|
5
|
+
export async function buildMetadata(root, dist) {
|
|
6
|
+
const appDir = path.join(root, "app");
|
|
7
|
+
const appFile = "app.js";
|
|
8
|
+
const appPath = path.join(appDir, appFile);
|
|
9
|
+
|
|
10
|
+
if (!fs.existsSync(appPath)) {
|
|
11
|
+
console.error(`\x1b[31m❌ app/${appFile} not found.\x1b[0m`);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
const appUrl = pathToFileURL(appPath).href;
|
|
17
|
+
const cacheBuster = `?t=${Date.now()}`;
|
|
18
|
+
await import(appUrl + cacheBuster);
|
|
19
|
+
|
|
20
|
+
const config = globalThis.__TITAN_CONFIG__ || { port: 3000, threads: 4, stack_mb: 8 };
|
|
21
|
+
const routes = globalThis.__TITAN_ROUTES_MAP__ || {};
|
|
22
|
+
const dynamicRoutes = globalThis.__TITAN_DYNAMIC_ROUTES__ || {};
|
|
23
|
+
const actionMap = globalThis.__TITAN_ACTION_MAP__ || {};
|
|
24
|
+
|
|
25
|
+
const routesPath = path.join(dist, "routes.json");
|
|
26
|
+
const actionMapPath = path.join(dist, "action_map.json");
|
|
27
|
+
|
|
28
|
+
fs.writeFileSync(
|
|
29
|
+
routesPath,
|
|
30
|
+
JSON.stringify(
|
|
31
|
+
{
|
|
32
|
+
__config: config,
|
|
33
|
+
routes,
|
|
34
|
+
__dynamic_routes: Object.values(dynamicRoutes).flat()
|
|
35
|
+
},
|
|
36
|
+
null,
|
|
37
|
+
2
|
|
38
|
+
)
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
fs.writeFileSync(
|
|
42
|
+
actionMapPath,
|
|
43
|
+
JSON.stringify(actionMap, null, 2)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
} catch (err) {
|
|
47
|
+
console.error("\x1b[31m❌ Failed to parse routes from app.js\x1b[0m", err);
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
}
|
package/js/titan/bundle.js
CHANGED
|
@@ -1,164 +1,164 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bundle.js (JavaScript Version)
|
|
3
|
-
* Handles esbuild bundling with error reporting
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import esbuild from 'esbuild';
|
|
7
|
-
import path from 'path';
|
|
8
|
-
import fs from 'fs';
|
|
9
|
-
import { fileURLToPath } from 'url';
|
|
10
|
-
import { createRequire } from 'module';
|
|
11
|
-
import { renderErrorBox, parseEsbuildError } from './error-box.js';
|
|
12
|
-
|
|
13
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
-
const __dirname = path.dirname(__filename);
|
|
15
|
-
|
|
16
|
-
// Required for resolving node_modules inside ESM
|
|
17
|
-
const require = createRequire(import.meta.url);
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Titan Node Builtin Rewrite Map
|
|
21
|
-
*/
|
|
22
|
-
const NODE_BUILTIN_MAP = {
|
|
23
|
-
"fs": "@titanpl/node/fs",
|
|
24
|
-
"node:fs": "@titanpl/node/fs",
|
|
25
|
-
"path": "@titanpl/node/path",
|
|
26
|
-
"node:path": "@titanpl/node/path",
|
|
27
|
-
"os": "@titanpl/node/os",
|
|
28
|
-
"node:os": "@titanpl/node/os",
|
|
29
|
-
"crypto": "@titanpl/node/crypto",
|
|
30
|
-
"node:crypto": "@titanpl/node/crypto",
|
|
31
|
-
"process": "@titanpl/node/process",
|
|
32
|
-
"util": "@titanpl/node/util",
|
|
33
|
-
"node:util": "@titanpl/node/util",
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const titanNodeCompatPlugin = {
|
|
37
|
-
name: "titan-node-compat",
|
|
38
|
-
setup(build) {
|
|
39
|
-
build.onResolve({ filter: /.*/ }, args => {
|
|
40
|
-
if (NODE_BUILTIN_MAP[args.path]) {
|
|
41
|
-
try {
|
|
42
|
-
const resolved = require.resolve(NODE_BUILTIN_MAP[args.path]);
|
|
43
|
-
return { path: resolved };
|
|
44
|
-
} catch (e) {
|
|
45
|
-
throw new Error(`[TitanPL] Failed to resolve Node shim: ${NODE_BUILTIN_MAP[args.path]}`);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
function getTitanVersion() {
|
|
53
|
-
try {
|
|
54
|
-
const pkgPath = require.resolve("@titanpl/cli/package.json");
|
|
55
|
-
return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
|
|
56
|
-
} catch (e) {
|
|
57
|
-
return "1.0.0";
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export class BundleError extends Error {
|
|
62
|
-
constructor(message, errors = [], warnings = []) {
|
|
63
|
-
super(message);
|
|
64
|
-
this.name = 'BundleError';
|
|
65
|
-
this.errors = errors;
|
|
66
|
-
this.warnings = warnings;
|
|
67
|
-
this.isBundleError = true;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function validateEntryPoint(entryPoint) {
|
|
72
|
-
const absPath = path.resolve(entryPoint);
|
|
73
|
-
if (!fs.existsSync(absPath)) {
|
|
74
|
-
throw new BundleError(`Entry point does not exist: ${entryPoint}`, [{ text: `Cannot find file: ${absPath}`, location: { file: entryPoint } }]);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Bundles a single file
|
|
80
|
-
*/
|
|
81
|
-
export async function bundleFile(options) {
|
|
82
|
-
const { entryPoint, outfile, format = 'iife', globalName = '__titan_exports', target = 'es2020' } = options;
|
|
83
|
-
|
|
84
|
-
await validateEntryPoint(entryPoint);
|
|
85
|
-
|
|
86
|
-
const outDir = path.dirname(outfile);
|
|
87
|
-
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
const result = await esbuild.build({
|
|
91
|
-
entryPoints: [entryPoint],
|
|
92
|
-
bundle: true,
|
|
93
|
-
outfile,
|
|
94
|
-
format,
|
|
95
|
-
globalName,
|
|
96
|
-
platform: 'node',
|
|
97
|
-
target,
|
|
98
|
-
logLevel: 'silent',
|
|
99
|
-
plugins: [titanNodeCompatPlugin],
|
|
100
|
-
banner: { js: "var Titan = t;" },
|
|
101
|
-
footer: options.footer || {}
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
if (result.errors?.length) {
|
|
105
|
-
throw new BundleError(`Build failed`, result.errors);
|
|
106
|
-
}
|
|
107
|
-
} catch (err) {
|
|
108
|
-
if (err.errors) throw new BundleError(`Build failed`, err.errors);
|
|
109
|
-
throw err;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Main JS Bundler
|
|
115
|
-
*/
|
|
116
|
-
export async function bundle(options = {}) {
|
|
117
|
-
const root = options.root || process.cwd();
|
|
118
|
-
const outDir = options.outDir || path.join(root, 'dist');
|
|
119
|
-
const titanVersion = getTitanVersion();
|
|
120
|
-
|
|
121
|
-
const actionsDir = path.join(root, 'app', 'actions');
|
|
122
|
-
const bundleDir = path.join(outDir, 'actions');
|
|
123
|
-
|
|
124
|
-
if (fs.existsSync(bundleDir)) fs.rmSync(bundleDir, { recursive: true, force: true });
|
|
125
|
-
fs.mkdirSync(bundleDir, { recursive: true });
|
|
126
|
-
|
|
127
|
-
if (!fs.existsSync(actionsDir)) return;
|
|
128
|
-
|
|
129
|
-
const files = fs.readdirSync(actionsDir).filter(f => f.endsWith('.js'));
|
|
130
|
-
|
|
131
|
-
for (const file of files) {
|
|
132
|
-
const actionName = path.basename(file, path.extname(file));
|
|
133
|
-
const entryPoint = path.join(actionsDir, file);
|
|
134
|
-
const outfile = path.join(bundleDir, actionName + ".jsbundle");
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
await bundleFile({
|
|
138
|
-
entryPoint,
|
|
139
|
-
outfile,
|
|
140
|
-
footer: {
|
|
141
|
-
js: `
|
|
142
|
-
(function () {
|
|
143
|
-
const fn = __titan_exports["${actionName}"] || __titan_exports.default;
|
|
144
|
-
if (typeof fn !== "function") throw new Error("[TitanPL] Action '${actionName}' not found or not a function");
|
|
145
|
-
globalThis["${actionName}"] = globalThis.defineAction(fn);
|
|
146
|
-
})();`
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
} catch (error) {
|
|
150
|
-
console.error();
|
|
151
|
-
if (error.isBundleError && error.errors?.length) {
|
|
152
|
-
for (const err of error.errors) {
|
|
153
|
-
const errorInfo = parseEsbuildError(err);
|
|
154
|
-
errorInfo.titanVersion = titanVersion;
|
|
155
|
-
console.error(renderErrorBox(errorInfo));
|
|
156
|
-
console.error();
|
|
157
|
-
}
|
|
158
|
-
} else {
|
|
159
|
-
console.error(renderErrorBox({ title: 'Build Error', message: error.message, titanVersion }));
|
|
160
|
-
}
|
|
161
|
-
throw new Error('__TITAN_BUNDLE_FAILED__');
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Bundle.js (JavaScript Version)
|
|
3
|
+
* Handles esbuild bundling with error reporting
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import esbuild from 'esbuild';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
import { createRequire } from 'module';
|
|
11
|
+
import { renderErrorBox, parseEsbuildError } from './error-box.js';
|
|
12
|
+
|
|
13
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
+
const __dirname = path.dirname(__filename);
|
|
15
|
+
|
|
16
|
+
// Required for resolving node_modules inside ESM
|
|
17
|
+
const require = createRequire(import.meta.url);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Titan Node Builtin Rewrite Map
|
|
21
|
+
*/
|
|
22
|
+
const NODE_BUILTIN_MAP = {
|
|
23
|
+
"fs": "@titanpl/node/fs",
|
|
24
|
+
"node:fs": "@titanpl/node/fs",
|
|
25
|
+
"path": "@titanpl/node/path",
|
|
26
|
+
"node:path": "@titanpl/node/path",
|
|
27
|
+
"os": "@titanpl/node/os",
|
|
28
|
+
"node:os": "@titanpl/node/os",
|
|
29
|
+
"crypto": "@titanpl/node/crypto",
|
|
30
|
+
"node:crypto": "@titanpl/node/crypto",
|
|
31
|
+
"process": "@titanpl/node/process",
|
|
32
|
+
"util": "@titanpl/node/util",
|
|
33
|
+
"node:util": "@titanpl/node/util",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const titanNodeCompatPlugin = {
|
|
37
|
+
name: "titan-node-compat",
|
|
38
|
+
setup(build) {
|
|
39
|
+
build.onResolve({ filter: /.*/ }, args => {
|
|
40
|
+
if (NODE_BUILTIN_MAP[args.path]) {
|
|
41
|
+
try {
|
|
42
|
+
const resolved = require.resolve(NODE_BUILTIN_MAP[args.path]);
|
|
43
|
+
return { path: resolved };
|
|
44
|
+
} catch (e) {
|
|
45
|
+
throw new Error(`[TitanPL] Failed to resolve Node shim: ${NODE_BUILTIN_MAP[args.path]}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function getTitanVersion() {
|
|
53
|
+
try {
|
|
54
|
+
const pkgPath = require.resolve("@titanpl/cli/package.json");
|
|
55
|
+
return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
|
|
56
|
+
} catch (e) {
|
|
57
|
+
return "1.0.0";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class BundleError extends Error {
|
|
62
|
+
constructor(message, errors = [], warnings = []) {
|
|
63
|
+
super(message);
|
|
64
|
+
this.name = 'BundleError';
|
|
65
|
+
this.errors = errors;
|
|
66
|
+
this.warnings = warnings;
|
|
67
|
+
this.isBundleError = true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function validateEntryPoint(entryPoint) {
|
|
72
|
+
const absPath = path.resolve(entryPoint);
|
|
73
|
+
if (!fs.existsSync(absPath)) {
|
|
74
|
+
throw new BundleError(`Entry point does not exist: ${entryPoint}`, [{ text: `Cannot find file: ${absPath}`, location: { file: entryPoint } }]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Bundles a single file
|
|
80
|
+
*/
|
|
81
|
+
export async function bundleFile(options) {
|
|
82
|
+
const { entryPoint, outfile, format = 'iife', globalName = '__titan_exports', target = 'es2020' } = options;
|
|
83
|
+
|
|
84
|
+
await validateEntryPoint(entryPoint);
|
|
85
|
+
|
|
86
|
+
const outDir = path.dirname(outfile);
|
|
87
|
+
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const result = await esbuild.build({
|
|
91
|
+
entryPoints: [entryPoint],
|
|
92
|
+
bundle: true,
|
|
93
|
+
outfile,
|
|
94
|
+
format,
|
|
95
|
+
globalName,
|
|
96
|
+
platform: 'node',
|
|
97
|
+
target,
|
|
98
|
+
logLevel: 'silent',
|
|
99
|
+
plugins: [titanNodeCompatPlugin],
|
|
100
|
+
banner: { js: "var Titan = t;" },
|
|
101
|
+
footer: options.footer || {}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (result.errors?.length) {
|
|
105
|
+
throw new BundleError(`Build failed`, result.errors);
|
|
106
|
+
}
|
|
107
|
+
} catch (err) {
|
|
108
|
+
if (err.errors) throw new BundleError(`Build failed`, err.errors);
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Main JS Bundler
|
|
115
|
+
*/
|
|
116
|
+
export async function bundle(options = {}) {
|
|
117
|
+
const root = options.root || process.cwd();
|
|
118
|
+
const outDir = options.outDir || path.join(root, 'dist');
|
|
119
|
+
const titanVersion = getTitanVersion();
|
|
120
|
+
|
|
121
|
+
const actionsDir = path.join(root, 'app', 'actions');
|
|
122
|
+
const bundleDir = path.join(outDir, 'actions');
|
|
123
|
+
|
|
124
|
+
if (fs.existsSync(bundleDir)) fs.rmSync(bundleDir, { recursive: true, force: true });
|
|
125
|
+
fs.mkdirSync(bundleDir, { recursive: true });
|
|
126
|
+
|
|
127
|
+
if (!fs.existsSync(actionsDir)) return;
|
|
128
|
+
|
|
129
|
+
const files = fs.readdirSync(actionsDir).filter(f => f.endsWith('.js'));
|
|
130
|
+
|
|
131
|
+
for (const file of files) {
|
|
132
|
+
const actionName = path.basename(file, path.extname(file));
|
|
133
|
+
const entryPoint = path.join(actionsDir, file);
|
|
134
|
+
const outfile = path.join(bundleDir, actionName + ".jsbundle");
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await bundleFile({
|
|
138
|
+
entryPoint,
|
|
139
|
+
outfile,
|
|
140
|
+
footer: {
|
|
141
|
+
js: `
|
|
142
|
+
(function () {
|
|
143
|
+
const fn = __titan_exports["${actionName}"] || __titan_exports.default;
|
|
144
|
+
if (typeof fn !== "function") throw new Error("[TitanPL] Action '${actionName}' not found or not a function");
|
|
145
|
+
globalThis["${actionName}"] = globalThis.defineAction(fn);
|
|
146
|
+
})();`
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error();
|
|
151
|
+
if (error.isBundleError && error.errors?.length) {
|
|
152
|
+
for (const err of error.errors) {
|
|
153
|
+
const errorInfo = parseEsbuildError(err);
|
|
154
|
+
errorInfo.titanVersion = titanVersion;
|
|
155
|
+
console.error(renderErrorBox(errorInfo));
|
|
156
|
+
console.error();
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
console.error(renderErrorBox({ title: 'Build Error', message: error.message, titanVersion }));
|
|
160
|
+
}
|
|
161
|
+
throw new Error('__TITAN_BUNDLE_FAILED__');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|