|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | + |
| 5 | +/** |
| 6 | + * Real, unmocked test: getBaseBackendBuildConfig must produce a working |
| 7 | + * bundle for a backend function that imports a real Node built-in module |
| 8 | + * (e.g. node:crypto), not the browser-target `__vite-browser-external:*` |
| 9 | + * externalization stub Vite falls back to when `build.ssr` isn't set. |
| 10 | + * |
| 11 | + * Discovered while building a local-execution POC: without `ssr: true`, |
| 12 | + * Vite defaults to a browser-target build, and any `*.backend.ts` file that |
| 13 | + * imports a real Node built-in fails to bundle correctly -- this affects |
| 14 | + * dev-server.ts's existing bundleBackendFunction() today (both the current |
| 15 | + * cloud round-trip and any future local-execution path), not just new code. |
| 16 | + */ |
| 17 | + |
| 18 | +import { outputFileSync } from '@dd/core/helpers/fs'; |
| 19 | +import { getTempWorkingDir } from '@dd/tests/_jest/helpers/env'; |
| 20 | +import { build } from 'vite'; |
| 21 | + |
| 22 | +import { getBaseBackendBuildConfig } from './build-config'; |
| 23 | + |
| 24 | +describe('getBaseBackendBuildConfig', () => { |
| 25 | + test('bundles a backend function that imports a real Node builtin module with a working import, not a browser-external stub', async () => { |
| 26 | + const workingDir = getTempWorkingDir(`build-config-ssr-${Date.now()}`); |
| 27 | + const absolutePath = `${workingDir}/src/usesCrypto.backend.ts`; |
| 28 | + |
| 29 | + outputFileSync( |
| 30 | + absolutePath, |
| 31 | + ` |
| 32 | + import { randomBytes } from 'node:crypto'; |
| 33 | + export async function usesCrypto() { |
| 34 | + return randomBytes(4).toString('hex'); |
| 35 | + } |
| 36 | + `, |
| 37 | + ); |
| 38 | + |
| 39 | + const virtualId = 'virtual:dd-backend-test:usesCrypto'; |
| 40 | + const virtualContent = `import { usesCrypto } from ${JSON.stringify(absolutePath)};\nexport async function main($) { return await usesCrypto(); }`; |
| 41 | + const baseConfig = getBaseBackendBuildConfig( |
| 42 | + workingDir, |
| 43 | + { [virtualId]: virtualContent }, |
| 44 | + [], |
| 45 | + ); |
| 46 | + |
| 47 | + const result = await build({ |
| 48 | + ...baseConfig, |
| 49 | + build: { |
| 50 | + ...baseConfig.build, |
| 51 | + write: false, |
| 52 | + rollupOptions: { |
| 53 | + ...baseConfig.build.rollupOptions, |
| 54 | + input: virtualId, |
| 55 | + output: baseConfig.build.rollupOptions.output, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + const output = Array.isArray(result) ? result[0] : result; |
| 61 | + if (!('output' in output)) { |
| 62 | + throw new Error('Unexpected vite.build result'); |
| 63 | + } |
| 64 | + const chunk = output.output[0]; |
| 65 | + const code = chunk.type === 'chunk' ? chunk.code : ''; |
| 66 | + |
| 67 | + // Without `ssr: true`, Vite externalizes node:crypto to |
| 68 | + // `__vite-browser-external:node:crypto`, which has no real exports -- |
| 69 | + // calling randomBytes() from it throws at runtime, and the import |
| 70 | + // specifier itself is rewritten away from 'node:crypto'. Assert the |
| 71 | + // real, working import survived instead. |
| 72 | + expect(code).toContain("from 'node:crypto'"); |
| 73 | + expect(code).not.toContain('__vite-browser-external'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments