MCPcopy Create free account
hub / github.com/webpack/webpack-cli / loadConfig

Method loadConfig

packages/webpack-cli/src/webpack-cli.ts:2836–3420  ·  view source on GitHub ↗
(options: Options)

Source from the content-addressed store, hash-verified

2834 }
2835
2836 async loadConfig(options: Options) {
2837 const disableInterpret =
2838 typeof options.disableInterpret !== "undefined" && options.disableInterpret;
2839
2840 const loadConfigByPath = async (
2841 configPath: string,
2842 argv: Argv = { env: {} },
2843 ): Promise<{ options: Configuration | MultiConfiguration; path: string }> => {
2844 let options: LoadableWebpackConfiguration | undefined;
2845
2846 const isFileURL = configPath.startsWith("file://");
2847
2848 try {
2849 const ext = path.extname(configPath).toLowerCase();
2850 const dataFormatLoader = DATA_FORMAT_LOADERS[ext];
2851
2852 if (dataFormatLoader) {
2853 // Non-JavaScript formats cannot be `import(...)`ed, so parse them
2854 // directly and skip the doomed dynamic import attempt entirely.
2855 const configFilePath = isFileURL ? fileURLToPath(configPath) : path.resolve(configPath);
2856 let parser: Record<string, (source: string) => unknown>;
2857
2858 try {
2859 // Resolve the parser from the configuration file's location (so it
2860 // is picked up from the user's project rather than from webpack-cli),
2861 // then load it asynchronously via `import(...)`.
2862 const parserPath = createRequire(configFilePath).resolve(dataFormatLoader.package);
2863 const parserModule = await import(pathToFileURL(parserPath).href);
2864
2865 parser = (parserModule.default ?? parserModule) as Record<
2866 string,
2867 (source: string) => unknown
2868 >;
2869 } catch {
2870 this.logger.error(`Unable to load the '${configFilePath}' configuration file.`);
2871 this.logger.error(
2872 `Loading '${ext}' configuration files requires the '${dataFormatLoader.package}' package, which is not installed. Please install it, e.g. \`npm install --save-dev ${dataFormatLoader.package}\`.`,
2873 );
2874 process.exit(2);
2875 }
2876
2877 // Reading/parsing errors propagate to the handler below, which
2878 // reports `Failed to load '<config>' config`.
2879 const source = await fs.promises.readFile(configFilePath, "utf8");
2880
2881 options = parser[dataFormatLoader.method](source) as LoadableWebpackConfiguration;
2882 } else {
2883 let loadingError;
2884
2885 try {
2886 options = // eslint-disable-next-line no-eval
2887 (await eval(`import("${isFileURL ? configPath : pathToFileURL(configPath)}")`))
2888 .default;
2889 } catch (err) {
2890 if (this.isValidationError(err) || process.env?.WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG) {
2891 throw err;
2892 }
2893

Callers 2

WebpackCLIClass · 0.95
createCompilerMethod · 0.95

Calls 5

isPackageInstalledMethod · 0.95
installPackageMethod · 0.95
getMethod · 0.80

Tested by

no test coverage detected