错误:加载块0失败。 - webpack尝试加载0.js [英] Error: Loading chunk 0 failed. - webpack tries to load 0.js

查看:149
本文介绍了错误:加载块0失败。 - webpack尝试加载0.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下项目结构:

|-mainFile.js
|-scripts -
          |-Library1.js
          |-Library2.js

库文件使用requirejs define([],function(){})语法。

Library files use requirejs define([], function() {}) syntax.

因此我把它放到了webpack.config中。 js:

I've therefore put this into the webpack.config.js:

module.exports = {
    resolve: {
        modules: [
            path.resolve(__dirname, 'scripts'),
            // The normal path
            "node_modules"
        ]
    },
    /** ... **/
}

然后我在 mainFile.js中执行此操作这是webpack的入口点:

I then do this in mainFile.js which is entry point for webpack:

require(["Library1", "Library2"], function(Library1, Library2) { ... });

我收到此错误:

GET http://localhost:3000/0.js [HTTP/1.1 404 Not Found 3ms]
Error: Loading chunk 0 failed.
Stack trace:
onScriptComplete@http://localhost:3000/player/webpack_build.js:100:24
                     webpack_build.js:146:52
The resource from "http://localhost:3000/0.js" was blocked due to MIME type mismatch (X-Content-Type-Options: nosniff).[Learn More]
test_file.html Error: Loading chunk 0 failed.

所以webpack尝试加载一些 0.js 文件。应该是什么?

So webpack tries to load some 0.js file. What's that supposed to be?

推荐答案

我想出了一个解决方案,我希望它有所帮助。

I have figured out a fix, I hope it helps.

对于我的React Routes,我使用带有import语句的动态加载,没有CommonChunks插件。

For my React Routes, I'm using dynamic loading with import statements, without the CommonChunks plugin.

我得到了相同的错误(或块1,块2等),具体取决于我加载的路线。我终于意识到我的资产包实际上被输出到我的html所在的当前目录中,即使我的output.path指向assets文件夹。

I was getting the same error (or "Chunk 1", "Chunk 2", etc) depending on the route I was loading. I finally realized that my asset bundles were actually being outputted into the current directory that my html was in, even though my output.path was pointing to the assets folder.

要解决此问题,我只需将output.chunkFilename设置为'../../../assets/js/com/[name].bundle.js ',然后将其输出到正确的文件夹。

To fix this, I just set my output.chunkFilename to '../../../assets/js/com/[name].bundle.js', which then output it to the correct folder.

从那时起,我的应用程序能够找到我的捆绑包并且效果很好!

From then on, my app was able to find my bundles and it worked great!

希望有所帮助,
Michael

Hope it helps, Michael

这篇关于错误:加载块0失败。 - webpack尝试加载0.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