跳至主要内容

其他

其他有用的工具函数

const { throwUnexpectedConfigError } = require('@craco/craco');

函数

throwUnexpectedConfigError

throwUnexpectedConfigError(options)

引发错误并使 Node.js 崩溃。

options 应为一个具有以下结构的对象

属性说明类型/格式必需
message解释出错原因的错误消息字符串
packageNamenpm 包名称字符串
githubRepo人们可以打开一个 issue 的 GitHub 仓库字符串: 用户名/仓库
githubIssueQuery用于查找相关问题的搜索字符串字符串
提示

如果配置更改且不符合你的预期,则抛出错误。(例如,getLoader 无法找到加载器且 isFound 为 false。)Create React App 可能会更新其 webpack 配置的结构,因此当出现故障时,显示有用的错误消息非常重要。

示例

$ yarn start
yarn run v1.12.3
$ craco start
/path/to/your/app/craco.config.js:23
throw new Error(
^

Error: Can't find eslint-loader in the webpack config!

This error probably occurred because you updated react-scripts or craco. Please try updating craco-less to the latest version:

$ yarn upgrade craco-less

Or:

$ npm update craco-less

If that doesn't work, craco-less needs to be fixed to support the latest version.
Please check to see if there's already an issue in the ndbroadbent/craco-less repo:

* https://github.com/DocSpring/craco-less/issues?q=is%3Aissue+webpack+eslint-loader

If not, please open an issue and we'll take a look. (Or you can send a PR!)

You might also want to look for related issues in the craco and create-react-app repos:

* https://github.com/dilanx/craco/issues?q=is%3Aissue+webpack+eslint-loader
* https://github.com/facebook/create-react-app/issues?q=is%3Aissue+webpack+eslint-loader

at throwUnexpectedConfigError (/path/to/your/app/craco.config.js:23:19)
...

用法

const {
getLoader,
loaderByName,
throwUnexpectedConfigError,
} = require('@craco/craco');

// Create a helper function if you need to call this multiple times
const throwError = (message, githubIssueQuery) =>
throwUnexpectedConfigError({
packageName: 'craco-less',
githubRepo: 'ndbroadbent/craco-less',
message,
githubIssueQuery,
});

const { isFound, match } = getLoader(
webpackConfig,
loaderByName('eslint-loader')
);

if (!isFound) {
throwError(
"Can't find eslint-loader in the webpack config!",
'webpack+eslint-loader'
);
}