每次我尝试在Coffee-Script中分配变量时,r.js都会抛出一个错误

时间:2014-07-16 16:27:29

标签: coffeescript

所以我使用r.js构建了一堆我的文件 - 其中一些是Coffee-Script。我使用Require plugin require-cs来处理这个问题。

以下是我的Require.js配置,la rjs:

rjs.optimize({
    baseUrl: SRC_PATH,
    include: channelMap[channel],
    optimize: 'none',
    stubModules: ['cs', 'tpl', 'less', 'text'],
    exclude: ['normalize', 'coffee-script', 'underscore'],
    CoffeeScript: {
        header: false,
        // since we use AMD, there's no need for IIFE
        bare: true
    },
    separateCSS: true,
    skipModuleInsertion: true,
    // If something needs to be present for tests too and not only for
    // the build step, then add it tools/karma-amd.js instead
    paths: _.extend({
        'less-builder': 'vendor/require-less/less-builder',
        'normalize': 'vendor/require-less/normalize'
    }, rjsPaths),
    wrap: true,
    less: {
        paths: [path.join(BASE_SHOP_FOLDER, 'static', 'zalando', 'css', channel)]
    },
    out: path.join(BUILD_PATH, channel, BUILD_BASE_FILE_NAME + '.js')
}, function () {
    // this needs to be async because less builder uses
    // process.nextTick() to write the file
    process.nextTick(done);
});

即使是最简单的.coffee文件似乎也会暴力失败。 E.g。

define [], ->
    foo = "hello world"
    return foo

引发以下错误:

the variable "foo" can't be assigned with undefined because it has not been declared before
      foo = "hello world"
      ^^^

当我将require-cs的{​​{1}}替换为旧版coffee-script.js时,一切正常。

3 个答案:

答案 0 :(得分:1)

您的代码编译BTW。尝试转到CoffeeScriptDahWebSite并点击TRY COFFEESCRIPT,您会看到它是有效的代码。

define [], () -> code ...开始,我假设您正在使用带有require.js的CoffeeScript插件。我准备打赌你的问题在require.js配置(应该是你的main.js文件或你命名的任何文件),因为你得到的错误看起来很奇怪,就像试图运行无效代码的JavaScript解释器一样你写的(对于JavaScript来说:)。意思是,你的插件根本不存在。

如果你给我你需要的配置,我可以编辑这个答案并帮助你。

干杯!

修改

我看到你编辑了你的问题,但是你提供了错误的文件。您向我展示的是r.js优化器配置,而不是main.js,它指定了cs.jscoffee-script.js文件的加载方式。错误可能出在您的优化器中,但我无法看到您的其他配置。

重申一下,向我展示您的程序的入口点,即HTML中加载的data-main

答案 1 :(得分:0)

我无法重新创建问题:

$ cat ./etc/temp1.coffee
define [], ->
  foo = "hello world"
  return foo

$ coffee --version
CoffeeScript version 1.7.1

$ which coffee
/home/dev/.nvm/v0.10.23/bin/coffee

$ coffee -cp ./etc/temp1.coffee
// Generated by CoffeeScript 1.7.1
(function() {
  define([], function() {
    var foo;
    foo = "hello world";
    return foo;
  });

}).call(this);

$ coffee -cpb ./etc/temp1.coffee
// Generated by CoffeeScript 1.7.1
define([], function() {
  var foo;
  foo = "hello world";
  return foo;
});

答案 2 :(得分:0)

原来问题出在我之前版本的1.7.1上。有人美化它并打破了一切。当我从http://coffeescript.org/extras/coffee-script.js

获取coffee-script.js时,一切都像广告一样有效。