如何配置 Rails 3.2+/4 默认生成 .js 而不是 .js.coffee?

2022-01-24 00:00:00 ruby-on-rails javascript coffeescript

我已经看到这个问题的许多变体,其中所有答案都只是建议从 Gemfile 中删除 coffee-rails gem(这个和这个).

I have seen a number of variations of this question where all the answers simply suggest removing the coffee-rails gem from the Gemfile (this one and this one).

这不是我要寻找的答案.我可能仍然想在项目中编写一些 CoffeeScript,我只是希望 JavaScript (.js) 成为 Rails 生成器的默认值.

This is not the answer that I am looking for. I may still want to write some CoffeeScript in the project, I just want JavaScript (.js) to be the default for the Rails generators.

这也意味着 将 .js.coffee 重命名为 .js 每次我生成代码的建议不是一个理想的解决方案.

This also means the suggestions to rename .js.coffee to .js every time I generate code is not a desirable solution.

我可以更改 Rails 中的某种配置以使 .js 成为默认配置,但仍然启用 CoffeeScript(资产管道应该仍然能够转译 .js.coffee 我可以写的文件)?

Is there some sort of configuration in Rails I can change to make .js the default, but still leave CoffeeScript enabled (Asset Pipeline should still be able to transpile an .js.coffee files that I may write)?

推荐答案

你可以通过 自定义生成器配置.

config.generators do |g|
  g.javascript_engine :js
end

# or
config.generators.javascript_engine = :js

您可以在这里查看更多可用的配置选项.

相关文章