使用 gsub 从源代码中删除 javascript 控制台语句
我是 ruby 新手,我编写了一个简单的 rakefile ...
我的任务之一是从 javascript 文件中删除“控制台”行,您对当前代码片段有何看法?
def self.remove_debug()
FileList[File.join(DIST_DIR, '**/console-nodebug.js')].each do |file_name|
puts "file: #{file_name}"
content = File.read(file_name).gsub(/console\..*?;/m, "// console removed")
File.open(file_name, 'wb') { |file| file.write(content) }
end
end
还好??我需要改变一些东西吗?
我测试了代码,一切顺利,但是......我正在寻找好的做法......
谢谢!
Im noob in ruby and i coding a simple rakefile ...
one of my task remove from javascript files the "console" lines, what do you thing about the current snippet?
def self.remove_debug()
FileList[File.join(DIST_DIR, '**/console-nodebug.js')].each do |file_name|
puts "file: #{file_name}"
content = File.read(file_name).gsub(/console\..*?;/m, "// console removed")
File.open(file_name, 'wb') { |file| file.write(content) }
end
end
its fine?? i need to change something?
i test the code and all goes fine, but ... im looking for good practices ...
thks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议有一个调试变量并让 ruby 初始化该变量(我真的对 ruby 不太了解,我把这个留给你,我猜想像将它注入到 html 文件中一样)。然后在你的 js 文件中你可以这样做:
以我的拙见,这比修改文件更好。
希望它有所帮助,祝你好运!
==编辑==
如果它是一个需要缩小的缩小文件,我建议将您的正则表达式替换为“/* console returned */”而不是“// console returned”,以防万一之后还有更多代码线。
除此之外我想你会没事的。有效吗?
I would recommend having a debug variable and have ruby initialize that variable (I really don't know much about ruby, I leave this to you, I guess something like injecting it in the html file). And then in you js file you may do like this:
In my humble opinion this is better than modifying the file.
Hope it helped, good luck!
==EDIT==
If it's a minified file that you need to shrink I would suggest replacing your regex bu "/* console removed */" instead of "// console removed", just in case that there is some more code after that line.
Other than that I think you will be ok. Is it working?