简体   繁体   English

Webpack中的动态需求

[英]Dynamic require in webpack

i have directory with files. 我有文件目录。

/web/public/js/core /网络/公/ JS /核心

My core folder consist of: modules, helper, config, styl, core.js, base.js, service.js. 我的核心文件夹包括:模块,助手,配置,样式,core.js,base.js,service.js。

I use webpack for build and in my service.js i use dynamic require. 我使用webpack进行构建,在service.js中使用动态需求。

For example: 例如:

config.js config.js

export default {
    modules: {
        validation: 'validation',
        async: 'asyncLoadContent',
        carousel: 'carousel',
        tab: 'tab',
        accordeon: 'accordeon',
        modal: 'modal',
        lightbox: 'lightbox',
        zoom: 'zoom',
        notification: 'notification',
        slider: 'slider',
        rates: 'rates'
    },
    helper: {
        event: 'event',
        css: 'css',
        error: 'error',
        transition: 'transition'
    }

}

service.js In this file I get the Json object from the configuration file I go through it recursively and form the path for loading the module or helper. service.js在此文件中,我递归地从配置文件中获取Json对象,并形成用于加载模块或帮助程序的路径。 Accordingly, the modules are located along the path /core/helper, and the modules along the path /core/modules, that is, the directories are different and the path is dynamic. 因此,模块沿着路径/ core / helper定位,而模块沿着路径/ core / modules定位,即目录不同且路径是动态的。

registrationModule(config, option){

        for(let key in config){

            if(typeof config[key] == 'object' && !this.classes[key]){
                this.classes[key] = {};
                this.registrationModule(config[key], key);
            } else {

                let url = (option == 'modules') ? option + '/' + key + '/' + config[key] : option + '/' + config[key];

                //!!!!! PROBLEM !!!!!!!/
                let module = require("./" + url + '.js').default;

                this.setModule(key, option, module);
            }

        }

    }

Before build i have Warning 在构建之前,我有警告

WARNING in ./web/public/js/core ^\.\/.*\.js$
Module not found: Error: a dependency to an entry point is not allowed
 @ ./web/public/js/core ^\.\/.*\.js$

And in build i see: 在构建中,我看到:

var map = {
        "./base.js": 24,
        "./config/config.js": 26,
        "./helper/css.js": 30,
        "./helper/error.js": 27,
        "./helper/event.js": 31,
        "./helper/transition.js": 32,
        "./modules/accordeon/accordeon.js": 33,
        "./modules/accordeon/accordeon.md.js": 34,
        "./modules/async/asyncLoadContent.js": 35,
        "./modules/carousel/carousel.js": 36,
        "./modules/carousel/carousel.md.js": 37,
        "./modules/lightbox/lightbox.js": 38,
        "./modules/lightbox/lightbox.md.js": 39,
        "./modules/lightbox/lightbox.view.js": 40,
        "./modules/modal/modal.js": 41,
        "./modules/modal/modal.md.js": 42,
        "./modules/modal/modal.view.js": 43,
        "./modules/notification/notification.js": 44,
        "./modules/notification/notification.md.js": 45,
        "./modules/rates/rates.js": 46,
        "./modules/rates/rates.md.js": 47,
        "./modules/slider/slider.js": 48,
        "./modules/slider/slider.md.js": 49,
        "./modules/tab/tab.js": 50,
        "./modules/tab/tab.md.js": 51,
        "./modules/validation/validation.js": 52,
        "./modules/validation/validation.md.js": 53,
        "./modules/zoom/zoom.js": 54,
        "./modules/zoom/zoom.md.js": 55,
        "./modules/zoom/zoom.view.js": 56,
        "./service.js": 25
    }; 

Map consist of: "./base.js": 24, "./service.js": 25. 地图包含:“ ./ base.js”:24,“ ./ service.js”:25。

I understand from what error in the console but how to fix it? 我从控制台中的错误中了解到,但是如何解决呢? How to let understand webpack so that it looks for files only by the way that I want when dynamically forming the path.??? 如何让webpack理解,以便它在动态形成路径时仅按照我想要的方式查找文件。

For this specific case, you should be able to work around this with code that makes it clear to Webpack that you only care about helpers and modules, eg 对于这种特定情况,您应该能够使用代码解决此问题,该代码使Webpack清楚地知道您只在乎助手和模块,例如

let module;
if (option === "modules") {
    module = require("./modules/" + key + '/' + config[key] + '.js');
} else if (option === "helper") {
    module = require("./helper/" + key + '.js');
} else {
    throw new Error("Unexpected option value;");
}

this.setModule(key, option, module.default);

because the require calls have a specific prefix that Webpack can pick up, it will know you don't care about the root-level files. 因为require调用具有Webpack可以选择的特定前缀,所以它将知道您不在乎根级文件。

Just note however, building custom registration like this makes your dependency graph super hard to follow and makes working with code like this really annoying. 不过请注意,像这样构建自定义注册会使您的依赖图变得非常难以遵循,并且使使用此类代码的工作变得非常烦人。

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

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