(
overrides: SnowpackUserConfig = {},
configPath?: string,
)
| 787 | } |
| 788 | |
| 789 | export async function loadConfiguration( |
| 790 | overrides: SnowpackUserConfig = {}, |
| 791 | configPath?: string, |
| 792 | ): Promise<SnowpackConfig> { |
| 793 | let result: Awaited<ReturnType<typeof loadConfigurationFile>> = null; |
| 794 | // if user specified --config path, load that |
| 795 | if (configPath) { |
| 796 | result = await loadConfigurationFile(configPath, overrides); |
| 797 | if (!result) { |
| 798 | throw new Error(`Snowpack config file could not be found: ${configPath}`); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | const configs = [ |
| 803 | 'snowpack.config.mjs', |
| 804 | 'snowpack.config.cjs', |
| 805 | 'snowpack.config.js', |
| 806 | 'snowpack.config.json', |
| 807 | ]; |
| 808 | |
| 809 | // If no config was found above, search for one. |
| 810 | if (!result) { |
| 811 | for (const potentialConfigurationFile of configs) { |
| 812 | if (result) break; |
| 813 | result = await loadConfigurationFile(potentialConfigurationFile, overrides); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | // Support package.json "snowpack" config |
| 818 | if (!result) { |
| 819 | const potentialPackageJsonConfig = await loadConfigurationFile('package.json', overrides); |
| 820 | if (potentialPackageJsonConfig && (potentialPackageJsonConfig.config as any).snowpack) { |
| 821 | result = { |
| 822 | filepath: potentialPackageJsonConfig.filepath, |
| 823 | config: (potentialPackageJsonConfig.config as any).snowpack, |
| 824 | }; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (!result) { |
| 829 | logger.warn('Hint: run "snowpack init" to create a project config file. Using defaults...'); |
| 830 | result = {filepath: undefined, config: {}}; |
| 831 | } |
| 832 | |
| 833 | const {config, filepath} = result; |
| 834 | const configBase = getConfigBasePath(filepath, config.root); |
| 835 | valdiateDeprecatedConfig(config); |
| 836 | valdiateDeprecatedConfig(overrides); |
| 837 | resolveRelativeConfig(config, configBase); |
| 838 | |
| 839 | let extendConfig: SnowpackUserConfig = {} as SnowpackUserConfig; |
| 840 | if (config.extends) { |
| 841 | const extendConfigLoc = require.resolve(config.extends, {paths: [configBase]}); |
| 842 | const extendResult = await loadConfigurationFile(extendConfigLoc, {}); |
| 843 | if (!extendResult) { |
| 844 | handleConfigError(`Could not locate "extends" config at ${extendConfigLoc}`); |
| 845 | process.exit(1); |
| 846 | } |
no test coverage detected