-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathswc-esbuild-plugin.ts
More file actions
386 lines (351 loc) · 14.2 KB
/
swc-esbuild-plugin.ts
File metadata and controls
386 lines (351 loc) · 14.2 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { readFile } from 'node:fs/promises';
import { relative } from 'node:path';
import { promisify } from 'node:util';
import enhancedResolveOrig from 'enhanced-resolve';
import type { Plugin } from 'esbuild';
import {
applySwcTransform,
type WorkflowManifest,
} from './apply-swc-transform.js';
import {
jsTsRegex,
parentHasChild,
} from './discover-entries-esbuild-plugin.js';
import { resolveWorkflowAliasRelativePath } from './workflow-alias.js';
export interface SwcPluginOptions {
mode: 'step' | 'workflow' | 'client';
entriesToBundle?: string[];
outdir?: string;
projectRoot?: string;
workflowManifest?: WorkflowManifest;
/**
* Rewrite TypeScript extensions (.ts, .tsx, .mts, .cts) to their JS
* equivalents (.js, .mjs, .cjs) in externalized import paths.
*
* Enable this when the output bundle is consumed directly by Node's native
* ESM loader (e.g. vitest), which cannot resolve .ts extensions.
*
* Leave disabled (default) when a downstream bundler (webpack, Vite, etc.)
* handles resolution — those tools resolve .ts natively and rewriting
* breaks them because the .js file doesn't exist on disk.
*/
rewriteTsExtensions?: boolean;
/**
* Absolute file paths of discovered workflow/step/serde entries whose
* imports must be treated as side-effectful.
*
* The SWC compiler transform injects registration calls (workflow IDs,
* step IDs, class serialization, etc.) into these files. Without this
* override, esbuild honours `"sideEffects": false` from the package's
* `package.json` and silently drops bare imports of these modules.
*/
sideEffectEntries?: string[];
}
const NODE_RESOLVE_OPTIONS = {
dependencyType: 'commonjs',
modules: ['node_modules'],
exportsFields: ['exports'],
importsFields: ['imports'],
conditionNames: ['node', 'require'],
descriptionFiles: ['package.json'],
extensions: [
'.ts',
'.tsx',
'.mts',
'.cts',
'.cjs',
'.mjs',
'.js',
'.jsx',
'.json',
'.node',
],
enforceExtensions: false,
symlinks: true,
mainFields: ['main'],
mainFiles: ['index'],
roots: [],
fullySpecified: false,
preferRelative: false,
preferAbsolute: false,
restrictions: [],
};
const NODE_ESM_RESOLVE_OPTIONS = {
...NODE_RESOLVE_OPTIONS,
dependencyType: 'esm',
conditionNames: ['node', 'import'],
};
export function createSwcPlugin(options: SwcPluginOptions): Plugin {
return {
name: 'swc-workflow-plugin',
setup(build) {
// everything is external unless explicitly configured
// to be bundled
const cjsResolver = promisify(
enhancedResolveOrig.create(NODE_RESOLVE_OPTIONS)
);
const esmResolver = promisify(
enhancedResolveOrig.create(NODE_ESM_RESOLVE_OPTIONS)
);
const enhancedResolve = async (context: string, path: string) => {
try {
return await esmResolver(context, path);
} catch (_) {
return cjsResolver(context, path);
}
};
// Pre-compute the normalized side-effect entries set for O(1) lookups.
const normalizedSideEffectEntries = new Set(
options.sideEffectEntries?.map((e) => e.replace(/\\/g, '/'))
);
build.onResolve({ filter: /.*/ }, async (args) => {
if (args.pluginData?.skipSwcPlugin) return null;
if (
!options.entriesToBundle &&
normalizedSideEffectEntries.size === 0
) {
return null;
}
// When only sideEffectEntries is set (no entriesToBundle), we only
// need to override sideEffects for top-level bare imports — typically
// from the virtual entry. Skip resolution for transitive imports
// (dynamic imports, requires, etc.) to avoid unnecessary overhead.
if (!options.entriesToBundle && args.kind !== 'import-statement') {
return null;
}
try {
const specifier = args.path;
const specifierIsPath =
specifier.startsWith('.') || specifier.startsWith('/');
let resolvedPath: string | false | undefined;
// Determines whether the external path should be relativized
// (project-local file) or kept as a bare specifier (npm package).
let shouldMakeRelative = specifierIsPath;
if (specifierIsPath) {
resolvedPath = await enhancedResolve(args.resolveDir, specifier);
} else {
// Resolve from project root so nested deps aren't externalized
resolvedPath = await enhancedResolve(
build.initialOptions.absWorkingDir || process.cwd(),
specifier
).catch(() => undefined); // swallow so esbuild fallback below can try
// Fall back to esbuild for aliases/tsconfig paths,
// but only accept project-local results
if (!resolvedPath) {
const esbuildResult = await build.resolve(specifier, {
resolveDir: args.resolveDir,
kind: args.kind,
pluginData: { skipSwcPlugin: true },
});
const didResolve =
!!esbuildResult.path && !esbuildResult.errors.length;
const isProjectLocalFile =
didResolve &&
!esbuildResult.external &&
!esbuildResult.path
.replace(/\\/g, '/')
.includes('/node_modules/');
if (isProjectLocalFile) {
resolvedPath = esbuildResult.path;
shouldMakeRelative = true;
}
}
}
if (!resolvedPath) return null;
// Normalize to forward slashes for cross-platform comparison
const normalizedResolvedPath = resolvedPath.replace(/\\/g, '/');
// Check if this module is a discovered entry whose SWC-transformed
// code contains side effects (workflow/step/class registration).
// Override the package.json "sideEffects": false so esbuild does not
// drop bare imports of these modules.
const hasSideEffects = normalizedSideEffectEntries.has(
normalizedResolvedPath
);
if (options.entriesToBundle) {
let shouldBundle = false;
for (const entryToBundle of options.entriesToBundle) {
const normalizedEntry = entryToBundle.replace(/\\/g, '/');
if (normalizedResolvedPath === normalizedEntry) {
shouldBundle = true;
break;
}
// if the current entry imports a child that needs
// to be bundled then it needs to also be bundled so
// that the child can have our transform applied
if (parentHasChild(normalizedResolvedPath, normalizedEntry)) {
shouldBundle = true;
break;
}
}
if (shouldBundle) {
// Let esbuild bundle this entry, but override sideEffects if needed.
// We must return the resolved `path` alongside `sideEffects` because
// returning only `{ sideEffects: true }` without a path causes esbuild
// to fall through to its own resolver, which re-reads the package.json
// and applies `"sideEffects": false` from there.
return hasSideEffects
? { path: resolvedPath, sideEffects: true }
: null;
}
let externalPath: string;
if (shouldMakeRelative) {
// When the resolved file lives inside node_modules, let
// esbuild bundle it rather than externalizing with a deeply
// nested relative path. Downstream bundlers (Rollup/Vite)
// can't rewrite opaque `__require()` calls in CJS shims, so
// relative paths computed for `outdir` break once the output
// is rebundled to a different directory.
if (normalizedResolvedPath.includes('/node_modules/')) {
return null; // let esbuild bundle it
}
externalPath = relative(
options.outdir || process.cwd(),
resolvedPath
).replace(/\\/g, '/');
if (options.rewriteTsExtensions) {
// Rewrite TypeScript extensions to their JS equivalents so the
// externalized import is loadable by Node's native ESM loader.
externalPath = externalPath
.replace(/\.tsx?$/, '.js')
.replace(/\.mts$/, '.mjs')
.replace(/\.cts$/, '.cjs');
}
} else {
externalPath = specifier;
}
return {
external: true,
path: externalPath,
sideEffects: hasSideEffects || undefined,
};
}
// No entriesToBundle — only override sideEffects when needed.
// We must return the resolved `path` alongside `sideEffects` because
// returning only `{ sideEffects: true }` without a path causes esbuild
// to fall through to its own resolver, which re-reads the package.json
// and applies `"sideEffects": false` from there.
return hasSideEffects
? { path: resolvedPath, sideEffects: true }
: null;
} catch (_) {}
return null;
});
// Handle TypeScript and JavaScript files
build.onLoad({ filter: jsTsRegex }, async (args) => {
// Determine if this is a TypeScript file
try {
// Determine the loader based on the output
let loader: 'js' | 'jsx' | 'tsx' = 'js';
if (args.path.endsWith('.jsx')) {
loader = 'jsx';
} else if (args.path.endsWith('.tsx')) {
loader = 'tsx';
}
const source = await readFile(args.path, 'utf8');
const normalizedSource = source
.replace(/require\(\s*(['"])server-only\1\s*\)/g, 'void 0')
.replace(/require\(\s*(['"])client-only\1\s*\)/g, 'void 0');
// Calculate relative path for SWC plugin
// The filename parameter is used to generate workflowId/stepId, so it must be relative
const workingDir =
build.initialOptions.absWorkingDir || process.cwd();
const projectRoot = options.projectRoot || workingDir;
// Normalize paths: convert backslashes to forward slashes and remove trailing slashes
const normalizedWorkingDir = workingDir
.replace(/\\/g, '/')
.replace(/\/$/, '');
const normalizedPath = args.path.replace(/\\/g, '/');
// Windows fix: Always do case-insensitive path comparison as the PRIMARY logic
// to work around node:path.relative() not recognizing paths with different drive
// letter casing (e.g., D: vs d:) as being in the same tree
const lowerWd = normalizedWorkingDir.toLowerCase();
const lowerPath = normalizedPath.toLowerCase();
let relativeFilepath: string;
if (lowerPath.startsWith(lowerWd + '/')) {
// File is under working directory - manually calculate relative path
// This ensures we get a relative path even with drive letter casing issues
relativeFilepath = normalizedPath.substring(
normalizedWorkingDir.length + 1
);
} else if (lowerPath === lowerWd) {
// File IS the working directory
relativeFilepath = '.';
} else {
// File is outside working directory - use relative() and strip ../ prefixes if needed
relativeFilepath = relative(
normalizedWorkingDir,
normalizedPath
).replace(/\\/g, '/');
// Handle files discovered outside the working directory
// These come back as ../path/to/file, but we want just path/to/file
if (relativeFilepath.startsWith('../')) {
const aliasedRelativePath =
await resolveWorkflowAliasRelativePath(args.path, workingDir);
if (aliasedRelativePath) {
relativeFilepath = aliasedRelativePath;
} else {
relativeFilepath = relativeFilepath
.split('/')
.filter((part) => part !== '..')
.join('/');
}
}
}
// Final safety check - ensure we never pass an absolute path to SWC
if (
relativeFilepath.includes(':') ||
relativeFilepath.startsWith('/')
) {
// This should never happen, but if it does, use just the filename as last resort
console.error(
`[ERROR] relativeFilepath is still absolute: ${relativeFilepath}`
);
relativeFilepath = normalizedPath.split('/').pop() || 'unknown.ts';
}
const { code: transformedCode, workflowManifest } =
await applySwcTransform(
relativeFilepath,
normalizedSource,
options.mode,
args.path, // Pass absolute path for module specifier resolution
projectRoot
);
if (!options.workflowManifest) {
options.workflowManifest = {};
}
options.workflowManifest.workflows = Object.assign(
options.workflowManifest.workflows || {},
workflowManifest.workflows
);
options.workflowManifest.steps = Object.assign(
options.workflowManifest.steps || {},
workflowManifest.steps
);
options.workflowManifest.classes = Object.assign(
options.workflowManifest.classes || {},
workflowManifest.classes
);
return {
contents: transformedCode,
loader,
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
console.error(
`❌ SWC transform error in ${args.path}:`,
errorMessage
);
return {
errors: [
{
text: `SWC transform failed: ${errorMessage}`,
location: { file: args.path, line: 0, column: 0 },
},
],
};
}
});
},
};
}