访问RequireJS路径配置 [英] Access RequireJS path configuration

查看:44
本文介绍了访问RequireJS路径配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档中注意到有一种方法可以将自定义配置传递到模块中:

I notice in the documentation there is a way to pass custom configuration into a module:

requirejs.config({
    baseUrl: './js',
    paths: {
        jquery: 'libs/jquery-1.9.1',
        jqueryui: 'libs/jquery-ui-1.9.2'
    },
    config: {
        'baz': {
            color: 'blue'
        }
    }
});

然后您可以从模块访问:

Which you can then access from the module:

define(['module'], function (module) {        
    var color = module.config().color; // 'blue'
});

但是有没有办法访问顶级路径配置,像这样?

But is there also a way to access the top-level paths configuration, something like this?

define(['module', 'require'], function (module, require) {        
    console.log( module.paths() ); // no method paths()
    console.log( require.paths() ); // no method paths()
});

仅供参考,这不适用于生产站点.我正在尝试将一些奇怪的调试/配置代码连接到 QUnit 测试页面中.我想枚举哪些模块名称定义了自定义路径.这个问题触及了这个问题,但只让我查询已知模块,而不是枚举它们.

FYI, this is not for a production site. I'm trying to wire together some odd debug/config code inside a QUnit test page. I want to enumerate which module names have a custom path defined. This question touched on the issue but only lets me query known modules, not enumerate them.

推荐答案

我不相信 require 会在任何地方公开它,至少我在庞大的代码库中找不到它.有两种方法可以实现这一点.第一个也是最明显的是将配置定义为全局变量.第二个,也更接近你想要的,是创建一个 require 插件,它覆盖加载函数以将配置附加到模块:

I don't believe require exposes that anywhere, at least I can't find it looking through the immense codebase. There are two ways you could achieve this though. The first and most obvious is to define the config as a global variable. The second, and closer to what you want, is to create a require plugin that overrides the load function to attach the config to the module:

define({
    load: function (name, req, onload, config) {
        req([name], function (value) {
            value.requireConfig = config;
            onload(value);
        });
    }
});

这篇关于访问RequireJS路径配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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