简体   繁体   English

Webpack 5 in Ceate React App can't resolve not fully specified routes

[英]Webpack 5 in Ceate React App can't resolve not fully specified routes

We are developing a react library and recently noticed, that it throws an error when we want to consume it with a new (webpack 5) Create React App.我们正在开发一个 React 库,最近注意到,当我们想要使用新的 (webpack 5) Create React App 使用它时,它会抛出一个错误。 It complains about this:它抱怨这个:

"BREAKING CHANGE: The request failed to resolve only because it was resolved as fully specified (probably because the origin is strict EcmaScript Module, eg a module with javascript mimetype, a '.mjs' file, or a '.js' file where the package.json contains '"type": "module"')." “重大变化:请求解析失败只是因为它被解析为完全指定的(可能是因为来源是严格的 EcmaScript 模块,例如具有 javascript mimetype 的模块,'.mjs' 文件或'.js' 文件,其中package.json 包含“类型”:“模块”)。

I managed to fix this, by adding我设法通过添加来解决这个问题

{
    test: /\.m?js/,
    resolve: {
      fullySpecified: false,
    },
}

While this works fine for any applications that have access to the webpack config, we'd like to make our lib "plug&play", without any eject or additional setup.虽然这适用于任何有权访问 webpack 配置的应用程序,但我们希望让我们的库“即插即用”,无需任何弹出或额外设置。 What config should we have to make the consumer's webpack resolve our not fully specified routes?我们需要什么样的配置才能使消费者的 webpack 解析我们未完全指定的路由? Code on github密码github

Thanks in advance提前致谢

I solved this without ejecting or modifying files in node_modules.我在没有弹出或修改 node_modules 中的文件的情况下解决了这个问题。 First, add craco to override the webpack config: yarn add -D @craco/craco .首先,添加 craco 以覆盖 webpack 配置: yarn add -D @craco/craco Next, in the root of your project (where package.json is located) create a file named craco.config.js with the following content:接下来,在项目的根目录中( package.json所在的位置)创建一个名为craco.config.js的文件,其中包含以下内容:

module.exports = {
    webpack: {
        configure: {
            module: {
                rules: [
                    {
                        test: /\.m?js$/,
                        resolve: {
                            fullySpecified: false,
                        },
                    },
                ],
            },
        },
    },
};

This config disables the breaking change that causes this error.此配置禁用导致此错误的重大更改。 Then change the start/build/test commands in package.json replacing react-scripts to craco :然后更改package.json中的启动/构建/测试命令,将react-scripts替换为craco

"scripts": {
        "start": "craco start",
        "build": "craco build",
        "test": "craco test",

Now do the usual yarn start and it should work.现在做通常的yarn start ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM