|
| 1 | +import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { createExternalRequireResolver } from '../../src/orchestrion/bundler/externalRequire'; |
| 6 | + |
| 7 | +interface ContextOptions { |
| 8 | + environment?: string; |
| 9 | + meta?: Record<string, string>; |
| 10 | + mode?: string; |
| 11 | + external?: string[] | true; |
| 12 | + resolved?: { id: string; external?: boolean } | null; |
| 13 | +} |
| 14 | + |
| 15 | +describe('createExternalRequireResolver', () => { |
| 16 | + let root: string; |
| 17 | + let pgFile: string; |
| 18 | + |
| 19 | + function installPackage(name: string, manifest: Record<string, unknown> = {}, parentDir = root): string { |
| 20 | + const dir = join(parentDir, 'node_modules', name); |
| 21 | + mkdirSync(dir, { recursive: true }); |
| 22 | + writeFileSync(join(dir, 'package.json'), JSON.stringify({ name, main: 'index.js', ...manifest })); |
| 23 | + writeFileSync(join(dir, 'index.js'), ''); |
| 24 | + return join(dir, 'index.js'); |
| 25 | + } |
| 26 | + |
| 27 | + // By default, a Rolldown build in which Vite keeps the required package external. |
| 28 | + function context({ |
| 29 | + environment = 'ssr', |
| 30 | + meta = { rolldownVersion: '1.2.14' }, |
| 31 | + mode = 'build', |
| 32 | + external = [], |
| 33 | + resolved = { id: 'external', external: true }, |
| 34 | + }: ContextOptions = {}): never { |
| 35 | + return { |
| 36 | + meta, |
| 37 | + environment: { name: environment, mode, config: { resolve: { external } } }, |
| 38 | + resolve: vi.fn().mockResolvedValue(resolved), |
| 39 | + } as never; |
| 40 | + } |
| 41 | + |
| 42 | + function resolveRequire( |
| 43 | + resolver: ReturnType<typeof createExternalRequireResolver>, |
| 44 | + source: string, |
| 45 | + importer: string, |
| 46 | + contextOptions?: ContextOptions, |
| 47 | + kind = 'require-call', |
| 48 | + ): Promise<string | null> { |
| 49 | + return resolver.call(context(contextOptions), source, importer, { kind, isEntry: false } as never); |
| 50 | + } |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + root = realpathSync(mkdtempSync(join(tmpdir(), 'orch-external-require-'))); |
| 54 | + pgFile = join(root, 'node_modules/pg/lib/client.js'); |
| 55 | + installPackage('pg'); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + rmSync(root, { recursive: true, force: true }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('bundles an external package that an instrumented package requires', async () => { |
| 63 | + const pgTypes = installPackage('pg-types'); |
| 64 | + |
| 65 | + await expect(resolveRequire(createExternalRequireResolver(['pg']), 'pg-types', pgFile)).resolves.toBe(pgTypes); |
| 66 | + }); |
| 67 | + |
| 68 | + it.each([ |
| 69 | + ['it bundled', { id: 'external', external: true }], |
| 70 | + ['Vite bundles itself', { id: 'pg-types/index.js' }], |
| 71 | + ])('follows the requires of a package that %s', async (_, resolved) => { |
| 72 | + const pgTypes = installPackage('pg-types'); |
| 73 | + const postgresArray = installPackage('postgres-array'); |
| 74 | + const resolver = createExternalRequireResolver(['pg']); |
| 75 | + |
| 76 | + await resolveRequire(resolver, 'pg-types', pgFile, { resolved }); |
| 77 | + |
| 78 | + await expect(resolveRequire(resolver, 'postgres-array', pgTypes)).resolves.toBe(postgresArray); |
| 79 | + }); |
| 80 | + |
| 81 | + // Every require() here resolves to a real file, so only the filter can return `null`. |
| 82 | + it.each([ |
| 83 | + ['app code', 'pg-types', 'src/db.js'], |
| 84 | + ['a package that orchestrion does not bundle', 'pg-types', 'node_modules/other/index.js'], |
| 85 | + ['a builtin', 'node:net', 'node_modules/pg/lib/client.js'], |
| 86 | + ['a relative path', './connection', 'node_modules/pg/lib/client.js'], |
| 87 | + ])('ignores a require() of %s', async (_, source, importer) => { |
| 88 | + installPackage('pg-types'); |
| 89 | + installPackage('other'); |
| 90 | + mkdirSync(join(root, 'node_modules/pg/lib'), { recursive: true }); |
| 91 | + writeFileSync(join(root, 'node_modules/pg/lib/connection.js'), ''); |
| 92 | + |
| 93 | + await expect( |
| 94 | + resolveRequire(createExternalRequireResolver(['pg']), source, join(root, importer)), |
| 95 | + ).resolves.toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('bundles a require() from a pnpm store path', async () => { |
| 99 | + const store = join(root, 'node_modules/.pnpm/pg@8.23.1'); |
| 100 | + installPackage('pg', {}, store); |
| 101 | + const pgTypes = installPackage('pg-types', {}, store); |
| 102 | + |
| 103 | + await expect( |
| 104 | + resolveRequire(createExternalRequireResolver(['pg']), 'pg-types', join(store, 'node_modules/pg/lib/client.js')), |
| 105 | + ).resolves.toBe(pgTypes); |
| 106 | + }); |
| 107 | + |
| 108 | + it('tracks bundled packages for each environment separately', async () => { |
| 109 | + const pgTypes = installPackage('pg-types'); |
| 110 | + installPackage('postgres-array'); |
| 111 | + const resolver = createExternalRequireResolver(['pg']); |
| 112 | + |
| 113 | + await resolveRequire(resolver, 'pg-types', pgFile, { environment: 'ssr' }); |
| 114 | + |
| 115 | + await expect(resolveRequire(resolver, 'postgres-array', pgTypes, { environment: 'nitro' })).resolves.toBeNull(); |
| 116 | + }); |
| 117 | + |
| 118 | + it.each([ |
| 119 | + ['on Rollup', { meta: { rollupVersion: '4.0.0' } }, 'require-call'], |
| 120 | + ['on the dev server', { mode: 'dev' }, 'require-call'], |
| 121 | + ['for an import', {}, 'import-statement'], |
| 122 | + ])('leaves resolution to the bundler %s', async (_, contextOptions, kind) => { |
| 123 | + installPackage('pg-types'); |
| 124 | + |
| 125 | + await expect( |
| 126 | + resolveRequire(createExternalRequireResolver(['pg']), 'pg-types', pgFile, contextOptions, kind), |
| 127 | + ).resolves.toBeNull(); |
| 128 | + }); |
| 129 | + |
| 130 | + it.each([ |
| 131 | + ['an optional peer', { peerDependenciesMeta: { 'pg-native': { optional: true } } }], |
| 132 | + ['an optional dependency', { optionalDependencies: { 'pg-native': '^3' } }], |
| 133 | + ])('keeps %s external', async (_, manifest) => { |
| 134 | + installPackage('pg', manifest); |
| 135 | + installPackage('pg-native'); |
| 136 | + |
| 137 | + await expect(resolveRequire(createExternalRequireResolver(['pg']), 'pg-native', pgFile)).resolves.toBeNull(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('keeps a scoped subpath external when the user lists its package', async () => { |
| 141 | + installPackage('@scope/pkg'); |
| 142 | + writeFileSync(join(root, 'node_modules/@scope/pkg/sub.js'), ''); |
| 143 | + |
| 144 | + await expect( |
| 145 | + resolveRequire(createExternalRequireResolver(['pg']), '@scope/pkg/sub', pgFile, { external: ['@scope/pkg'] }), |
| 146 | + ).resolves.toBeNull(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('keeps a package external that the user lists in `external`', async () => { |
| 150 | + installPackage('pg-types'); |
| 151 | + |
| 152 | + await expect( |
| 153 | + resolveRequire(createExternalRequireResolver(['pg']), 'pg-types', pgFile, { external: ['pg-types'] }), |
| 154 | + ).resolves.toBeNull(); |
| 155 | + }); |
| 156 | + |
| 157 | + it.each([ |
| 158 | + ['only a subpath of the package', ['pg-types/lib/arrays']], |
| 159 | + ['`true`, the SSR default', true], |
| 160 | + ] as const)('bundles a package despite an external entry that names %s', async (_, external) => { |
| 161 | + const pgTypes = installPackage('pg-types'); |
| 162 | + |
| 163 | + await expect( |
| 164 | + resolveRequire(createExternalRequireResolver(['pg']), 'pg-types', pgFile, { |
| 165 | + external: external === true ? true : [...external], |
| 166 | + }), |
| 167 | + ).resolves.toBe(pgTypes); |
| 168 | + }); |
| 169 | + |
| 170 | + it('keeps a package external that is not installed', async () => { |
| 171 | + await expect(resolveRequire(createExternalRequireResolver(['pg']), 'pg-types', pgFile)).resolves.toBeNull(); |
| 172 | + }); |
| 173 | +}); |
0 commit comments