2013-04-09 73 views
1

我就severals问题,运行的时候我想在我的项目运行的节点RequireJS。RequireJS优化与CoffeeScript的

这是我的文件夹结构:

-root 
    -/src 
     -App.coffee 

    -/static 
     -/vendor 
      -/plugin 
       -r.js 
       -coffee-script.js 

      -/lib 
       -jquery.js 

      -main.js 

    -build.js 

这是我build.js文件:

({ 
    appDir   : './', 
    baseUrl   : './static/js/', 
    dir    : '../public', 
    optimize  : 'uglify', 
    exclude   : ['coffee-script'], 
    stubModules  : ['cs'], 

    paths: { 

     // Libraries 

     'modernizr'  : 'vendor/modernizr', 
     'jquery'  : ['//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min', 'vendor/jquery'], 
     'jqueryui'  : 'vendor/jquery-ui', 
     'backbone'  : 'vendor/backbone', 
     'underscore' : 'vendor/underscore', 

     // Plugins 

     'plugin'  : 'plugin/plugin', 

     // RequireJS 

     'cs'   : 'plugin/cs', 
     'coffee-script' : 'plugin/coffee-script' 

    }, 
    shim: { 

     'jqueryui' : ['jquery'], 

     'underscore': { 
      exports: '_' 
     }, 

     'backbone': { 
      deps: ['underscore', 'jquery'], 
      exports: 'Backbone' 
     } 
    }, 
    modules: [{ 
     name: "main" 
    }] 
}) 

最后,这是我main.js文件:

require({ 
    baseUrl : '../../src/', 
    paths: { 
    cs: '../../cs', 
    'coffee-script': '../../coffee-script' 
    } 
}, ['cs!App']); 

我总是出现与错误路径设置有关的错误,我无法弄清楚我错在哪里。

谢谢!

回答

0

下面的解决方案适用于我的情况。对于使用垫片导入的非amd模块或手动包装的常见问题(例如this之一,带有自定义路径)。

尝试避免相对路径,并使用绝对路径代替。 从别名模块调用的依赖项将使用其当前位置来查找所需的模块。

require.config(
{ 
    locale: window.GIS.i18n.locale, 
    deps: ['cs!modules/main'], 
    paths: { 
    'i18n'      : 'i18n', 
    'underscore'     : 'libs/underscore', 
    'cs'       : 'libs/cs', // there's no '../something/else/libs/cs' 
    'CoffeeScript'    : 'libs/coffeescript', // ibidem. 
    'text'      : 'libs/text', 
    // ... other amd module aliases go here... 
    }, 

    shim:{ 
    // ... 
    } 

}); 

define(['cs!modules/main'], function(){}); 

当然,这些都不是绝对路径本身,但它们是相对于你的模块树的根。