Skip to content

Commit e24c448

Browse files
committed
feat: add globalThis.__import
1 parent fd1806e commit e24c448

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/instrument-bundler/src/bundle.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import * as esbuild from './vendor/esbuild.js';
66
import type { BundleOptions } from './schemas.js';
77
import type { BuildOutput } from './types.js';
88

9-
const GLOBAL_PROXY_SHIM = `
9+
const GLOBALS = `
10+
globalThis.__import = async (moduleName) => {
11+
try {
12+
return import(moduleName);
13+
} catch (err) {
14+
throw new Error('Failed to import module: ' + moduleName, { cause: err });
15+
}
16+
};
1017
const __createProxy = (name) => {
1118
const formatErrorMessage = (method, propertyName, targetName) => {
1219
const contextName = globalThis.__ODC_BUNDLER_ERROR_CONTEXT ?? 'UNKNOWN'
@@ -39,7 +46,7 @@ export async function createBundle(output: BuildOutput, options: { minify: boole
3946
inject = `Object.defineProperty(__exports.content, '__injectHead', { value: Object.freeze({ style: "${btoa(output.css)}" }), writable: false });`;
4047
}
4148
const bundle = `(async () => {
42-
${GLOBAL_PROXY_SHIM}
49+
${GLOBALS}
4350
${output.js}
4451
${inject}
4552
return __exports;

packages/instrument-bundler/src/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function transformImports(code: string) {
3333
const endIndex = _endIndex + indexDiff;
3434
const startIndex = _startIndex + indexDiff;
3535
const source = code.slice(startIndex, endIndex);
36-
const replacement = `const ${transformImportClause(importClause!)} = await import(${moduleSpecifier.code})`;
36+
const replacement = `const ${transformImportClause(importClause!)} = await __import(${moduleSpecifier.code})`;
3737
indexDiff += replacement.length - source.length;
3838
code = code.slice(0, startIndex) + replacement + code.slice(endIndex, code.length);
3939
}

0 commit comments

Comments
 (0)