|
1 | 1 | import { resolve } from 'node:path'
|
2 | 2 | import { fileURLToPath } from 'node:url'
|
| 3 | +import colors from 'picocolors' |
3 | 4 | import type { Logger } from 'vite'
|
4 | 5 | import { describe, expect, test, vi } from 'vitest'
|
5 | 6 | import type { OutputOptions } from 'rollup'
|
@@ -350,18 +351,92 @@ describe('resolveBuildOutputs', () => {
|
350 | 351 | name: 'entryA'
|
351 | 352 | }
|
352 | 353 |
|
353 |
| - const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger) |
354 |
| - |
355 |
| - expect(outputs).toEqual([{ format: 'es' }, { format: 'umd' }]) |
| 354 | + expect(resolveBuildOutputs(undefined, libOptions, {} as Logger)).toEqual([ |
| 355 | + { format: 'es' }, |
| 356 | + { format: 'umd' } |
| 357 | + ]) |
| 358 | + expect( |
| 359 | + resolveBuildOutputs({ name: 'A' }, libOptions, {} as Logger) |
| 360 | + ).toEqual([ |
| 361 | + { format: 'es', name: 'A' }, |
| 362 | + { format: 'umd', name: 'A' } |
| 363 | + ]) |
| 364 | + expect( |
| 365 | + resolveBuildOutputs([{ name: 'A' }], libOptions, {} as Logger) |
| 366 | + ).toEqual([{ name: 'A' }]) |
356 | 367 | })
|
357 | 368 |
|
358 | 369 | test('default format: multiple entries', () => {
|
359 | 370 | const libOptions: LibraryOptions = {
|
360 | 371 | entry: ['entryA.js', 'entryB.js']
|
361 | 372 | }
|
362 | 373 |
|
363 |
| - const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger) |
| 374 | + expect(resolveBuildOutputs(undefined, libOptions, {} as Logger)).toEqual([ |
| 375 | + { format: 'es' }, |
| 376 | + { format: 'cjs' } |
| 377 | + ]) |
| 378 | + expect( |
| 379 | + resolveBuildOutputs({ name: 'A' }, libOptions, {} as Logger) |
| 380 | + ).toEqual([ |
| 381 | + { format: 'es', name: 'A' }, |
| 382 | + { format: 'cjs', name: 'A' } |
| 383 | + ]) |
| 384 | + expect( |
| 385 | + resolveBuildOutputs([{ name: 'A' }], libOptions, {} as Logger) |
| 386 | + ).toEqual([{ name: 'A' }]) |
| 387 | + }) |
| 388 | + |
| 389 | + test('umd or iife: should not support multiple entries', () => { |
| 390 | + ;['umd', 'iife'].forEach((format) => { |
| 391 | + expect(() => |
| 392 | + resolveBuildOutputs( |
| 393 | + undefined, |
| 394 | + { |
| 395 | + entry: ['entryA.js', 'entryB.js'], |
| 396 | + formats: [format as LibraryFormats] |
| 397 | + }, |
| 398 | + {} as Logger |
| 399 | + ) |
| 400 | + ).toThrow( |
| 401 | + `Multiple entry points are not supported when output formats include "umd" or "iife".` |
| 402 | + ) |
| 403 | + }) |
| 404 | + }) |
364 | 405 |
|
365 |
| - expect(outputs).toEqual([{ format: 'es' }, { format: 'cjs' }]) |
| 406 | + test('umd or iife: should define build.lib.name', () => { |
| 407 | + ;['umd', 'iife'].forEach((format) => { |
| 408 | + expect(() => |
| 409 | + resolveBuildOutputs( |
| 410 | + undefined, |
| 411 | + { |
| 412 | + entry: 'entryA.js', |
| 413 | + formats: [format as LibraryFormats] |
| 414 | + }, |
| 415 | + {} as Logger |
| 416 | + ) |
| 417 | + ).toThrow( |
| 418 | + `Option "build.lib.name" is required when output formats include "umd" or "iife".` |
| 419 | + ) |
| 420 | + }) |
| 421 | + }) |
| 422 | + |
| 423 | + test('array outputs: should ignore build.lib.formats', () => { |
| 424 | + // @ts-expect-error mock Logger |
| 425 | + const log = { warn: vi.fn() } as Logger |
| 426 | + expect( |
| 427 | + resolveBuildOutputs( |
| 428 | + [{ name: 'A' }], |
| 429 | + { |
| 430 | + entry: 'entryA.js', |
| 431 | + formats: ['es'] |
| 432 | + }, |
| 433 | + log |
| 434 | + ) |
| 435 | + ).toEqual([{ name: 'A' }]) |
| 436 | + expect(log.warn).toHaveBeenLastCalledWith( |
| 437 | + colors.yellow( |
| 438 | + `"build.lib.formats" will be ignored because "build.rollupOptions.output" is already an array format.` |
| 439 | + ) |
| 440 | + ) |
366 | 441 | })
|
367 | 442 | })
|
0 commit comments