【问题标题】:RequireJS optimizer with coffeescriptRequireJS 优化器和咖啡脚本
【发布时间】:2013-04-16 05:35:20
【问题描述】:

当我尝试在我的项目上运行节点 RequireJS 时,我遇到了多个问题。

这是我的文件夹结构:

-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']);

我总是遇到与不正确路径设置相关的错误,我不知道我错在哪里。

谢谢!

【问题讨论】:

    标签: javascript coffeescript requirejs


    【解决方案1】:

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

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

    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(){});
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2013-01-09
      • 1970-01-01
      • 2011-11-08
      • 2011-06-30
      • 2013-06-10
      相关资源
      最近更新 更多