|
1 | 1 | import http from 'node:http'
|
2 |
| -import { describe, expect, test } from 'vitest' |
| 2 | +import path from 'node:path' |
| 3 | +import fs from 'node:fs' |
| 4 | +import { afterEach, describe, expect, test } from 'vitest' |
3 | 5 | import type { InlineConfig, PluginOption } from '..'
|
4 | 6 | import type { UserConfig, UserConfigExport } from '../config'
|
5 |
| -import { defineConfig, resolveConfig } from '../config' |
| 7 | +import { defineConfig, loadConfigFromFile, resolveConfig } from '../config' |
6 | 8 | import { resolveEnvPrefix } from '../env'
|
7 | 9 | import { createLogger, mergeConfig } from '../publicUtils'
|
8 | 10 |
|
@@ -607,3 +609,103 @@ test('preTransformRequests', async () => {
|
607 | 609 | }
|
608 | 610 | `)
|
609 | 611 | })
|
| 612 | + |
| 613 | +describe('loadConfigFromFile', () => { |
| 614 | + const root = path.resolve(__dirname, './fixtures/config/loadConfigFromFile') |
| 615 | + |
| 616 | + let writtenConfig: string | undefined |
| 617 | + afterEach(() => { |
| 618 | + if (writtenConfig) { |
| 619 | + fs.unlinkSync(path.resolve(root, writtenConfig)) |
| 620 | + } |
| 621 | + fs.unlinkSync(path.resolve(root, 'package.json')) |
| 622 | + }) |
| 623 | + |
| 624 | + const writeConfig = (fileName: string, content: string) => { |
| 625 | + fs.writeFileSync(path.resolve(root, fileName), content) |
| 626 | + writtenConfig = fileName |
| 627 | + } |
| 628 | + const writePackageJson = (typeField: string | undefined) => { |
| 629 | + fs.writeFileSync( |
| 630 | + path.resolve(root, 'package.json'), |
| 631 | + JSON.stringify({ |
| 632 | + name: '@vitejs/test-load-config-from-file', |
| 633 | + type: typeField, |
| 634 | + }), |
| 635 | + ) |
| 636 | + } |
| 637 | + |
| 638 | + const canLoadConfig = async () => { |
| 639 | + const result = await loadConfigFromFile( |
| 640 | + { command: 'build', mode: 'production' }, |
| 641 | + undefined, |
| 642 | + root, |
| 643 | + ) |
| 644 | + expect(result).toBeTruthy() |
| 645 | + expect(result?.config).toStrictEqual({ define: { foo: 1 } }) |
| 646 | + expect(path.normalize(result!.path)).toBe( |
| 647 | + path.resolve(root, writtenConfig!), |
| 648 | + ) |
| 649 | + } |
| 650 | + |
| 651 | + const cases = [ |
| 652 | + { |
| 653 | + fileName: 'vite.config.js', |
| 654 | + content: 'export default { define: { foo: 1 } }', |
| 655 | + }, |
| 656 | + { |
| 657 | + fileName: 'vite.config.js', |
| 658 | + content: 'export default { define: { foo: 1 } }', |
| 659 | + }, |
| 660 | + { |
| 661 | + fileName: 'vite.config.cjs', |
| 662 | + content: 'module.exports = { define: { foo: 1 } }', |
| 663 | + }, |
| 664 | + { |
| 665 | + fileName: 'vite.config.cjs', |
| 666 | + content: 'module.exports = { define: { foo: 1 } }', |
| 667 | + }, |
| 668 | + { |
| 669 | + fileName: 'vite.config.mjs', |
| 670 | + content: 'export default { define: { foo: 1 } }', |
| 671 | + }, |
| 672 | + { |
| 673 | + fileName: 'vite.config.mjs', |
| 674 | + content: 'export default { define: { foo: 1 } }', |
| 675 | + }, |
| 676 | + { |
| 677 | + fileName: 'vite.config.ts', |
| 678 | + content: 'export default { define: { foo: 1 as number } }', |
| 679 | + }, |
| 680 | + { |
| 681 | + fileName: 'vite.config.ts', |
| 682 | + content: 'export default { define: { foo: 1 as number } }', |
| 683 | + }, |
| 684 | + { |
| 685 | + fileName: 'vite.config.mts', |
| 686 | + content: 'export default { define: { foo: 1 as number } }', |
| 687 | + }, |
| 688 | + { |
| 689 | + fileName: 'vite.config.mts', |
| 690 | + content: 'export default { define: { foo: 1 as number } }', |
| 691 | + }, |
| 692 | + { |
| 693 | + fileName: 'vite.config.cts', |
| 694 | + content: 'module.exports = { define: { foo: 1 as number } }', |
| 695 | + }, |
| 696 | + { |
| 697 | + fileName: 'vite.config.cts', |
| 698 | + content: 'module.exports = { define: { foo: 1 as number } }', |
| 699 | + }, |
| 700 | + ] |
| 701 | + |
| 702 | + for (const { fileName, content } of cases) { |
| 703 | + for (const typeField of [undefined, 'module']) { |
| 704 | + test(`load ${fileName}${typeField ? ' with package#type module' : ''}`, async () => { |
| 705 | + writePackageJson(typeField) |
| 706 | + writeConfig(fileName, content) |
| 707 | + await canLoadConfig() |
| 708 | + }) |
| 709 | + } |
| 710 | + } |
| 711 | +}) |
0 commit comments