简体   繁体   English

Webpack-动态需求和路径

[英]Webpack - dynamic require and paths

My webpack.config file looks like this 我的webpack.config文件如下所示

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: {
        app: './app'
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: '[name]-bundle.js'
    }
};

I have some dynamic requires that look like this 我有一些动态要求,看起来像这样

function loadModule(modName, cb, onErr){
    if (modName == 'contacts') require(['../modules/contacts/contacts', 'html!../modules/contacts/contacts.htm'], cb);
    else if (modName == 'tasks') require(['../modules/tasks/tasks', 'html!../modules/tasks/tasks.htm'], cb);
    else onErr();
}

Which work great. 哪个工作很棒。 I'm seeing 1.1-bundle.js and 2.2-bundle.js getting added to my build folder . 我看到1.1-bundle.js2.2-bundle.js添加到我的build文件夹中

The problem is, when these dynamic requires are triggered, I'm seeing 404s in my network tab, since webpack is trying to load 1-1.bundle.js from the root of my project, instead of the build folder where I told webpack to put it. 问题是,当触发这些动态要求时,我在网络选项卡中看到404,因为webpack试图从项目的根目录而不是我告诉webpack的build文件夹中加载1-1.bundle.js。把它。

How do I correct this? 我该如何纠正?

You probably need to set the public path - basically, you tell webpack where on your webserver it will find the built files, which for you seems to the the build folder. 您可能需要设置公共路径 -基本上,您告诉webpack在Web服务器上的什么位置可以找到生成的文件,对于您来说,在build文件夹中似乎是如此。

{
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: '[name]-bundle.js',
        publicPath: '/build/',
    },
}

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

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