【问题标题】:How do I add a boilerplate comment or license to the top of a JavaScript file compiled using the Rails 3.1 Asset Pipeline?如何将样板注释或许可证添加到使用 Rails 3.1 资产管道编译的 JavaScript 文件的顶部?
【发布时间】:2011-12-15 03:06:37
【问题描述】:

我正在使用 Asset Pipeline 构建一些 JavaScript,这些 JavaScript 将移交给许多第三方开发人员。我想在生成的(可能是混淆的)输出文件的顶部放置一条警告注释,但不清楚如何使用 sprockets / coffeescript 组合来实现这一点。

# This is a manifest file that'll be compiled into including all the files listed below.
# Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
# be included in the compiled file accessible from http://example.com/assets/application.js
# It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
# the compiled file.
###
  The following code was compiled from source by MF. 
  Please do not edit this JavaScript directly.  
####
#= require util/extensions
#= require util/date_manipulation
#= require util/format
#= require points_data
#= require graphics/canvas_graphics
#= require graphics/explorer_canvas_graphics
#= require renderer

我得到以下结果:

(function() {
  /*
    The following code was compiled from source by MF. 
    Please do not edit this JavaScript directly.  
  */
}).call(this);

我想要的是这个(或类似的东西):

/*
    The following code was compiled from source by MF. 
    Please do not edit this JavaScript directly.  
  */
(function() {
  // ******** my compiled code from all those required files! *******
}).call(this);

我要如何让它工作?

【问题讨论】:

    标签: ruby-on-rails-3.1 coffeescript asset-pipeline sprockets


    【解决方案1】:

    最简单的方法是在编译后向文件添加注释。

    Rails 的默认压缩器 - Uglifier - 有一个 :copyright 选项用于保留文件的第一批注释行,因此您可以使用它来保留注释(以及所有其他版权)。

    config.assets.js_compressor = Uglifier.new(:copyright => true)

    【讨论】:

    • 我显然不想在编译后执行任何手动步骤 - 那么我应该在哪里添加此评论?
    • 如果你在本地预编译,你需要把它放在 application.rb 中。我会将您的特别评论放在它自己的文件中,并首先将其包含在清单中。在文件中使用 /* 注释语法 */。
    • 版权标志默认为真。其他事情正在发生。
    【解决方案2】:

    JetBrains 的工具集有一个“版权”插件 https://plugins.jetbrains.com/plugin/88-copyright/

    它允许将您的版权/标题注入源文件。 我已经为 IntelliJ Idea 和 RubyMine 尝试过它,它对我有用。

    这个插件可能适用于整个项目、目录或最近更改的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多