|
| 1 | +import { |
| 2 | + buildSync, |
| 3 | + type BuildFailure, |
| 4 | + type BuildOptions, |
| 5 | + type BuildResult, |
| 6 | +} from 'esbuild'; |
| 7 | +import { dirname } from 'path'; |
1 | 8 | import { runInNewContext } from 'vm';
|
2 |
| -import { type BuildOptions, buildSync, type BuildResult } from 'esbuild'; |
| 9 | +import { LoadEnvFileOptions } from './envVariables/loadEnvFile'; |
3 | 10 | import { getSandBoxContext } from './getSandboxContext';
|
4 |
| -import { ESMxCJSRequire } from './utils/ESMxCJSRequire'; |
5 | 11 | import { logger } from './logger';
|
6 |
| -import { LoadEnvFileOptions } from './envVariables/loadEnvFile'; |
7 |
| -import { dirname } from 'path'; |
| 12 | +import { logTypeScriptErrors } from './logTypeScriptErrors'; |
| 13 | +import { ESMxCJSRequire } from './utils/ESMxCJSRequire'; |
8 | 14 |
|
9 | 15 | const getTransformationOptions = (filePath: string): BuildOptions => ({
|
10 | 16 | loader: {
|
@@ -55,11 +61,26 @@ export const loadExternalFile = (
|
55 | 61 |
|
56 | 62 | // Rest is JS, MJS or TS
|
57 | 63 |
|
| 64 | + if (fileExtension === 'ts' || fileExtension === 'tsx') { |
| 65 | + logTypeScriptErrors(filePath); |
| 66 | + } |
| 67 | + |
58 | 68 | const moduleResult: BuildResult = buildSync({
|
59 | 69 | entryPoints: [filePath],
|
60 | 70 | ...getTransformationOptions(filePath),
|
| 71 | + logLevel: 'silent', // prevent auto printing |
61 | 72 | });
|
62 | 73 |
|
| 74 | + // Log Warnings from esbuild |
| 75 | + if (moduleResult.warnings?.length > 0) { |
| 76 | + for (const warn of moduleResult.warnings) { |
| 77 | + logger( |
| 78 | + `[ESBUILD WARNING] ${warn.text} at ${warn.location?.file ?? filePath}:${warn.location?.line ?? '?'}:${warn.location?.column ?? '?'}`, |
| 79 | + { level: 'warn' } |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
63 | 84 | const moduleResultString = moduleResult.outputFiles?.[0].text;
|
64 | 85 |
|
65 | 86 | if (!moduleResultString) {
|
@@ -104,6 +125,17 @@ export const loadExternalFile = (
|
104 | 125 |
|
105 | 126 | return fileContent;
|
106 | 127 | } catch (error) {
|
| 128 | + // Specific handling for esbuild errors (i.e. build-time TypeScript errors) |
| 129 | + if ((error as BuildFailure).errors) { |
| 130 | + const esbuildErrors = (error as BuildFailure).errors; |
| 131 | + for (const err of esbuildErrors) { |
| 132 | + logger( |
| 133 | + `${err.text} at ${err.location?.file}:${err.location?.line}:${err.location?.column}`, |
| 134 | + { level: 'error' } |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
107 | 139 | logger(
|
108 | 140 | `Error: ${error} ${JSON.stringify((error as Error).stack, null, 2)}`,
|
109 | 141 | {
|
|
0 commit comments