|
1 | 1 | import {expect, test} from 'vitest'; |
2 | 2 | import type {TestProjectInlineConfiguration} from 'vitest/config'; |
3 | 3 | import {getOcularConfig, getVitestConfig} from '@vis.gl/dev-tools'; |
4 | | -import {getBundleConfig, parseBundleArguments} from '../src/configuration/get-esbuild-config.js'; |
| 4 | +import { |
| 5 | + getBundleConfig, |
| 6 | + getCJSExportConfig, |
| 7 | + parseBundleArguments |
| 8 | +} from '../src/configuration/get-esbuild-config.js'; |
5 | 9 | import {execFileSync} from 'node:child_process'; |
6 | | -import {mkdtempSync, readFileSync, rmSync, writeFileSync} from 'node:fs'; |
| 10 | +import {build} from 'esbuild'; |
| 11 | +import {mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync} from 'node:fs'; |
7 | 12 | import {tmpdir} from 'node:os'; |
8 | 13 | import path from 'node:path'; |
9 | 14 | import {fileURLToPath} from 'node:url'; |
@@ -119,6 +124,55 @@ test('dev-tools#getBundleConfig passes normalized externals to esbuild', async ( |
119 | 124 | expect(config.alias).not.toHaveProperty('@vis.gl/dev-tools/test'); |
120 | 125 | }); |
121 | 126 |
|
| 127 | +test('dev-tools#getCJSExportConfig neutralizes tsconfig paths', async () => { |
| 128 | + const config = await getCJSExportConfig({ |
| 129 | + input: './dist/index.js', |
| 130 | + output: './dist/index.cjs' |
| 131 | + }); |
| 132 | + |
| 133 | + expect(config.packages).toBe('external'); |
| 134 | + expect(config.tsconfigRaw).toEqual({compilerOptions: {paths: {}}}); |
| 135 | +}); |
| 136 | + |
| 137 | +test('dev-tools#getCJSExportConfig keeps workspace packages out of the bundle', async () => { |
| 138 | + const fixtureDirectory = mkdtempSync(path.join(tmpdir(), 'ocular-cjs-')); |
| 139 | + |
| 140 | + try { |
| 141 | + // A monorepo that maps its own scope to sibling sources, as vis.gl repos do. |
| 142 | + writeFileSync( |
| 143 | + path.join(fixtureDirectory, 'tsconfig.json'), |
| 144 | + JSON.stringify({ |
| 145 | + compilerOptions: { |
| 146 | + moduleResolution: 'bundler', |
| 147 | + paths: {'@example/sibling': ['./sibling/src/index.js']} |
| 148 | + } |
| 149 | + }) |
| 150 | + ); |
| 151 | + mkdirSync(path.join(fixtureDirectory, 'sibling', 'src'), {recursive: true}); |
| 152 | + writeFileSync( |
| 153 | + path.join(fixtureDirectory, 'sibling', 'src', 'index.js'), |
| 154 | + "export const sibling = 'incorrectly bundled';\n" |
| 155 | + ); |
| 156 | + mkdirSync(path.join(fixtureDirectory, 'dist'), {recursive: true}); |
| 157 | + writeFileSync( |
| 158 | + path.join(fixtureDirectory, 'dist', 'index.js'), |
| 159 | + "import {sibling} from '@example/sibling';\nexport const value = sibling;\n" |
| 160 | + ); |
| 161 | + |
| 162 | + const config = await getCJSExportConfig({ |
| 163 | + input: path.join(fixtureDirectory, 'dist', 'index.js'), |
| 164 | + output: path.join(fixtureDirectory, 'dist', 'index.cjs') |
| 165 | + }); |
| 166 | + await build({...config, logLevel: 'silent'}); |
| 167 | + |
| 168 | + const output = readFileSync(path.join(fixtureDirectory, 'dist', 'index.cjs'), 'utf8'); |
| 169 | + expect(output).toContain('require("@example/sibling")'); |
| 170 | + expect(output).not.toContain('incorrectly bundled'); |
| 171 | + } finally { |
| 172 | + rmSync(fixtureDirectory, {recursive: true, force: true}); |
| 173 | + } |
| 174 | +}); |
| 175 | + |
122 | 176 | test('ocular-bundle CLI bundles the requested entry point', () => { |
123 | 177 | const fixtureDirectory = mkdtempSync(path.join(tmpdir(), 'ocular-bundle-')); |
124 | 178 | const scriptPath = fileURLToPath(new URL('../scripts/bundle.js', import.meta.url)); |
|
0 commit comments