├── .ruby-gemset ├── .ruby-version ├── test ├── dummy │ ├── log │ │ └── .keep │ ├── app │ │ ├── mailers │ │ │ └── .keep │ │ ├── models │ │ │ ├── .keep │ │ │ └── concerns │ │ │ │ └── .keep │ │ ├── assets │ │ │ ├── images │ │ │ │ └── .keep │ │ │ ├── components │ │ │ │ ├── simple_js │ │ │ │ │ ├── test.js │ │ │ │ │ └── index.html │ │ │ │ ├── simple │ │ │ │ │ └── index.html │ │ │ │ ├── template │ │ │ │ │ ├── test.css │ │ │ │ │ └── index.html │ │ │ │ ├── compile_coffee │ │ │ │ │ ├── test.coffee │ │ │ │ │ └── index.html │ │ │ │ ├── simple_css │ │ │ │ │ ├── test.css │ │ │ │ │ └── index.html │ │ │ │ ├── compile_scss │ │ │ │ │ ├── index.html │ │ │ │ │ └── test.scss │ │ │ │ ├── external_js │ │ │ │ │ └── index.html │ │ │ │ ├── external_css │ │ │ │ │ └── index.html │ │ │ │ └── application.html │ │ │ ├── stylesheets │ │ │ │ └── application.css │ │ │ └── javascripts │ │ │ │ └── application.js │ │ ├── controllers │ │ │ ├── concerns │ │ │ │ └── .keep │ │ │ ├── application_controller.rb │ │ │ └── dummy_controller.rb │ │ ├── views │ │ │ ├── dummy │ │ │ │ └── index.html.erb │ │ │ └── layouts │ │ │ │ └── application.html.erb │ │ └── helpers │ │ │ └── application_helper.rb │ ├── lib │ │ └── assets │ │ │ └── .keep │ ├── public │ │ ├── favicon.ico │ │ ├── 500.html │ │ ├── 422.html │ │ └── 404.html │ ├── vendor │ │ └── assets │ │ │ └── components │ │ │ └── .keep │ ├── .bowerrc │ ├── bin │ │ ├── rake │ │ ├── bundle │ │ └── rails │ ├── config │ │ ├── routes.rb │ │ ├── initializers │ │ │ ├── session_store.rb │ │ │ ├── filter_parameter_logging.rb │ │ │ ├── mime_types.rb │ │ │ ├── backtrace_silencers.rb │ │ │ ├── wrap_parameters.rb │ │ │ ├── secret_token.rb │ │ │ └── inflections.rb │ │ ├── environment.rb │ │ ├── boot.rb │ │ ├── database.yml │ │ ├── locales │ │ │ └── en.yml │ │ ├── application.rb │ │ └── environments │ │ │ ├── development.rb │ │ │ ├── test.rb │ │ │ └── production.rb │ ├── config.ru │ ├── Rakefile │ └── README.rdoc ├── helpers_test.rb ├── test_helper.rb ├── node_test.rb ├── dummy_app_integration_test.rb ├── resolver_test.rb ├── document_test.rb ├── compressors_test.rb └── processors_test.rb ├── .rbenv-gemsets ├── lib ├── emcee │ ├── version.rb │ ├── helpers │ │ ├── asset_url_helper.rb │ │ ├── asset_tag_helper.rb │ │ └── sprockets_helper.rb │ ├── compressors │ │ └── html_compressor.rb │ ├── loose_assets.rb │ ├── node.rb │ ├── processors │ │ ├── import_processor.rb │ │ ├── stylesheet_processor.rb │ │ └── script_processor.rb │ ├── directive_processor.rb │ ├── resolver.rb │ ├── railtie.rb │ └── document.rb ├── generators │ └── emcee │ │ └── install │ │ ├── templates │ │ ├── template.bowerrc │ │ └── application.html │ │ └── install_generator.rb └── emcee.rb ├── .gitignore ├── Gemfile ├── Rakefile ├── emcee.gemspec ├── MIT-LICENSE └── README.md /.ruby-gemset: -------------------------------------------------------------------------------- 1 | emcee 2 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.1.5 2 | -------------------------------------------------------------------------------- /test/dummy/log/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.rbenv-gemsets: -------------------------------------------------------------------------------- 1 | .gems 2 | -------------------------------------------------------------------------------- /test/dummy/app/mailers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/app/models/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/lib/assets/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/app/assets/images/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/app/models/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/vendor/assets/components/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/app/views/dummy/index.html.erb: -------------------------------------------------------------------------------- 1 |

Dummy

2 | -------------------------------------------------------------------------------- /lib/emcee/version.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | VERSION = "1.0.10" 3 | end 4 | -------------------------------------------------------------------------------- /test/dummy/.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "vendor/assets/components" 3 | } 4 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/simple_js/test.js: -------------------------------------------------------------------------------- 1 | var hello = "world"; 2 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/simple/index.html: -------------------------------------------------------------------------------- 1 |

Test simple import

2 | -------------------------------------------------------------------------------- /test/dummy/app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/template/test.css: -------------------------------------------------------------------------------- 1 | :host { 2 | color: pink; 3 | } 4 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/compile_coffee/test.coffee: -------------------------------------------------------------------------------- 1 | # Hello world 2 | hello = "world" 3 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/simple_js/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Test js

3 | -------------------------------------------------------------------------------- /lib/generators/emcee/install/templates/template.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "vendor/assets/components" 3 | } 4 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/simple_css/test.css: -------------------------------------------------------------------------------- 1 | p { 2 | /* Make it red. */ 3 | color: red; 4 | } 5 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/simple_css/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Test css

3 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/compile_scss/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Test scss

3 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/compile_coffee/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Test CoffeeScript

