0

How can I run the rails assets pipeline from the terminal to get un-minified javascript out?

I was able to run RAILS_ENV=development bundle exec rake assets:precompile, but this appears to have generated bundles, and what I'm looking for is simply to have all coffeescript transpiled to javascript, but not minified and not bundled. We simply need to remove coffeescript from our codebase.

I've also tried the npm module decaffeinate, but this generates different results from the rails assets pipeline, and breaks all of our tests.

rm.rf.etc
  • 884
  • 2
  • 8
  • 20
  • Have your tried commenting out `config.assets.js_compressor`? This is possibly a duplicate of https://stackoverflow.com/questions/9674714/disable-asset-minification-in-rails-production – max Feb 14 '20 at 18:27
  • I think I need a different config attribute to also disable bundling. – rm.rf.etc Feb 14 '20 at 19:05
  • That would be `config.assets.debug = true` which disables concatenation in development but I don't think that does anything when precompiling. You might have to take the task apart and look at how its implemented. – max Feb 14 '20 at 19:09
  • Is that located internally within rails? – rm.rf.etc Feb 14 '20 at 19:16
  • https://github.com/rails/sprockets/blob/master/lib/rake/sprocketstask.rb – max Feb 14 '20 at 19:17

1 Answers1

0

Someone directed me to this article: http://scottwb.com/blog/2012/06/30/compile-a-single-coffeescript-file-from-your-rails-project/ and I updated it to give me the option to run recursively on a directory, or once on a single file. I added this to lib/tasks/ and it worked like a charm. I added a test for sprockets-style directives, which start with #= require, because the CoffeeScript compiler strips out all comments, which causes everything to break. Instead, I converted all skipped files to JS manually and included the directives as //= require, and that worked.

namespace :coffee do

  def do_one(filepath)
    File.write(filepath.chomp(".coffee"), CoffeeScript.compile(File.open(filepath)))
    File.rename(filepath, filepath.chomp(".js.coffee") + ".backup")
  end

  def cs_task(path)
    Dir.glob("#{path.chomp("/")}/*.js.coffee").each do |filename|
      file = File.open(filename)

      if (file.read[/\= require/])
        puts "skip #{filename}"
      else
        puts "process #{filename}"
        do_one(filename)
      end
    end
    Dir.glob("#{path.chomp("/")}/*/").each do |child_path|
      cs_task(child_path)
    end
  end

  task :cancel, :path do |t, args|
    cs_task(args.path)
  end

  task :show, :path do |t, args|
    puts CoffeeScript.compile(File.open(args.path))
  end

  task :one_off, :path do |t, args|
    do_one(args.path)
  end
end
rm.rf.etc
  • 884
  • 2
  • 8
  • 20