[英]Require config: get `paths` object from JSON in Gruntfile.js and main.js
最近,我遇到了一些令人不快的情况,其中我们的Require配置的paths
对象在requirejs.dist.options.paths
Grunt实例与传递给require.config( paths: { /* etc */ } } )
的paths
属性中的前端引导程序( main.js
)之间不同步require.config( paths: { /* etc */ } } )
。
为了避免麻烦的工作,我想将paths
另存为JSON对象到文件require_config_paths.json
。
我在Gruntfile.js
掌握了JSON,如下所示:
requirejs: {
dist: {
options: {
paths: grunt.file.readJSON( '/assets/js/src/require_config_paths.json' )
}
}
};
在main.js
中main.js
是薄煎饼。 我目前得到以下结果:
// Temporary config to circumvent error when I Require the JSON later on:
// Cannot optimize network URL, skipping: require_config_paths.json?define
require.config( {
paths: {
'require_config_paths.json?define' : 'empty:'
}
} );
// Require the JSON: handle as JSONP via define callback and pass as `paths`
require( [ 'require_config_paths.json?define' ], function configureR( paths ){
require.config( {
paths: paths
} );
} );
但是尝试将其设置为empty:
什么也没做–仍然会被跳过,因为无法优化网络URL –这是一个奇怪的抱怨,因为该资源隐含地是本地资源。
empty:
是您只想放入传递给r.js
的构建配置中的r.js
。 您不要将其放在HTML或HTML加载的文件中。
在您的情况下,这意味着将'require_config_paths.json?define'
的empty:
设置放入'/assets/js/src/require_config_paths.json'
。 我意识到它将在运行时加载,然后将其覆盖为'require_config_paths.json?define'
的设置为empty:
但是到那时,这将无关紧要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.