3 | -------------------------------------------------------------------------------- /test/dummy/bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative '../config/boot' 3 | require 'rake' 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/compile_scss/test.scss: -------------------------------------------------------------------------------- 1 | $color: red; 2 | 3 | p { 4 | // Make it $color. 5 | color: $color; 6 | } 7 | -------------------------------------------------------------------------------- /test/dummy/config/routes.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.routes.draw do 2 | root "dummy#index" 3 | get "/assets/application", to: "dummy#assets" 4 | end 5 | -------------------------------------------------------------------------------- /test/dummy/bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/external_js/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Test external js

3 | -------------------------------------------------------------------------------- /test/dummy/bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path('../../config/application', __FILE__) 3 | require_relative '../config/boot' 4 | require 'rails/commands' 5 | -------------------------------------------------------------------------------- /test/dummy/config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run Rails.application 5 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Dummy::Application.config.session_store :cookie_store, key: '_dummy_session' 4 | -------------------------------------------------------------------------------- /test/dummy/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require File.expand_path('../application', __FILE__) 3 | 4 | # Initialize the Rails application. 5 | Dummy::Application.initialize! 6 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/external_css/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Test external css

3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | test/dummy/db/*.sqlite3 5 | test/dummy/db/*.sqlite3-journal 6 | test/dummy/log/*.log 7 | test/dummy/tmp/ 8 | test/dummy/.sass-cache 9 | 10 | *.gem 11 | .gems 12 | 13 | Gemfile.lock 14 | -------------------------------------------------------------------------------- /lib/emcee.rb: -------------------------------------------------------------------------------- 1 | require "emcee/version" 2 | 3 | require "emcee/helpers/asset_url_helper" 4 | require "emcee/helpers/asset_tag_helper" 5 | require "emcee/helpers/sprockets_helper" 6 | 7 | require "emcee/loose_assets" 8 | require "emcee/railtie" 9 | -------------------------------------------------------------------------------- /test/helpers_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class Helpers < ActionView::TestCase 4 | test "html_import should work" do 5 | assert_equal "", html_import_tag("test") 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure sensitive parameters which will be filtered from the log file. 4 | Rails.application.config.filter_parameters += [:password] 5 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | # Mime::Type.register_alias "text/html", :iphone 6 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Prevent CSRF attacks by raising an exception. 3 | # For APIs, you may want to use :null_session instead. 4 | protect_from_forgery with: :exception 5 | end 6 | -------------------------------------------------------------------------------- /test/dummy/config/boot.rb: -------------------------------------------------------------------------------- 1 | # Set up gems listed in the Gemfile. 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) 3 | 4 | require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) 5 | $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) 6 | -------------------------------------------------------------------------------- /test/dummy/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | 6 | Dummy::Application.load_tasks 7 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/template/index.html: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/dummy_controller.rb: -------------------------------------------------------------------------------- 1 | class DummyController < ApplicationController 2 | def index 3 | end 4 | 5 | def assets 6 | # Send the compiled application.html as a string so that we can test asset 7 | # compilation. 8 | render json: Dummy::Application::assets.find_asset("application.html").source 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /test/dummy/app/assets/components/application.html: -------------------------------------------------------------------------------- 1 | 10 | -------------------------------------------------------------------------------- /lib/generators/emcee/install/templates/application.html: -------------------------------------------------------------------------------- 1 | 10 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /test/dummy/app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Dummy 5 | <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> 6 | <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 7 | <%= html_import_tag "application", "data-turbolinks-track" => true %> 8 | <%= csrf_meta_tags %> 9 | 10 | 11 | 12 | <%= yield %> 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # Configure Rails Environment 2 | ENV["RAILS_ENV"] = "test" 3 | 4 | require File.expand_path("../dummy/config/environment.rb", __FILE__) 5 | require "rails/test_help" 6 | 7 | Rails.backtrace_cleaner.remove_silencers! 8 | 9 | # Load support files 10 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } 11 | 12 | # Load fixtures from the engine 13 | if ActiveSupport::TestCase.method_defined?(:fixture_path=) 14 | ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) 15 | end 16 | -------------------------------------------------------------------------------- /lib/emcee/helpers/asset_url_helper.rb: -------------------------------------------------------------------------------- 1 | module ActionView 2 | module Helpers 3 | module AssetUrlHelper 4 | # Modify ActionView to recognize html files and the '/components' path. 5 | ASSET_EXTENSIONS.merge!({ html: '.html' }) 6 | ASSET_PUBLIC_DIRECTORIES.merge!({ html: '/components' }) 7 | 8 | # Convenience method for html. Based on ActionView's standard 9 | # javascript_path method. 10 | def path_to_html(source, options = {}) 11 | path_to_asset(source, { type: :html }.merge!(options)) 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Declare your gem's dependencies in emcee.gemspec. 4 | # Bundler will treat runtime dependencies like base dependencies, and 5 | # development dependencies will be added by default to the :development group. 6 | gemspec 7 | 8 | # Declare any dependencies that are still in development here instead of in 9 | # your gemspec. These might include edge Rails or gems from your path or 10 | # Git. Remember to move these dependencies to your gemspec before releasing 11 | # your gem to rubygems.org. 12 | 13 | # To use debugger 14 | # gem 'debugger' 15 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] if respond_to?(:wrap_parameters) 9 | end 10 | 11 | # To enable root element in JSON for ActiveRecord objects. 12 | # ActiveSupport.on_load(:active_record) do 13 | # self.include_root_in_json = true 14 | # end 15 | -------------------------------------------------------------------------------- /test/dummy/README.rdoc: -------------------------------------------------------------------------------- 1 | == README 2 | 3 | This README would normally document whatever steps are necessary to get the 4 | application up and running. 5 | 6 | Things you may want to cover: 7 | 8 | * Ruby version 9 | 10 | * System dependencies 11 | 12 | * Configuration 13 | 14 | * Database creation 15 | 16 | * Database initialization 17 | 18 | * How to run the test suite 19 | 20 | * Services (job queues, cache servers, search engines, etc.) 21 | 22 | * Deployment instructions 23 | 24 | * ... 25 | 26 | 27 | Please feel free to use a different markup language if you do not plan to run 28 | rake doc:app. 29 | -------------------------------------------------------------------------------- /test/dummy/app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a manifest file that'll be compiled into application.css, which will include all the files 3 | * listed below. 4 | * 5 | * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, 6 | * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. 7 | * 8 | * You're free to add application-wide styles to this file and they'll appear at the top of the 9 | * compiled file, but it's generally better to create a new file per style scope. 10 | * 11 | *= require_self 12 | *= require_tree . 13 | */ 14 | -------------------------------------------------------------------------------- /lib/emcee/compressors/html_compressor.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | module Compressors 3 | # HtmlCompressor is a very basic compressor that removes blank lines and 4 | # comments from an HTML file. 5 | class HtmlCompressor 6 | HTML_COMMENTS = /\)/m 7 | JS_MULTI_COMMENTS = /\/\*(?:.*?)\*\//m 8 | JS_COMMENTS = /^\s*\/\/.*$/ 9 | BLANK_LINES = /^[\s]*$\n/ 10 | 11 | def compress(string) 12 | ops = [HTML_COMMENTS, JS_MULTI_COMMENTS, JS_COMMENTS, BLANK_LINES] 13 | 14 | ops.reduce(string) do |output, op| 15 | output.gsub(op, "") 16 | end 17 | end 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /test/dummy/app/assets/javascripts/application.js: -------------------------------------------------------------------------------- 1 | // This is a manifest file that'll be compiled into application.js, which will include all the files 2 | // listed below. 3 | // 4 | // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 5 | // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. 6 | // 7 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 8 | // compiled file. 9 | // 10 | // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details 11 | // about supported directives. 12 | // 13 | //= require_tree . 14 | -------------------------------------------------------------------------------- /test/dummy/config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem 'sqlite3' 6 | development: 7 | adapter: sqlite3 8 | database: db/development.sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | # Warning: The database defined as "test" will be erased and 13 | # re-generated from your development database when you run "rake". 14 | # Do not set this db to the same as development or production. 15 | test: 16 | adapter: sqlite3 17 | database: db/test.sqlite3 18 | pool: 5 19 | timeout: 5000 20 | 21 | production: 22 | adapter: sqlite3 23 | database: db/production.sqlite3 24 | pool: 5 25 | timeout: 5000 26 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | begin 2 | require 'bundler/setup' 3 | rescue LoadError 4 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 5 | end 6 | 7 | require 'rdoc/task' 8 | 9 | RDoc::Task.new(:rdoc) do |rdoc| 10 | rdoc.rdoc_dir = 'rdoc' 11 | rdoc.title = 'Emcee' 12 | rdoc.options << '--line-numbers' 13 | rdoc.rdoc_files.include('README.rdoc') 14 | rdoc.rdoc_files.include('lib/**/*.rb') 15 | end 16 | 17 | 18 | 19 | 20 | Bundler::GemHelper.install_tasks 21 | 22 | require 'rake/testtask' 23 | 24 | Rake::TestTask.new(:test) do |t| 25 | t.libs << 'lib' 26 | t.libs << 'test' 27 | t.pattern = 'test/**/*_test.rb' 28 | t.verbose = false 29 | end 30 | 31 | 32 | task default: :test 33 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/secret_token.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key is used for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | 6 | # Make sure the secret is at least 30 characters and all random, 7 | # no regular words or you'll be exposed to dictionary attacks. 8 | # You can use `rake secret` to generate a secure secret key. 9 | 10 | # Make sure your secret_key_base is kept private 11 | # if you're sharing your code publicly. 12 | Dummy::Application.config.secret_key_base = '333a83947133eab0bf2da71086e66a4c25a85739575f595c2fb42faf041c73bf5b576747f926f5de828ad1953f0459cc433b4bdef30426badbf3a5b124f45418' 13 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, '\1en' 8 | # inflect.singular /^(ox)en/i, '\1' 9 | # inflect.irregular 'person', 'people' 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym 'RESTful' 16 | # end 17 | -------------------------------------------------------------------------------- /test/dummy/config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t 'hello' 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t('hello') %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # To learn more, please read the Rails Internationalization guide 20 | # available at http://guides.rubyonrails.org/i18n.html. 21 | 22 | en: 23 | hello: "Hello world" 24 | -------------------------------------------------------------------------------- /lib/emcee/helpers/asset_tag_helper.rb: -------------------------------------------------------------------------------- 1 | module ActionView 2 | module Helpers 3 | module AssetTagHelper 4 | # Custom view helper used to create an html import. Will search the asset 5 | # directories for the sources. 6 | # 7 | # html_import_tag("navigation") 8 | # 9 | def html_import_tag(*sources) 10 | options = sources.extract_options!.stringify_keys 11 | path_options = options.extract!('protocol').symbolize_keys 12 | 13 | sources.uniq.map { |source| 14 | tag_options = { 15 | "rel" => "import", 16 | "href" => path_to_html(source, path_options) 17 | }.merge!(options) 18 | tag(:link, tag_options) 19 | }.join("\n").html_safe 20 | end 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/emcee/loose_assets.rb: -------------------------------------------------------------------------------- 1 | # Monkey patch Sprockets-rails so that loose html files are handled correctly. 2 | # 3 | # Not sure that modifying another gem's railtie is a good idea, but it works 4 | # for now. 5 | module Sprockets 6 | class Railtie 7 | # Remove the LOOSE_APP_ASSETS constant so we can modify it without Ruby 8 | # complaining. 9 | remove_const(:LOOSE_APP_ASSETS) if defined?(LOOSE_APP_ASSETS) 10 | 11 | # Add .html to the loose app assets file extensions. 12 | LOOSE_APP_ASSETS = lambda do |filename, path| 13 | path =~ /app\/assets/ && !%w(.js .css .html).include?(File.extname(filename)) 14 | end 15 | 16 | # Add the .html extension to the 'precompile' regex. 17 | config.assets.precompile = [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js|html)$/] 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/emcee/node.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | # Document is responsible for interacting with individual html nodes that 3 | # make up the parsed document. 4 | class Node 5 | def initialize(parser_node) 6 | @parser_node = parser_node 7 | end 8 | 9 | def path 10 | href || src 11 | end 12 | 13 | def remove 14 | @parser_node.remove 15 | end 16 | 17 | def replace(type, new_content) 18 | new_node = Nokogiri::XML::Node.new(type, document) 19 | new_node.content = new_content 20 | @parser_node.replace(new_node) 21 | end 22 | 23 | private 24 | 25 | def href 26 | @parser_node.attribute("href") 27 | end 28 | 29 | def src 30 | @parser_node.attribute("src") 31 | end 32 | 33 | def document 34 | @parser_node.document 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /lib/emcee/processors/import_processor.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | module Processors 3 | # ImportProcessor scans a file for html imports and adds them to the current 4 | # required assets. 5 | class ImportProcessor 6 | def initialize(resolver) 7 | @resolver = resolver 8 | end 9 | 10 | def process(doc) 11 | require_assets(doc) 12 | remove_imports(doc) 13 | doc 14 | end 15 | 16 | private 17 | 18 | def require_assets(doc) 19 | doc.html_imports.each do |node| 20 | path = @resolver.absolute_path(node.path) 21 | @resolver.require_asset(path) 22 | end 23 | end 24 | 25 | def remove_imports(doc) 26 | doc.html_imports.each do |node| 27 | node.remove 28 | end 29 | end 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/emcee/processors/stylesheet_processor.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | module Processors 3 | # StylesheetProcessor scans a document for external stylesheet references and 4 | # inlines them into the current document. 5 | class StylesheetProcessor 6 | def initialize(resolver) 7 | @resolver = resolver 8 | end 9 | 10 | def process(doc) 11 | inline_styles(doc) 12 | doc 13 | end 14 | 15 | private 16 | 17 | def inline_styles(doc) 18 | doc.style_references.each do |node| 19 | path = @resolver.absolute_path(node.path) 20 | return unless @resolver.should_inline?(path) 21 | content = @resolver.evaluate(path) 22 | node.replace("style", content) 23 | @resolver.depend_on_asset(path) 24 | end 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /emcee.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | 3 | require "emcee/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "emcee" 7 | s.version = Emcee::VERSION 8 | s.authors = ["Andrew Huth"] 9 | s.email = ["andrew@huth.me"] 10 | s.homepage = "https://github.com/ahuth/emcee" 11 | s.summary = "Add web components to the Rails asset pipeline." 12 | s.description = "Add web components to the Rails asset pipeline" 13 | s.license = "MIT" 14 | 15 | s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] 16 | s.test_files = Dir["test/**/*"] 17 | 18 | s.add_dependency "nokogiri", "~> 1.0" 19 | s.add_dependency "nokogumbo", "~> 1.1" 20 | s.add_dependency "rails", "~> 4.2.0" 21 | 22 | s.add_development_dependency "coffee-rails", "~> 4.0" 23 | s.add_development_dependency "sass", "~> 3.0" 24 | s.add_development_dependency "sqlite3", "~> 1.3" 25 | end 26 | -------------------------------------------------------------------------------- /lib/emcee/helpers/sprockets_helper.rb: -------------------------------------------------------------------------------- 1 | module Sprockets 2 | module Rails 3 | module Helper 4 | # Custom view helper used to create an html import. This same method is 5 | # already defined in ActionView. We pull out the sources here, before 6 | # calling back to ActionView's. 7 | # 8 | # Based on Sprocket's javascript_include_tag. 9 | def html_import_tag(*sources) 10 | options = sources.extract_options!.stringify_keys 11 | if options["debug"] != false && request_debug_assets? 12 | sources.map { |source| 13 | #check_errors_for(source) 14 | if asset = lookup_asset_for_path(source, type: :html) 15 | asset.to_a.map do |a| 16 | super(path_to_html(a.logical_path, debug: true), options) 17 | end 18 | else 19 | super(source, options) 20 | end 21 | }.flatten.uniq.join("\n").html_safe 22 | else 23 | sources.push(options) 24 | super(*sources) 25 | end 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/emcee/processors/script_processor.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | module Processors 3 | # ScriptProcessor scans a document for external script references and inlines 4 | # them into the current document. 5 | class ScriptProcessor 6 | def initialize(resolver) 7 | @resolver = resolver 8 | end 9 | 10 | def process(doc) 11 | inline_scripts(doc) 12 | doc 13 | end 14 | 15 | private 16 | 17 | def inline_scripts(doc) 18 | doc.script_references.each do |node| 19 | path = @resolver.absolute_path(node.path) 20 | return unless @resolver.should_inline?(path) 21 | script = @resolver.evaluate(path) 22 | node.replace("script", escape_with_slash(script)) 23 | @resolver.depend_on_asset(path) 24 | end 25 | end 26 | 27 | def escape_with_slash(script) 28 | script = script.sub('))+/ 8 | 9 | # Implement `render` so that it uses our own header pattern. 10 | def render(context, locals) 11 | @context = context 12 | @pathname = context.pathname 13 | @directory = File.dirname(@pathname) 14 | 15 | @header = data[HEADER_PATTERN, 0] || "" 16 | @body = $' || data 17 | # Ensure body ends in a new line 18 | @body += "\n" if @body != "" && @body !~ /\n\Z/m 19 | 20 | @included_pathnames = [] 21 | 22 | @result = "" 23 | @result.force_encoding(body.encoding) 24 | 25 | @has_written_body = false 26 | 27 | process_directives 28 | process_source 29 | 30 | @result 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 Andrew Huth 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/dummy/config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require "active_model/railtie" 4 | require "action_controller/railtie" 5 | require "action_mailer/railtie" 6 | require "action_view/railtie" 7 | require "sprockets/railtie" 8 | 9 | Bundler.require(*Rails.groups) 10 | require "emcee" 11 | 12 | module Dummy 13 | class Application < Rails::Application 14 | # Settings in config/environments/* take precedence over those specified here. 15 | # Application configuration should go into files in config/initializers 16 | # -- all .rb files in that directory are automatically loaded. 17 | 18 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 19 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 20 | # config.time_zone = 'Central Time (US & Canada)' 21 | 22 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 23 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 24 | # config.i18n.default_locale = :de 25 | end 26 | end 27 | 28 | -------------------------------------------------------------------------------- /lib/emcee/resolver.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | # Resolver is responsible for interfacing with Sprockets. 3 | class Resolver 4 | def initialize(context) 5 | @context = context 6 | @directory = File.dirname(context.pathname) 7 | end 8 | 9 | # Declare a file as a dependency to Sprockets. The dependency will be 10 | # included in the application's html bundle. 11 | def require_asset(path) 12 | @context.require_asset(path) 13 | end 14 | 15 | # Allows to state an asset dependency without including it 16 | def depend_on_asset(path) 17 | @context.depend_on_asset(path) 18 | end 19 | 20 | # Return the contents of a file. Does any required processing, such as SCSS 21 | # or CoffeeScript. 22 | def evaluate(path) 23 | @context.evaluate(path) 24 | end 25 | 26 | # Indicate if an asset should be inlined or not. References to files at an 27 | # external web address, for example, should not be inlined. 28 | def should_inline?(path) 29 | path !~ /\A\/\// 30 | end 31 | 32 | def absolute_path(path) 33 | File.absolute_path(path, @directory) 34 | end 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /test/node_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'emcee/node.rb' 3 | 4 | class NodeTest < ActiveSupport::TestCase 5 | setup do 6 | @body = "" 7 | @document = Nokogiri::HTML.fragment(@body) 8 | @node = Emcee::Node.new(@document.children.first) 9 | end 10 | 11 | test "should have a stylesheet path" do 12 | assert_equal "test.css", @node.path.to_s 13 | end 14 | 15 | test "should have a script path" do 16 | document = Nokogiri::HTML.fragment("") 17 | node = Emcee::Node.new(document.children.first) 18 | assert_equal "test.js", node.path.to_s 19 | end 20 | 21 | test "should remove itself" do 22 | assert_difference "@document.children.length", -1 do 23 | @node.remove 24 | end 25 | end 26 | 27 | test "can be replaced by a ", @document.to_s 31 | end 32 | 33 | test "can be replaced by a ", @document.to_s 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /test/dummy/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the web server when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Do not eager load code on boot. 10 | config.eager_load = false 11 | 12 | # Show full error reports and disable caching. 13 | config.consider_all_requests_local = true 14 | config.action_controller.perform_caching = false 15 | 16 | # Don't care if the mailer can't send. 17 | config.action_mailer.raise_delivery_errors = false 18 | 19 | # Print deprecation notices to the Rails logger. 20 | config.active_support.deprecation = :log 21 | 22 | # Raise an error on page load if there are pending migrations 23 | config.active_record.migration_error = :page_load 24 | 25 | # Debug mode disables concatenation and preprocessing of assets. 26 | # This option may cause significant delays in view rendering with a large 27 | # number of complex assets. 28 | config.assets.debug = true 29 | end 30 | -------------------------------------------------------------------------------- /lib/emcee/railtie.rb: -------------------------------------------------------------------------------- 1 | require "emcee/directive_processor" 2 | require "emcee/processors/import_processor" 3 | require "emcee/processors/script_processor" 4 | require "emcee/processors/stylesheet_processor" 5 | require "emcee/compressors/html_compressor" 6 | require "emcee/document" 7 | require "emcee/resolver" 8 | 9 | module Emcee 10 | class Railtie < Rails::Railtie 11 | initializer :add_asset_paths do |app| 12 | app.assets.paths.unshift(Rails.root.join("vendor", "assets", "components")) 13 | end 14 | 15 | initializer :add_preprocessors do |app| 16 | app.assets.register_mime_type "text/html", ".html" 17 | app.assets.register_preprocessor "text/html", Emcee::DirectiveProcessor 18 | end 19 | 20 | initializer :add_postprocessors do |app| 21 | app.assets.register_postprocessor "text/html", :web_components do |context, data| 22 | doc = Emcee::Document.new(data) 23 | resolver = Emcee::Resolver.new(context) 24 | Emcee::Processors::ImportProcessor.new(resolver).process(doc) 25 | Emcee::Processors::ScriptProcessor.new(resolver).process(doc) 26 | Emcee::Processors::StylesheetProcessor.new(resolver).process(doc) 27 | doc.to_s 28 | end 29 | end 30 | 31 | initializer :add_compressors do |app| 32 | app.assets.register_bundle_processor "text/html", :html_compressor do |context, data| 33 | Emcee::Compressors::HtmlCompressor.new.compress(data) 34 | end 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /test/dummy/public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 48 | 49 | 50 | 51 | 52 |
53 |

We're sorry, but something went wrong.

54 |
55 |

If you are the application owner check the logs for more information.

56 | 57 | 58 | -------------------------------------------------------------------------------- /test/dummy/public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 48 | 49 | 50 | 51 | 52 |
53 |

The change you wanted was rejected.

54 |

Maybe you tried to change something you didn't have access to.

55 |
56 |

If you are the application owner check the logs for more information.

57 | 58 | 59 | -------------------------------------------------------------------------------- /test/dummy/public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 48 | 49 | 50 | 51 | 52 |
53 |

The page you were looking for doesn't exist.

54 |

You may have mistyped the address or the page may have moved.

55 |
56 |

If you are the application owner check the logs for more information.

57 | 58 | 59 | -------------------------------------------------------------------------------- /test/dummy_app_integration_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'action_controller' 3 | require 'coffee-rails' 4 | require 'sass' 5 | 6 | class DummyAppIntegrationTest < ActionController::TestCase 7 | tests DummyController 8 | 9 | # The dummy app has a custom route and controller action that renders the 10 | # compiled html import as a json response. We test against that here. 11 | test "the test files should get compiled and concatenated" do 12 | get :assets 13 | assert_equal @response.body, <<-EOS.strip_heredoc 14 | 19 |

Test CoffeeScript

20 | 23 |

Test scss

24 | 25 |

Test external css

26 | 27 |

Test external js

28 |

Test simple import

29 | 33 |

Test css

34 | 36 |

Test js

37 | 38 | 46 | 47 | EOS 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /test/resolver_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'emcee/resolver.rb' 3 | 4 | # Create a stub of Sprocket's Context class, so we can test if we're sending 5 | # the correct messages to it. 6 | class ContextStub 7 | attr_reader :asset_required, :asset_dependent, :evaluated 8 | 9 | def pathname 10 | "/" 11 | end 12 | 13 | def require_asset(asset) 14 | @asset_required = true 15 | end 16 | 17 | def depend_on_asset(asset) 18 | @asset_dependent = true 19 | end 20 | 21 | def evaluate(path, options = {}) 22 | @evaluated = true 23 | end 24 | end 25 | 26 | class ResolverTest < ActiveSupport::TestCase 27 | setup do 28 | @context = ContextStub.new 29 | @resolver = Emcee::Resolver.new(@context) 30 | end 31 | 32 | test "should resolve absolute path" do 33 | assert_equal "/test.js", @resolver.absolute_path("test.js") 34 | end 35 | 36 | test "should require assets" do 37 | @resolver.require_asset("/asset1") 38 | assert @context.asset_required 39 | end 40 | 41 | test "should set dependencies on assets" do 42 | @resolver.depend_on_asset("/dependency") 43 | assert @context.asset_dependent 44 | end 45 | 46 | test "should evaluate an asset" do 47 | @resolver.evaluate("/test") 48 | assert @context.evaluated 49 | end 50 | 51 | test "should indicate if asset should be inlined" do 52 | assert @resolver.should_inline?("test.css") 53 | assert @resolver.should_inline?("/vendor/assets/test.js") 54 | 55 | assert_not @resolver.should_inline?("//fonts.googleapis.com/css?family=RobotoDraft:regular,bold,italic,thin,light,bolditalic,black,medium&lang=en") 56 | assert_not @resolver.should_inline?("//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js") 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /test/dummy/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Do not eager load code on boot. This avoids loading your whole application 11 | # just for the purpose of running a single test. If you are using a tool that 12 | # preloads Rails for running tests, you may have to set it to true. 13 | config.eager_load = false 14 | 15 | # Configure static asset server for tests with Cache-Control for performance. 16 | config.serve_static_files = true 17 | config.static_cache_control = "public, max-age=3600" 18 | 19 | # Show full error reports and disable caching. 20 | config.consider_all_requests_local = true 21 | config.action_controller.perform_caching = false 22 | 23 | # Raise exceptions instead of rendering exception templates. 24 | config.action_dispatch.show_exceptions = false 25 | 26 | # Disable request forgery protection in test environment. 27 | config.action_controller.allow_forgery_protection = false 28 | 29 | # Tell Action Mailer not to deliver emails to the real world. 30 | # The :test delivery method accumulates sent emails in the 31 | # ActionMailer::Base.deliveries array. 32 | config.action_mailer.delivery_method = :test 33 | 34 | # Print deprecation notices to the stderr. 35 | config.active_support.deprecation = :stderr 36 | 37 | # Silence a deprecation warning by explicitly setting sorted test order. 38 | config.active_support.test_order = :sorted 39 | end 40 | -------------------------------------------------------------------------------- /lib/emcee/document.rb: -------------------------------------------------------------------------------- 1 | require "emcee/node" 2 | require "nokogumbo" 3 | 4 | module Emcee 5 | # Document is responsible for parsing HTML and handling interaction with the 6 | # resulting document. 7 | class Document 8 | ENCODING = 'UTF-8' 9 | 10 | def initialize(data) 11 | @doc = Nokogiri::HTML5.parse("#{data}") 12 | end 13 | 14 | def to_s 15 | # Make an html string. The parser does weird things with certain 16 | # attributes, so turn those nodes into xhtml. 17 | xhtml_nodes = nodes_with_selected_attribute + nodes_with_src_attribute 18 | html = htmlify_except(xhtml_nodes) 19 | unescape(html).encode(ENCODING) 20 | end 21 | 22 | def html_imports 23 | wrap_nodes(@doc.css("link[rel='import']")) 24 | end 25 | 26 | def script_references 27 | wrap_nodes(@doc.css("script[src]")) 28 | end 29 | 30 | def style_references 31 | wrap_nodes(@doc.css("link[rel='stylesheet']")) 32 | end 33 | 34 | private 35 | 36 | # Get the html content of the document as a string. 37 | def to_html 38 | @doc.at("body").children.to_html(encoding: ENCODING).lstrip 39 | end 40 | 41 | def nodes_with_selected_attribute 42 | @doc.css("*[selected]") 43 | end 44 | 45 | def nodes_with_src_attribute 46 | @doc.css("*[src]:not(script)") 47 | end 48 | 49 | def wrap_nodes(nodes) 50 | nodes.map { |node| Emcee::Node.new(node) } 51 | end 52 | 53 | def unescape(content) 54 | content.gsub("&", "&") 55 | end 56 | 57 | # Generate an html string for the current document, but replace the provided 58 | # nodes with their xhtml strings. 59 | def htmlify_except(nodes) 60 | nodes.reduce(to_html) do |output, node| 61 | output.gsub(node.to_html, node.to_xhtml) 62 | end 63 | end 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /lib/generators/emcee/install/install_generator.rb: -------------------------------------------------------------------------------- 1 | module Emcee 2 | module Generators 3 | class InstallGenerator < Rails::Generators::Base 4 | source_root File.expand_path("../templates", __FILE__) 5 | 6 | desc "Adds an html import tag into your layout, adds a manifest to assets/components/application.html, and adds a vendor/assets/components directory" 7 | 8 | class_option :layout, :type => :string, :desc => "The path of the layout file to inject the html import tag into" 9 | 10 | def copy_application_manifest 11 | empty_directory "app/assets/components" 12 | copy_file "application.html", "app/assets/components/application.html" 13 | end 14 | 15 | def create_vendor_directory 16 | empty_directory "vendor/assets/components" 17 | create_file "vendor/assets/components/.keep" 18 | end 19 | 20 | def add_html_import_to_layout 21 | case 22 | when erb? 23 | insert_html_import("<%= html_import_tag \"application\", \"data-turbolinks-track\" => true %>", before: "<%= csrf_meta_tags %>") 24 | when haml? || slim? 25 | insert_html_import("= html_import_tag \"application\", \"data-turbolinks-track\" => true", before: "= csrf_meta_tags") 26 | end 27 | end 28 | 29 | def copy_bowerrc 30 | copy_file "template.bowerrc", ".bowerrc" 31 | end 32 | 33 | private 34 | 35 | def insert_html_import(content, options={}) 36 | # if this is a slim or haml file we need to match the indentation level 37 | if options[:before] && (line = layout_file.read.lines.detect {|l| l.include?(options[:before].to_s) }) 38 | indentation = line.match(/^\s+/).to_s 39 | content = content + "\n#{indentation}" 40 | end 41 | 42 | insert_into_file(layout_file.to_s, content, options) 43 | end 44 | 45 | def erb? 46 | layout_file.extname.match(/\.erb/) 47 | end 48 | 49 | def haml? 50 | layout_file.extname.match(/\.haml/) 51 | end 52 | 53 | def slim? 54 | layout_file.extname.match(/\.slim/) 55 | end 56 | 57 | def layout_file 58 | file = Pathname(options["layout"] || Dir[Rails.root.join("app","views","layouts","application*")].first) 59 | file.relative? ? file : file.relative_path_from(Rails.root) 60 | end 61 | 62 | end 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /test/document_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'emcee/document' 3 | 4 | class DocumentTest < ActiveSupport::TestCase 5 | setup do 6 | @body = <<-EOS.strip_heredoc 7 | 8 | 9 | 10 | 11 | EOS 12 | @doc = Emcee::Document.new(@body) 13 | end 14 | 15 | test "converts itself into a string" do 16 | assert_equal @body, @doc.to_s 17 | end 18 | 19 | test "can access html imports" do 20 | assert_equal 2, @doc.html_imports.length 21 | end 22 | 23 | test "can access scripts" do 24 | assert_equal 1, @doc.script_references.length 25 | end 26 | 27 | test "can access stylesheets" do 28 | assert_equal 1, @doc.style_references.length 29 | end 30 | 31 | test "optional attribute syntax should not be removed" do 32 | body = '

hidden

' 33 | doc = Emcee::Document.new(body) 34 | assert_equal body, doc.to_s 35 | end 36 | 37 | test "the selected attribute should be rendered correctly" do 38 | body = '

test

' 39 | doc = Emcee::Document.new(body) 40 | assert_equal body, doc.to_s 41 | end 42 | 43 | test "ampersands, spaces, and curly-brackets should be unescaped" do 44 | body = '

' 45 | doc = Emcee::Document.new(body) 46 | assert_equal body, doc.to_s 47 | end 48 | 49 | test "escaped single quotes in JavaScript should not be unescaped" do 50 | body = "" 51 | doc = Emcee::Document.new(body) 52 | assert_equal body, doc.to_s 53 | end 54 | 55 | test "escaped double quotes in JavaScript should not be unescaped" do 56 | body = '' 57 | doc = Emcee::Document.new(body) 58 | assert_equal body, doc.to_s 59 | end 60 | 61 | test "curly brackets in src attributes should be unescaped" do 62 | body = '' 63 | doc = Emcee::Document.new(body) 64 | assert_equal body, doc.to_s 65 | end 66 | 67 | test "umlauts and other utf-8 characters should be unescaped" do 68 | body = '' 69 | doc = Emcee::Document.new(body) 70 | assert_equal body, doc.to_s 71 | end 72 | 73 | test "document encoding should be utf-8" do 74 | doc = Emcee::Document.new('') 75 | assert_equal Encoding.find('UTF-8'), doc.to_s.encoding 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /test/compressors_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CompressorsTest < ActiveSupport::TestCase 4 | setup do 5 | @compressor = Emcee::Compressors::HtmlCompressor.new 6 | end 7 | 8 | test "compressor should remove html comments" do 9 | content = <<-EOS.strip_heredoc 10 | 14 | The span to end all spans 15 | EOS 16 | assert_equal @compressor.compress(content), <<-EOS.strip_heredoc 17 | The span to end all spans 18 | EOS 19 | end 20 | 21 | test "compressor should remove multi-line javascript comments" do 22 | content = <<-EOS.strip_heredoc 23 | 29 | EOS 30 | assert_equal @compressor.compress(content), <<-EOS.strip_heredoc 31 | 33 | EOS 34 | end 35 | 36 | test "compressor should remove single-line javascript comments" do 37 | content = <<-EOS.strip_heredoc 38 | 42 | EOS 43 | assert_equal @compressor.compress(content), <<-EOS.strip_heredoc 44 | 46 | EOS 47 | end 48 | 49 | test "compressor should remove css comments" do 50 | content = <<-EOS.strip_heredoc 51 | 59 | EOS 60 | assert_equal @compressor.compress(content), <<-EOS.strip_heredoc 61 | 66 | EOS 67 | end 68 | 69 | test "compressor should remove blank lines" do 70 | content = <<-EOS.strip_heredoc 71 |

test

72 | 73 | 74 | 75 |

oh yeah

76 | 77 |

test

78 | EOS 79 | assert_equal @compressor.compress(content), <<-EOS.strip_heredoc 80 |

test

81 |

oh yeah

82 |

test

83 | EOS 84 | end 85 | 86 | test "compressor should not attempt to remove javascript comments within a string" do 87 | content = <<-EOS.strip_heredoc 88 | 91 | EOS 92 | assert_equal @compressor.compress(content), content 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /test/dummy/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # Code is not reloaded between requests. 5 | config.cache_classes = true 6 | 7 | # Eager load code on boot. This eager loads most of Rails and 8 | # your application in memory, allowing both thread web servers 9 | # and those relying on copy on write to perform better. 10 | # Rake tasks automatically ignore this option for performance. 11 | config.eager_load = true 12 | 13 | # Full error reports are disabled and caching is turned on. 14 | config.consider_all_requests_local = false 15 | config.action_controller.perform_caching = true 16 | 17 | # Enable Rack::Cache to put a simple HTTP cache in front of your application 18 | # Add `rack-cache` to your Gemfile before enabling this. 19 | # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid. 20 | # config.action_dispatch.rack_cache = true 21 | 22 | # Disable Rails's static asset server (Apache or nginx will already do this). 23 | config.serve_static_assets = false 24 | 25 | # Compress JavaScripts and CSS. 26 | config.assets.js_compressor = :uglifier 27 | # config.assets.css_compressor = :sass 28 | 29 | # Do not fallback to assets pipeline if a precompiled asset is missed. 30 | config.assets.compile = false 31 | 32 | # Generate digests for assets URLs. 33 | config.assets.digest = true 34 | 35 | # Version of your assets, change this if you want to expire all your assets. 36 | config.assets.version = '1.0' 37 | 38 | # Specifies the header that your server uses for sending files. 39 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache 40 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx 41 | 42 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 43 | # config.force_ssl = true 44 | 45 | # Set to :debug to see everything in the log. 46 | config.log_level = :info 47 | 48 | # Prepend all log lines with the following tags. 49 | # config.log_tags = [ :subdomain, :uuid ] 50 | 51 | # Use a different logger for distributed setups. 52 | # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) 53 | 54 | # Use a different cache store in production. 55 | # config.cache_store = :mem_cache_store 56 | 57 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 58 | # config.action_controller.asset_host = "http://assets.example.com" 59 | 60 | # Precompile additional assets. 61 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. 62 | # config.assets.precompile += %w( search.js ) 63 | 64 | # Ignore bad email addresses and do not raise email delivery errors. 65 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 66 | # config.action_mailer.raise_delivery_errors = false 67 | 68 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 69 | # the I18n.default_locale when a translation can not be found). 70 | config.i18n.fallbacks = true 71 | 72 | # Send deprecation notices to registered listeners. 73 | config.active_support.deprecation = :notify 74 | 75 | # Disable automatic flushing of the log to improve performance. 76 | # config.autoflush_log = false 77 | 78 | # Use default logging formatter so that PID and timestamp are not suppressed. 79 | config.log_formatter = ::Logger::Formatter.new 80 | end 81 | -------------------------------------------------------------------------------- /test/processors_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'emcee/processors/import_processor' 3 | require 'emcee/processors/script_processor' 4 | require 'emcee/processors/stylesheet_processor' 5 | require 'emcee/document' 6 | 7 | # Create a stub of our asset resolver, so we can test if we're sending the 8 | # correct messages to it. 9 | class ResolverStub 10 | attr_reader :asset_required, :asset_dependent 11 | 12 | def initialize(contents) 13 | @contents = contents 14 | end 15 | 16 | def absolute_path(path) 17 | "/#{path}" 18 | end 19 | 20 | def require_asset(asset) 21 | @asset_required = true 22 | end 23 | 24 | def depend_on_asset(asset) 25 | @asset_dependent = true 26 | end 27 | 28 | def evaluate(path) 29 | @contents 30 | end 31 | 32 | def should_inline?(path) 33 | true 34 | end 35 | end 36 | 37 | class ProcessorsTest < ActiveSupport::TestCase 38 | setup do 39 | @resolver = ResolverStub.new "/* contents */" 40 | @body = <<-EOS.strip_heredoc 41 | 42 | 43 | 44 |

test

45 | EOS 46 | @doc = Emcee::Document.new(@body) 47 | end 48 | 49 | test "processing imports should work" do 50 | processor = Emcee::Processors::ImportProcessor.new(@resolver) 51 | processed = processor.process(@doc).to_s 52 | 53 | correct = <<-EOS.strip_heredoc 54 | 55 | 56 |

test

57 | EOS 58 | 59 | assert @resolver.asset_required 60 | assert_equal correct, processed 61 | end 62 | 63 | test "processing stylesheets should work" do 64 | processor = Emcee::Processors::StylesheetProcessor.new(@resolver) 65 | processed = processor.process(@doc).to_s 66 | 67 | correct = <<-EOS.strip_heredoc 68 | 69 | 70 | 71 |

test

72 | EOS 73 | 74 | assert @resolver.asset_dependent 75 | assert_equal correct, processed 76 | end 77 | 78 | test "processing scripts should work" do 79 | processor = Emcee::Processors::ScriptProcessor.new(@resolver) 80 | processed = processor.process(@doc).to_s 81 | 82 | correct = <<-EOS.strip_heredoc 83 | 84 | 85 | 86 |

test

87 | EOS 88 | 89 | assert @resolver.asset_dependent 90 | assert_equal correct, processed 91 | end 92 | 93 | test "processing scripts escapes " do 94 | @resolver = ResolverStub.new "// " 95 | 96 | processor = Emcee::Processors::ScriptProcessor.new(@resolver) 97 | processed = processor.process(@doc).to_s 98 | 99 | correct = <<-EOS.strip_heredoc 100 | 101 | 102 | 103 |

test

104 | EOS 105 | 106 | assert_equal correct, processed 107 | end 108 | 109 | test "processing scripts escapes