|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { afterEach, describe, it, mock } from 'node:test'; |
| 3 | + |
| 4 | +import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs'; |
| 5 | + |
| 6 | +mock.module('../vite.mjs', { |
| 7 | + namedExports: { createViteBundler: () => 'vite' }, |
| 8 | +}); |
| 9 | + |
| 10 | +const { resolveBundler } = await import('../index.mjs'); |
| 11 | + |
| 12 | +const VARIABLES = [ |
| 13 | + 'ROLLDOWN_WORKER_THREADS', |
| 14 | + 'RAYON_NUM_THREADS', |
| 15 | + 'MIMALLOC_PURGE_DELAY', |
| 16 | + 'MIMALLOC_ALLOW_THP', |
| 17 | +]; |
| 18 | + |
| 19 | +const original = Object.fromEntries( |
| 20 | + VARIABLES.map(name => [name, process.env[name]]) |
| 21 | +); |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + for (const name of VARIABLES) { |
| 25 | + if (original[name] === undefined) { |
| 26 | + delete process.env[name]; |
| 27 | + } else { |
| 28 | + process.env[name] = original[name]; |
| 29 | + } |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +describe('resolveBundler', () => { |
| 34 | + it("sizes the bundler's thread pools and allocator before loading it", async () => { |
| 35 | + await setConfig({ target: ['html'], threads: 3 }); |
| 36 | + |
| 37 | + for (const name of VARIABLES) { |
| 38 | + delete process.env[name]; |
| 39 | + } |
| 40 | + |
| 41 | + assert.equal(await resolveBundler(), 'vite'); |
| 42 | + assert.equal(process.env.ROLLDOWN_WORKER_THREADS, '3'); |
| 43 | + assert.equal(process.env.RAYON_NUM_THREADS, '3'); |
| 44 | + assert.equal(process.env.MIMALLOC_PURGE_DELAY, '0'); |
| 45 | + assert.equal(process.env.MIMALLOC_ALLOW_THP, '0'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('keeps what the environment already sets', async () => { |
| 49 | + await setConfig({ target: ['html'], threads: 3 }); |
| 50 | + |
| 51 | + process.env.ROLLDOWN_WORKER_THREADS = '8'; |
| 52 | + process.env.MIMALLOC_PURGE_DELAY = '250'; |
| 53 | + |
| 54 | + await resolveBundler(); |
| 55 | + |
| 56 | + assert.equal(process.env.ROLLDOWN_WORKER_THREADS, '8'); |
| 57 | + assert.equal(process.env.MIMALLOC_PURGE_DELAY, '250'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('leaves the environment alone for a custom bundler', async () => { |
| 61 | + for (const name of VARIABLES) { |
| 62 | + delete process.env[name]; |
| 63 | + } |
| 64 | + |
| 65 | + const bundler = {}; |
| 66 | + |
| 67 | + assert.equal(await resolveBundler(bundler), bundler); |
| 68 | + |
| 69 | + for (const name of VARIABLES) { |
| 70 | + assert.equal(process.env[name], undefined); |
| 71 | + } |
| 72 | + }); |
| 73 | +}); |
0 commit comments