grunt requirejs忽略来自我的mainConfigFile的路径 [英] grunt requirejs ignores paths from my mainConfigFile

查看:546
本文介绍了grunt requirejs忽略来自我的mainConfigFile的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目简介



我的项目是单页面店面。该项目有多个模块,每个模块包含一组controller.js,view.js和model.js文件,以及一个template.html文件。并使用requirejs来管理依赖关系。

问题陈述



我想使用mainConfigFile为参考模块提供路径在grunt-requirejs中。
mainConfigFile的部分require.config存储在单独的文件(base.dependency.config)中,并且require.config.paths在运行时通过下划线拼凑在一起。

base.dependency.config

  config = {
baseDependencyConfig:{
paths:{...}
shim:{...}
}
}

main.js

  var dependencies = config.baseDependencyConfig; 
var basePaths = config.baseDependencyConfig.paths;
var extensionPaths = {
//额外的路径集合
};

//在运行时使用下划线合并基本路径和扩展路径
var dependencyPaths = _.extend(basePaths,extensionPaths);

dependencies.paths = dependencyPaths;
require.config(依赖关系);
$ b $ //应用程序启动
require(['app','eventbus']){
// code
}


 'app': 'modules / base / app / base.app.controller'



我的gruntFile:



  module.exports = function(grunt){
grunt.initConfig({
// ...其他插件配置
requirejs:{
options:{
baseUrl:'public',
//命名模块的路径,如'app'被定义
// in main .js在require.config路径下
名称:'main',
include:[$ b $'app',$ b $'cart',
'category'
],
out:'public / build / app-optimized.js',
mainConfigFile:'public / main.js',
findNestedDependencies:true,
optimizeCss:'none',
cssImportIgnore:'style / style.css,style / mocha.css',
}
}
})
}



我的档案结构



  public 
| --modules /
| | --base /
| | | --cart
| | | - 类别
| | | --category.controller.js
| | | --category.view.js
| | | --category.model.js
| | └-category.template.html
| |
| └ - 扩展名/
|
| --style /
| --image /
| --main.js< - 主配置文件
| --other .js文件




  • mainConfigFile,main.js以及其他一些应用程序启动js文件

  • 主要批量应用程序文件位于模块文件夹中

  • 每个模块文件夹都包含其控制器,视图和模型js文件以及模板。 html文件


编辑



gruntFile之前的工作方式与mainConfigFile main.js)setup:

  require.config({
paths:{...}
shim:{...}
})

//应用程序启动
require(['app','eventbus']){
// code


解决方案

r.js使用 Esprima as Javascript parser 从指定的<$ c $中提取配置对象C> mainConfigFile 。它只在代码中寻找特定的签名。



看看


  • < a href =https://github.com/jrburke/r.js/blob/b8a6982d2923ae8389355edaa50d2b7f8065a01a/dist/r.js#L23093 =nofollow> hasRequire() :确定AST节点是配置调用候选人

  • findConfig() :调用上述决定如何提取配置



我已经创建了一个补丁程序,使其能够识别出承认

  requirejs.config(_VariableToExport = {...}); 

这是有限的,JavaScript解析器方法使得r.js能够提取配置变得非常复杂这是由函数(如你的代码)创建的。该补丁尚未提交到项目中。我还在为官僚作风苦苦挣扎。



我认为目前唯一可行的解​​决方法是


  • 不要使用 mainConfigFile

  • 将配置导出为NodeJS模块 Node(Grunt)中的 main.js / config.js

  • 将对象作为值传递给配置属性或方法


    请参阅我对此问题的评论以获取涂鸦

    这种方法已被证实在另一个项目中,我正在处理一个较旧的项目。



    与r.js 2.1.11相关。


    Project Intro

    My project is a single page storefront. The project has multiple modules, and each module contains a set of controller.js, view.js and model.js files, as well as a template.html file. And uses requirejs to manage dependencies.

    Problem Statement

    I want to use mainConfigFile to provide paths to reference modules in grunt-requirejs. Part of my mainConfigFile's require.config is stored in separate file (base.dependency.config), and require.config.paths are pieced together by underscore at runtime.

    base.dependency.config

    config = {
        baseDependencyConfig: {
            paths: { ... }
            shim: { ... }
        }
    }
    

    main.js

    var dependencies = config.baseDependencyConfig;
    var basePaths = config.baseDependencyConfig.paths;
    var extensionPaths = {
        // extra sets of paths
    };
    
    // combine base paths and extension paths at runtime using underscore
    var dependencyPaths = _.extend(basePaths, extensionPaths);
    
    dependencies.paths = dependencyPaths;
    require.config(dependencies);
    
    // application startup
    require(['app', 'eventbus']) {
        // code
    }
    

    Error

    However, grunt requirejs is ignoring mainConfigFile, grunt requirejs tries to find 'app.js' under root, when in fact, 'app' is defined under require.config paths as

    'app': 'modules/base/app/base.app.controller'
    

    my gruntFile:

    module.exports = function (grunt) {
        grunt.initConfig({
            // ... other plugin config
            requirejs: {
                options: {
                    baseUrl: 'public',
                    // the paths for the named modules such as 'app' are defined 
                    // in main.js under require.config paths
                    name: 'main',
                    include: [
                        'app',
                        'cart',
                        'category'
            ],
            out: 'public/build/app-optimized.js',
            mainConfigFile: 'public/main.js',
            findNestedDependencies: true,
            optimizeCss: 'none',
            cssImportIgnore: 'style/style.css, style/mocha.css',
                }
            }
        })
    }
    

    my file structure

    public
        |--modules/
        |       |--base/
        |       |       |--cart
        |       |       |--category
        |       |               |--category.controller.js
        |       |               |--category.view.js
        |       |               |--category.model.js
        |       |               └-category.template.html
        |       |
        |       └--extension/
        |
        |--style/
        |--image/
        |--main.js <-- main config file
        |--other .js files
    

    • mainConfigFile, main.js lives in root, along with a few other application startup js files
    • main bulk of application files lives inside modules folder
    • each module folder contains its controller, view and model js file, as well as a template.html file

    Edit

    the gruntFile worked before, with different mainConfigFile (main.js) setup:

    require.config({
        paths: {...}
        shim: {...}
    })
    
    // application startup
    require(['app', 'eventbus']) {
        // code
    }
    

    解决方案

    r.js uses Esprima as Javascript parser to extract the config object from the specified mainConfigFile. It only looks for certain signatures in the code.

    Look at

    • hasRequire(): determine the AST node is a configuration call candidate
    • findConfig(): calls the above deciding how to extract the config

    I've created a patch making it aware of recognizing

    requirejs.config(_VariableToExport = { ... });
    

    This is limited and the Javascript parser approach makes it very complicated to make r.js able to extract configurations that were created by functions (like in your code) or so. This patch has not been submitted to the project yet. I'm struggling with bureaucracy yet.

    I think the only working workaround so far is

    • not to use mainConfigFile
    • exporting the config as NodeJS module
    • requiring the main.js/config.js in Node (Grunt)
    • passing the object as value to the config attribute or method

    See my comment to this issue for a scribble.

    This approach is proven in another, a bit older project I'm working on.

    Related to r.js 2.1.11.

    这篇关于grunt requirejs忽略来自我的mainConfigFile的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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