@broxium/compiler 1.0.0
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/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +173 -0
- package/dist/index.mjs +136 -0
- package/package.json +26 -0
- package/src/compiler.ts +93 -0
- package/src/index.ts +2 -0
- package/src/plugins/clientStubPlugin.ts +24 -0
- package/src/plugins/serverStubPlugin.ts +20 -0
- package/src/types.ts +18 -0
- package/tsconfig.json +15 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface CompileInput {
|
|
2
|
+
componentId: number;
|
|
3
|
+
slug: string;
|
|
4
|
+
version: string;
|
|
5
|
+
files: Array<{
|
|
6
|
+
path: string;
|
|
7
|
+
content: string;
|
|
8
|
+
}>;
|
|
9
|
+
outputDir: string;
|
|
10
|
+
}
|
|
11
|
+
interface CompileOutput {
|
|
12
|
+
serverJsPath: string;
|
|
13
|
+
clientJsPath: string;
|
|
14
|
+
serverJsName: string;
|
|
15
|
+
clientJsName: string;
|
|
16
|
+
compiledAt: Date;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare class BrodoxCompiler {
|
|
20
|
+
compile(input: CompileInput): Promise<CompileOutput>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { BrodoxCompiler, type CompileInput, type CompileOutput };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface CompileInput {
|
|
2
|
+
componentId: number;
|
|
3
|
+
slug: string;
|
|
4
|
+
version: string;
|
|
5
|
+
files: Array<{
|
|
6
|
+
path: string;
|
|
7
|
+
content: string;
|
|
8
|
+
}>;
|
|
9
|
+
outputDir: string;
|
|
10
|
+
}
|
|
11
|
+
interface CompileOutput {
|
|
12
|
+
serverJsPath: string;
|
|
13
|
+
clientJsPath: string;
|
|
14
|
+
serverJsName: string;
|
|
15
|
+
clientJsName: string;
|
|
16
|
+
compiledAt: Date;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare class BrodoxCompiler {
|
|
20
|
+
compile(input: CompileInput): Promise<CompileOutput>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { BrodoxCompiler, type CompileInput, type CompileOutput };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
BrodoxCompiler: () => BrodoxCompiler
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/compiler.ts
|
|
38
|
+
var esbuild = __toESM(require("esbuild"));
|
|
39
|
+
var import_promises3 = __toESM(require("fs/promises"));
|
|
40
|
+
var import_node_path = __toESM(require("path"));
|
|
41
|
+
var import_node_os = __toESM(require("os"));
|
|
42
|
+
var import_node_crypto = require("crypto");
|
|
43
|
+
|
|
44
|
+
// src/plugins/clientStubPlugin.ts
|
|
45
|
+
var import_promises = __toESM(require("fs/promises"));
|
|
46
|
+
function clientStubPlugin() {
|
|
47
|
+
return {
|
|
48
|
+
name: "brodox-client-stub",
|
|
49
|
+
setup(build2) {
|
|
50
|
+
build2.onLoad({ filter: /\.(tsx?|jsx?)$/ }, async (args) => {
|
|
51
|
+
const content = await import_promises.default.readFile(args.path, "utf8");
|
|
52
|
+
const trimmed = content.trimStart();
|
|
53
|
+
if (trimmed.startsWith("'use client'") || trimmed.startsWith('"use client"')) {
|
|
54
|
+
return {
|
|
55
|
+
contents: `
|
|
56
|
+
import React from 'react'
|
|
57
|
+
export default function ClientStub() { return null }
|
|
58
|
+
export function getServerData() { return {} }
|
|
59
|
+
`,
|
|
60
|
+
loader: "jsx"
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/plugins/serverStubPlugin.ts
|
|
69
|
+
var import_promises2 = __toESM(require("fs/promises"));
|
|
70
|
+
function serverStubPlugin() {
|
|
71
|
+
return {
|
|
72
|
+
name: "brodox-server-stub",
|
|
73
|
+
setup(build2) {
|
|
74
|
+
build2.onLoad({ filter: /\.(tsx?|jsx?)$/ }, async (args) => {
|
|
75
|
+
const content = await import_promises2.default.readFile(args.path, "utf8");
|
|
76
|
+
const trimmed = content.trimStart();
|
|
77
|
+
if (trimmed.startsWith("'use server'") || trimmed.startsWith('"use server"')) {
|
|
78
|
+
return {
|
|
79
|
+
contents: `export default function ServerStub() { return null }`,
|
|
80
|
+
loader: "jsx"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/compiler.ts
|
|
89
|
+
var ENTRY_PRIORITY = [
|
|
90
|
+
"App.tsx",
|
|
91
|
+
"App.jsx",
|
|
92
|
+
"App.ts",
|
|
93
|
+
"App.js",
|
|
94
|
+
"index.tsx",
|
|
95
|
+
"index.jsx",
|
|
96
|
+
"index.ts",
|
|
97
|
+
"index.js"
|
|
98
|
+
];
|
|
99
|
+
var external = [
|
|
100
|
+
"react",
|
|
101
|
+
"react-dom",
|
|
102
|
+
"react/jsx-runtime",
|
|
103
|
+
"react/jsx-dev-runtime",
|
|
104
|
+
"@broxium/runtime"
|
|
105
|
+
];
|
|
106
|
+
var BrodoxCompiler = class {
|
|
107
|
+
async compile(input) {
|
|
108
|
+
const tmpDir = import_node_path.default.join(import_node_os.default.tmpdir(), `brodox-compile-${input.slug}-${(0, import_node_crypto.randomUUID)()}`);
|
|
109
|
+
await import_promises3.default.mkdir(tmpDir, { recursive: true });
|
|
110
|
+
for (const file of input.files) {
|
|
111
|
+
const filePath = import_node_path.default.join(tmpDir, file.path);
|
|
112
|
+
await import_promises3.default.mkdir(import_node_path.default.dirname(filePath), { recursive: true });
|
|
113
|
+
await import_promises3.default.writeFile(filePath, file.content, "utf8");
|
|
114
|
+
}
|
|
115
|
+
let entryPoint = null;
|
|
116
|
+
for (const candidate of ENTRY_PRIORITY) {
|
|
117
|
+
const full = import_node_path.default.join(tmpDir, candidate);
|
|
118
|
+
try {
|
|
119
|
+
await import_promises3.default.access(full);
|
|
120
|
+
entryPoint = full;
|
|
121
|
+
break;
|
|
122
|
+
} catch {
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!entryPoint) {
|
|
126
|
+
throw new Error(`No entry file found in component files for ${input.slug}`);
|
|
127
|
+
}
|
|
128
|
+
const safeName = `${input.slug}-v${input.version}`;
|
|
129
|
+
const serverJsName = `${safeName}.server.esm.js`;
|
|
130
|
+
const clientJsName = `${safeName}.client.esm.js`;
|
|
131
|
+
const serverJsPath = import_node_path.default.join(input.outputDir, serverJsName);
|
|
132
|
+
const clientJsPath = import_node_path.default.join(input.outputDir, clientJsName);
|
|
133
|
+
await import_promises3.default.mkdir(input.outputDir, { recursive: true });
|
|
134
|
+
await esbuild.build({
|
|
135
|
+
entryPoints: [entryPoint],
|
|
136
|
+
bundle: true,
|
|
137
|
+
format: "esm",
|
|
138
|
+
platform: "node",
|
|
139
|
+
target: "node20",
|
|
140
|
+
external,
|
|
141
|
+
plugins: [clientStubPlugin()],
|
|
142
|
+
outfile: serverJsPath,
|
|
143
|
+
minify: false,
|
|
144
|
+
sourcemap: false,
|
|
145
|
+
define: { "process.env.NODE_ENV": '"production"' }
|
|
146
|
+
});
|
|
147
|
+
await esbuild.build({
|
|
148
|
+
entryPoints: [entryPoint],
|
|
149
|
+
bundle: true,
|
|
150
|
+
format: "esm",
|
|
151
|
+
platform: "browser",
|
|
152
|
+
target: ["es2020", "chrome90", "firefox88", "safari14"],
|
|
153
|
+
external,
|
|
154
|
+
plugins: [serverStubPlugin()],
|
|
155
|
+
outfile: clientJsPath,
|
|
156
|
+
minify: true,
|
|
157
|
+
sourcemap: false,
|
|
158
|
+
define: { "process.env.NODE_ENV": '"production"' }
|
|
159
|
+
});
|
|
160
|
+
await import_promises3.default.rm(tmpDir, { recursive: true, force: true });
|
|
161
|
+
return {
|
|
162
|
+
serverJsPath,
|
|
163
|
+
clientJsPath,
|
|
164
|
+
serverJsName,
|
|
165
|
+
clientJsName,
|
|
166
|
+
compiledAt: /* @__PURE__ */ new Date()
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
171
|
+
0 && (module.exports = {
|
|
172
|
+
BrodoxCompiler
|
|
173
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// src/compiler.ts
|
|
2
|
+
import * as esbuild from "esbuild";
|
|
3
|
+
import fs3 from "fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import os from "os";
|
|
6
|
+
import { randomUUID } from "crypto";
|
|
7
|
+
|
|
8
|
+
// src/plugins/clientStubPlugin.ts
|
|
9
|
+
import fs from "fs/promises";
|
|
10
|
+
function clientStubPlugin() {
|
|
11
|
+
return {
|
|
12
|
+
name: "brodox-client-stub",
|
|
13
|
+
setup(build2) {
|
|
14
|
+
build2.onLoad({ filter: /\.(tsx?|jsx?)$/ }, async (args) => {
|
|
15
|
+
const content = await fs.readFile(args.path, "utf8");
|
|
16
|
+
const trimmed = content.trimStart();
|
|
17
|
+
if (trimmed.startsWith("'use client'") || trimmed.startsWith('"use client"')) {
|
|
18
|
+
return {
|
|
19
|
+
contents: `
|
|
20
|
+
import React from 'react'
|
|
21
|
+
export default function ClientStub() { return null }
|
|
22
|
+
export function getServerData() { return {} }
|
|
23
|
+
`,
|
|
24
|
+
loader: "jsx"
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/plugins/serverStubPlugin.ts
|
|
33
|
+
import fs2 from "fs/promises";
|
|
34
|
+
function serverStubPlugin() {
|
|
35
|
+
return {
|
|
36
|
+
name: "brodox-server-stub",
|
|
37
|
+
setup(build2) {
|
|
38
|
+
build2.onLoad({ filter: /\.(tsx?|jsx?)$/ }, async (args) => {
|
|
39
|
+
const content = await fs2.readFile(args.path, "utf8");
|
|
40
|
+
const trimmed = content.trimStart();
|
|
41
|
+
if (trimmed.startsWith("'use server'") || trimmed.startsWith('"use server"')) {
|
|
42
|
+
return {
|
|
43
|
+
contents: `export default function ServerStub() { return null }`,
|
|
44
|
+
loader: "jsx"
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/compiler.ts
|
|
53
|
+
var ENTRY_PRIORITY = [
|
|
54
|
+
"App.tsx",
|
|
55
|
+
"App.jsx",
|
|
56
|
+
"App.ts",
|
|
57
|
+
"App.js",
|
|
58
|
+
"index.tsx",
|
|
59
|
+
"index.jsx",
|
|
60
|
+
"index.ts",
|
|
61
|
+
"index.js"
|
|
62
|
+
];
|
|
63
|
+
var external = [
|
|
64
|
+
"react",
|
|
65
|
+
"react-dom",
|
|
66
|
+
"react/jsx-runtime",
|
|
67
|
+
"react/jsx-dev-runtime",
|
|
68
|
+
"@broxium/runtime"
|
|
69
|
+
];
|
|
70
|
+
var BrodoxCompiler = class {
|
|
71
|
+
async compile(input) {
|
|
72
|
+
const tmpDir = path.join(os.tmpdir(), `brodox-compile-${input.slug}-${randomUUID()}`);
|
|
73
|
+
await fs3.mkdir(tmpDir, { recursive: true });
|
|
74
|
+
for (const file of input.files) {
|
|
75
|
+
const filePath = path.join(tmpDir, file.path);
|
|
76
|
+
await fs3.mkdir(path.dirname(filePath), { recursive: true });
|
|
77
|
+
await fs3.writeFile(filePath, file.content, "utf8");
|
|
78
|
+
}
|
|
79
|
+
let entryPoint = null;
|
|
80
|
+
for (const candidate of ENTRY_PRIORITY) {
|
|
81
|
+
const full = path.join(tmpDir, candidate);
|
|
82
|
+
try {
|
|
83
|
+
await fs3.access(full);
|
|
84
|
+
entryPoint = full;
|
|
85
|
+
break;
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (!entryPoint) {
|
|
90
|
+
throw new Error(`No entry file found in component files for ${input.slug}`);
|
|
91
|
+
}
|
|
92
|
+
const safeName = `${input.slug}-v${input.version}`;
|
|
93
|
+
const serverJsName = `${safeName}.server.esm.js`;
|
|
94
|
+
const clientJsName = `${safeName}.client.esm.js`;
|
|
95
|
+
const serverJsPath = path.join(input.outputDir, serverJsName);
|
|
96
|
+
const clientJsPath = path.join(input.outputDir, clientJsName);
|
|
97
|
+
await fs3.mkdir(input.outputDir, { recursive: true });
|
|
98
|
+
await esbuild.build({
|
|
99
|
+
entryPoints: [entryPoint],
|
|
100
|
+
bundle: true,
|
|
101
|
+
format: "esm",
|
|
102
|
+
platform: "node",
|
|
103
|
+
target: "node20",
|
|
104
|
+
external,
|
|
105
|
+
plugins: [clientStubPlugin()],
|
|
106
|
+
outfile: serverJsPath,
|
|
107
|
+
minify: false,
|
|
108
|
+
sourcemap: false,
|
|
109
|
+
define: { "process.env.NODE_ENV": '"production"' }
|
|
110
|
+
});
|
|
111
|
+
await esbuild.build({
|
|
112
|
+
entryPoints: [entryPoint],
|
|
113
|
+
bundle: true,
|
|
114
|
+
format: "esm",
|
|
115
|
+
platform: "browser",
|
|
116
|
+
target: ["es2020", "chrome90", "firefox88", "safari14"],
|
|
117
|
+
external,
|
|
118
|
+
plugins: [serverStubPlugin()],
|
|
119
|
+
outfile: clientJsPath,
|
|
120
|
+
minify: true,
|
|
121
|
+
sourcemap: false,
|
|
122
|
+
define: { "process.env.NODE_ENV": '"production"' }
|
|
123
|
+
});
|
|
124
|
+
await fs3.rm(tmpDir, { recursive: true, force: true });
|
|
125
|
+
return {
|
|
126
|
+
serverJsPath,
|
|
127
|
+
clientJsPath,
|
|
128
|
+
serverJsName,
|
|
129
|
+
clientJsName,
|
|
130
|
+
compiledAt: /* @__PURE__ */ new Date()
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
export {
|
|
135
|
+
BrodoxCompiler
|
|
136
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@broxium/compiler",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Brodox component compiler — TSX to ESM server + client bundles",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"esbuild": "^0.25.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"tsup": "^8.0.0",
|
|
19
|
+
"typescript": "^5.0.0",
|
|
20
|
+
"@types/node": "^20.0.0"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
24
|
+
"dev": "tsup src/index.ts --format esm,cjs --dts --watch"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/compiler.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import * as esbuild from 'esbuild'
|
|
2
|
+
import fs from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import { randomUUID } from 'node:crypto'
|
|
6
|
+
import { clientStubPlugin } from './plugins/clientStubPlugin'
|
|
7
|
+
import { serverStubPlugin } from './plugins/serverStubPlugin'
|
|
8
|
+
import type { CompileInput, CompileOutput } from './types'
|
|
9
|
+
|
|
10
|
+
const ENTRY_PRIORITY = [
|
|
11
|
+
'App.tsx', 'App.jsx', 'App.ts', 'App.js',
|
|
12
|
+
'index.tsx', 'index.jsx', 'index.ts', 'index.js',
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
const external = [
|
|
16
|
+
'react',
|
|
17
|
+
'react-dom',
|
|
18
|
+
'react/jsx-runtime',
|
|
19
|
+
'react/jsx-dev-runtime',
|
|
20
|
+
'@broxium/runtime',
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
export class BrodoxCompiler {
|
|
24
|
+
async compile(input: CompileInput): Promise<CompileOutput> {
|
|
25
|
+
const tmpDir = path.join(os.tmpdir(), `brodox-compile-${input.slug}-${randomUUID()}`)
|
|
26
|
+
await fs.mkdir(tmpDir, { recursive: true })
|
|
27
|
+
|
|
28
|
+
for (const file of input.files) {
|
|
29
|
+
const filePath = path.join(tmpDir, file.path)
|
|
30
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true })
|
|
31
|
+
await fs.writeFile(filePath, file.content, 'utf8')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let entryPoint: string | null = null
|
|
35
|
+
for (const candidate of ENTRY_PRIORITY) {
|
|
36
|
+
const full = path.join(tmpDir, candidate)
|
|
37
|
+
try {
|
|
38
|
+
await fs.access(full)
|
|
39
|
+
entryPoint = full
|
|
40
|
+
break
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
if (!entryPoint) {
|
|
44
|
+
throw new Error(`No entry file found in component files for ${input.slug}`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const safeName = `${input.slug}-v${input.version}`
|
|
48
|
+
const serverJsName = `${safeName}.server.esm.js`
|
|
49
|
+
const clientJsName = `${safeName}.client.esm.js`
|
|
50
|
+
const serverJsPath = path.join(input.outputDir, serverJsName)
|
|
51
|
+
const clientJsPath = path.join(input.outputDir, clientJsName)
|
|
52
|
+
|
|
53
|
+
await fs.mkdir(input.outputDir, { recursive: true })
|
|
54
|
+
|
|
55
|
+
await esbuild.build({
|
|
56
|
+
entryPoints: [entryPoint],
|
|
57
|
+
bundle: true,
|
|
58
|
+
format: 'esm',
|
|
59
|
+
platform: 'node',
|
|
60
|
+
target: 'node20',
|
|
61
|
+
external,
|
|
62
|
+
plugins: [clientStubPlugin()],
|
|
63
|
+
outfile: serverJsPath,
|
|
64
|
+
minify: false,
|
|
65
|
+
sourcemap: false,
|
|
66
|
+
define: { 'process.env.NODE_ENV': '"production"' },
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
await esbuild.build({
|
|
70
|
+
entryPoints: [entryPoint],
|
|
71
|
+
bundle: true,
|
|
72
|
+
format: 'esm',
|
|
73
|
+
platform: 'browser',
|
|
74
|
+
target: ['es2020', 'chrome90', 'firefox88', 'safari14'],
|
|
75
|
+
external,
|
|
76
|
+
plugins: [serverStubPlugin()],
|
|
77
|
+
outfile: clientJsPath,
|
|
78
|
+
minify: true,
|
|
79
|
+
sourcemap: false,
|
|
80
|
+
define: { 'process.env.NODE_ENV': '"production"' },
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
await fs.rm(tmpDir, { recursive: true, force: true })
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
serverJsPath,
|
|
87
|
+
clientJsPath,
|
|
88
|
+
serverJsName,
|
|
89
|
+
clientJsName,
|
|
90
|
+
compiledAt: new Date(),
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Plugin } from 'esbuild'
|
|
2
|
+
import fs from 'node:fs/promises'
|
|
3
|
+
|
|
4
|
+
export function clientStubPlugin(): Plugin {
|
|
5
|
+
return {
|
|
6
|
+
name: 'brodox-client-stub',
|
|
7
|
+
setup(build) {
|
|
8
|
+
build.onLoad({ filter: /\.(tsx?|jsx?)$/ }, async (args) => {
|
|
9
|
+
const content = await fs.readFile(args.path, 'utf8')
|
|
10
|
+
const trimmed = content.trimStart()
|
|
11
|
+
if (trimmed.startsWith("'use client'") || trimmed.startsWith('"use client"')) {
|
|
12
|
+
return {
|
|
13
|
+
contents: `
|
|
14
|
+
import React from 'react'
|
|
15
|
+
export default function ClientStub() { return null }
|
|
16
|
+
export function getServerData() { return {} }
|
|
17
|
+
`,
|
|
18
|
+
loader: 'jsx',
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Plugin } from 'esbuild'
|
|
2
|
+
import fs from 'node:fs/promises'
|
|
3
|
+
|
|
4
|
+
export function serverStubPlugin(): Plugin {
|
|
5
|
+
return {
|
|
6
|
+
name: 'brodox-server-stub',
|
|
7
|
+
setup(build) {
|
|
8
|
+
build.onLoad({ filter: /\.(tsx?|jsx?)$/ }, async (args) => {
|
|
9
|
+
const content = await fs.readFile(args.path, 'utf8')
|
|
10
|
+
const trimmed = content.trimStart()
|
|
11
|
+
if (trimmed.startsWith("'use server'") || trimmed.startsWith('"use server"')) {
|
|
12
|
+
return {
|
|
13
|
+
contents: `export default function ServerStub() { return null }`,
|
|
14
|
+
loader: 'jsx',
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface CompileInput {
|
|
2
|
+
componentId: number
|
|
3
|
+
slug: string
|
|
4
|
+
version: string
|
|
5
|
+
files: Array<{
|
|
6
|
+
path: string
|
|
7
|
+
content: string
|
|
8
|
+
}>
|
|
9
|
+
outputDir: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CompileOutput {
|
|
13
|
+
serverJsPath: string
|
|
14
|
+
clientJsPath: string
|
|
15
|
+
serverJsName: string
|
|
16
|
+
clientJsName: string
|
|
17
|
+
compiledAt: Date
|
|
18
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"lib": ["ES2020"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"outDir": "./dist"
|
|
12
|
+
},
|
|
13
|
+
"include": ["src"],
|
|
14
|
+
"exclude": ["node_modules", "dist"]
|
|
15
|
+
}
|