Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function build({
buildOptions.banner = {
js:
(buildOptions.banner?.js || '') +
`import {createRequire as topLevelCreateRequire} from 'module';\nconst require = topLevelCreateRequire(import.meta.url);`,
`import {createRequire as topLevelCreateRequire} from 'node:module';\nconst require = topLevelCreateRequire(import.meta.url);`,
};
}

Expand Down
2 changes: 1 addition & 1 deletion dev/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '@google/adk';
import {Argument, Command, Option} from 'commander';
import dotenv from 'dotenv';
import * as path from 'path';
import * as path from 'node:path';
import {runIntegrationTests} from '../integration/run_integration_tests.js';
import {AdkApiServer} from '../server/adk_api_server.js';
import {FileModuleType} from '../utils/agent_loader.js';
Expand Down
2 changes: 1 addition & 1 deletion integrations/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function build({
buildOptions.banner = {
js:
(buildOptions.banner?.js || '') +
`import {createRequire as topLevelCreateRequire} from 'module';\nconst require = topLevelCreateRequire(import.meta.url);`,
`import {createRequire as topLevelCreateRequire} from 'node:module';\nconst require = topLevelCreateRequire(import.meta.url);`,
};
}

Expand Down
50 changes: 50 additions & 0 deletions tests/integration/build_setup/build_banner_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {readFile} from 'node:fs/promises';
import * as path from 'node:path';
import {describe, expect, it} from 'vitest';

/**
* esbuild prepends a `createRequire` shim to every file of the `esm` and `web`
* targets so that `require` exists in ESM output. The banner is emitted
* verbatim into published artifacts, so its specifier must use the `node:`
* scheme: a bare `module` specifier can be shadowed by an npm package of the
* same name in a consumer's resolution chain.
*/
const BANNER_IMPORT =
/^import \{createRequire as topLevelCreateRequire\} from '([^']*)';/;

const BANNERED_ARTIFACTS = [
'core/dist/esm/index.js',
'core/dist/web/index_web.js',
'integrations/dist/esm/index.js',
'integrations/dist/web/index_web.js',
];

const UNBANNERED_ARTIFACTS = [
'core/dist/cjs/index.js',
'integrations/dist/cjs/index.js',
];

function readArtifact(artifact: string): Promise<string> {
return readFile(path.join(process.cwd(), artifact), 'utf8');
}

describe('generated ESM build banner', () => {
it.each(BANNERED_ARTIFACTS)('imports node:module in %s', async (artifact) => {
const [firstLine] = (await readArtifact(artifact)).split('\n');

const banner = BANNER_IMPORT.exec(firstLine);
if (!banner) {
expect.fail(`no createRequire banner in ${artifact}, got: ${firstLine}`);
}
expect(banner[1]).toBe('node:module');
});

it.each(UNBANNERED_ARTIFACTS)('emits no banner in %s', async (artifact) => {
expect(await readArtifact(artifact)).not.toContain('topLevelCreateRequire');
});
});
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import path from 'path';
import path from 'node:path';
import {defineConfig} from 'vitest/config';

/**
Expand Down
Loading