Skip to content

Commit a915017

Browse files
authored
Merge pull request #1081 from joshunrau/patches
add new `__import` wrapper to internal instrument bundle implementation
2 parents ea794b8 + e24c448 commit a915017

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

packages/instrument-bundler/src/__tests__/transform.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { transformStaticImports } from '../transform.js';
3+
import { transformImports } from '../transform.js';
44

5-
describe('transformStaticImports', () => {
5+
describe('transformImports', () => {
66
it('should transform a default import', () => {
7-
expect(transformStaticImports("import React from 'react';")).toBe(
8-
"const { default: React } = await import('react');"
9-
);
7+
expect(transformImports("import React from 'react';")).toBe("const { default: React } = await import('react');");
108
});
119
it('should transform named imports', () => {
12-
expect(transformStaticImports("import { useEffect, useState } from 'react';")).toBe(
10+
expect(transformImports("import { useEffect, useState } from 'react';")).toBe(
1311
"const { useEffect, useState } = await import('react');"
1412
);
1513
});
1614
it('should transform named and default imports from the same source', () => {
17-
expect(transformStaticImports("import React, { useState } from 'react';")).toBe(
15+
expect(transformImports("import React, { useState } from 'react';")).toBe(
1816
"const { useState, default: React } = await import('react');"
1917
);
2018
});
2119
it('should transform a namespace import', () => {
22-
expect(transformStaticImports("import * as React from 'react';")).toBe("const React = await import('react');");
20+
expect(transformImports("import * as React from 'react';")).toBe("const React = await import('react');");
2321
});
2422
it('should transform named and default imports from the same source', () => {
25-
expect(transformStaticImports("import _ from 'lodash'; import { useEffect, useState } from 'react';")).toBe(
23+
expect(transformImports("import _ from 'lodash'; import { useEffect, useState } from 'react';")).toBe(
2624
"const { default: _ } = await import('lodash'); const { useEffect, useState } = await import('react');"
2725
);
2826
});

packages/instrument-bundler/src/bundle.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { build } from './build.js';
22
import { preprocess } from './preprocess.js';
3-
import { transformStaticImports } from './transform.js';
3+
import { transformImports } from './transform.js';
44
import * as esbuild from './vendor/esbuild.js';
55

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;
@@ -60,6 +67,6 @@ export async function createBundle(output: BuildOutput, options: { minify: boole
6067
export async function bundle({ inputs, minify = true }: BundleOptions): Promise<string> {
6168
preprocess(inputs);
6269
const result = await build({ inputs });
63-
result.js = transformStaticImports(result.js);
70+
result.js = transformImports(result.js);
6471
return createBundle(result, { minify });
6572
}

packages/instrument-bundler/src/transform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function transformImportClause(importClause: ImportClause) {
1818
return `{ ${names.join(', ')} }`;
1919
}
2020

21-
export function transformStaticImports(code: string) {
21+
export function transformImports(code: string) {
2222
let indexDiff = 0;
2323
for (const {
2424
endIndex: _endIndex,
@@ -33,7 +33,7 @@ export function transformStaticImports(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)