-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbuild.parser-javascript.mjs
More file actions
144 lines (131 loc) · 4.96 KB
/
build.parser-javascript.mjs
File metadata and controls
144 lines (131 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2026, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* For full license text, see the LICENSE file in the repo root or https://www.apache.org/licenses/LICENSE-2.0
*/
/**
* Build script for @agentscript/agentforce — parser-javascript mode (default).
*
* Builds: Node.js ESM bundle + browser ESM + browser IIFE + TypeScript declarations.
* No WASM generation — tree-sitter imports are stubbed out.
*/
import { build } from 'esbuild';
import { readFileSync } from 'node:fs';
import { buildNodeBundle, buildDeclarations } from './build.shared.mjs';
console.log('📦 Building (parser-javascript)...\n');
// ── esbuild plugin: stub tree-sitter-only imports ───────────────────────
/** Replaces web-tree-sitter and wasm-loader with empty modules. */
const stubTreeSitterPlugin = {
name: 'stub-tree-sitter',
setup(b) {
b.onResolve({ filter: /^web-tree-sitter$/ }, () => ({
path: 'web-tree-sitter',
namespace: 'stub',
}));
b.onResolve({ filter: /wasm-constants-generated/ }, () => ({
path: 'wasm-constants-generated',
namespace: 'stub',
}));
b.onResolve({ filter: /wasm-loader/ }, () => ({
path: 'wasm-loader',
namespace: 'stub',
}));
b.onLoad({ filter: /.*/, namespace: 'stub' }, args => {
if (args.path === 'wasm-constants-generated') {
return {
contents:
'export const TREE_SITTER_ENGINE_BASE64 = [];\nexport const TREE_SITTER_AGENTSCRIPT_BASE64 = [];',
loader: 'js',
};
}
if (args.path === 'wasm-loader') {
return {
contents:
'export async function loadWasmModule() { return undefined; }',
loader: 'js',
};
}
// web-tree-sitter — side-effect-only import, empty module is fine
return { contents: '', loader: 'js' };
});
},
};
// ── Bundles ─────────────────────────────────────────────────────────────
// 1. Node.js ESM bundle
await buildNodeBundle({ plugins: [stubTreeSitterPlugin] });
// 2. Browser ESM bundle
console.log('Building browser ESM bundle (parser-javascript)...');
await build({
entryPoints: ['src/browser.ts'],
bundle: true,
format: 'esm',
outfile: 'dist/browser.js',
platform: 'neutral',
target: 'es2020',
external: ['tree-sitter', 'fs', 'fs/promises', 'path', 'module', 'url'],
plugins: [stubTreeSitterPlugin],
sourcemap: true,
minify: false,
define: {
global: 'globalThis',
'process.env.NODE_ENV': '"production"',
},
});
console.log(' ✓ dist/browser.js created (ESM, parser-javascript)\n');
// 3. Browser WASM IIFE bundle for script tags
const { version } = JSON.parse(
readFileSync(new URL('./package.json', import.meta.url), 'utf8')
);
console.log('Building browser IIFE bundle (parser-javascript)...');
await build({
entryPoints: ['src/browser-iife.ts'],
bundle: true,
format: 'iife',
globalName: 'AgentforceScriptSDK',
outfile: 'dist/browser.iife.js',
platform: 'browser',
target: 'es2020',
external: ['tree-sitter', 'fs', 'fs/promises', 'path', 'module', 'url'],
plugins: [stubTreeSitterPlugin],
sourcemap: true,
minify: false,
define: {
global: 'globalThis',
'process.env.NODE_ENV': '"production"',
__PACKAGE_VERSION__: JSON.stringify(version),
},
});
console.log(' ✓ dist/browser.iife.js created (IIFE, parser-wasm)\n');
// 4. Browser JS IIFE bundle for script tags
console.log('Building browser IIFE bundle (parser-javascript)...');
await build({
entryPoints: ['src/browser-js-iife.ts'],
bundle: true,
format: 'iife',
globalName: 'AgentforceScriptSDK',
outfile: 'dist/browser-js.iife.js',
platform: 'browser',
target: 'es2020',
external: ['fs', 'fs/promises', 'path', 'module', 'url'],
plugins: [stubTreeSitterPlugin],
sourcemap: true,
minify: false,
define: {
global: 'globalThis',
'process.env.NODE_ENV': '"production"',
__PACKAGE_VERSION__: JSON.stringify(version),
},
});
console.log(' ✓ dist/browser-js.iife.js created (IIFE, parser-javascript)\n');
// ── Declarations ────────────────────────────────────────────────────────
buildDeclarations();
// ── Summary ─────────────────────────────────────────────────────────────
console.log('✅ Build complete!\n');
console.log(' 📁 dist/index.js → Node.js (with peer deps)');
console.log(' 📁 dist/index.d.ts → TypeScript declarations');
console.log(' 📁 dist/browser.js → Browser ESM (parser-javascript)');
console.log(
' 📁 dist/browser.iife.js → Browser IIFE (parser-javascript, window.AgentforceScriptSDK)'
);
console.log('');