Skip to content

Instantly share code, notes, and snippets.

@mbildner
Last active March 7, 2016 21:14
Show Gist options
  • Save mbildner/53754486a31f426477ec to your computer and use it in GitHub Desktop.
Save mbildner/53754486a31f426477ec to your computer and use it in GitHub Desktop.
require 'pry'
all_output = `ag -G ".jsx" "//= require"`
calls = all_output.lines.map do |line|
path = line.split(':').last
.gsub('//= ', '')
.gsub('require_tree ', '')
.gsub('require ', '')
.gsub(/\r/, '')
.gsub(/\n/, '')
file = line.gsub(/:.*$/, '')
.gsub(/\r/, '')
.gsub(/\n/, '')
[file, "require('#{path}')"]
end
calls.each{|c| puts c}
iife_wrapped = calls.map do |path, require_path|
collector = []
last_was_require = false
was_already_wrapped = false
file_text = File.read(path)
file_text.lines.each do |line|
if line.start_with? "//= require"
collector << line
last_was_require = true
else
if last_was_require
if line == "\n" || line == "\r"
# lol noop
elsif !line.start_with? "(function()"
collector << "(function(){"
binding.pry
else
was_already_wrapped = true
end
last_was_require = false
end
collector << " " + line
end
end
if !was_already_wrapped
collector << "})();"
end
collector.join("")
end
iife_wrapped.each{|f|puts f}
@mbildner
Copy link
Author

mbildner commented Mar 7, 2016

watch out for calls to global namespaces that have to be required

@mbildner
Copy link
Author

mbildner commented Mar 7, 2016

my eyes are literally bleeding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment