ruby-on-rails - 从 Rails 资产管道中获取未缩小的 JS
问题描述
如何从终端运行 rails assets 管道以获取未缩小的 javascript?
我能够运行RAILS_ENV=development bundle exec rake assets:precompile
,但这似乎已经生成了捆绑包,而我正在寻找的只是将所有咖啡脚本转译为 javascript,但不缩小也不捆绑。我们只需要从我们的代码库中删除 coffeescript。
我也尝试过 npm 模块 decaffeinate,但这会从 rails 资产管道产生不同的结果,并破坏我们所有的测试。
解决方案
有人指导我看这篇文章:
http ://scottwb.com/blog/2012/06/30/compile-a-single-coffeescript-file-from-your-rails-project/我更新了它给我选项在目录上递归运行,或在单个文件上运行一次。我添加了这个lib/tasks/
,它就像一个魅力。我为 sprockets 风格的指令添加了一个测试,它以 开头#= require
,因为 CoffeeScript 编译器会删除所有注释,这会导致一切中断。相反,我手动将所有跳过的文件转换为 JS 并将指令包含为//= require
,并且有效。
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
推荐阅读
- node.js - Gatsby + Reach 路由器:控制客户端路由路径的最佳模式
- python - Instagram Private API 更改您的简历
- python-3.x - 使用 Tensorflow 模型优化修剪后保存的模型文件大小相同
- node.js - Redis:“auth”命令的参数数量错误
- python - ScannerError:扫描下一个令牌时
- swift - 什么是在 Swift 中构建 CLI 应用程序的好方法或常用方法
- node.js - 类型错误:cloudinaryStorage 不是函数
- hive - 如何在 SparkSQL 上获取诸如 totalNumberFiles、totalFileSize、maxFileSize 等 FileSystemStats?
- jenkins - 如何在 Jenkins PipelineUnit 中使用 libraryResource 模拟`pwsh`
- javascript - 我的 JS 程序显示未定义的素数