Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ifyouseewendy/b12b90cce897c52e2ac5dae62412cc77 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/b12b90cce897c52e2ac5dae62412cc77 to your computer and use it in GitHub Desktop.
Use Sprockets outside Rack application. I'm working on a script project which is non Rack based. I want to send users an email notification when the script detects a state change. To use Sprockets outside a Rack application is not a common case, so there are not many articles talking about it online. You could count on this.
desc 'Compile scss files using Sprockets
+ source file: #{root}/lib/assets/stylesheets/application.scss,
in which you could require the third party assets
+ output file: #{root}/build/emai.css
'
task :compile do
require 'third_party_assets'
root = '~/Workspace/project/'
ROOT = Pathname(root)
LOGGER = Logger.new(STDOUT)
BUILD_DIR = ROOT.join('build')
BUILD_FILE = 'email.css'
SOURCE_DIR = ROOT.join('lib/assets')
SOURCE_FILE = 'application.scss'
environment = Sprockets::Environment.new(ROOT) do |env|
env.logger = LOGGER
end
environment.css_compressor = :scss
environment.append_path(SOURCE_DIR.join('stylesheets').to_s)
assets_path = Bundler.rubygems.find_name('third_party_assets').first.full_gem_path
environment.append_path("#{assets_path}/app/assets/stylesheets")
assets = environment.find_asset(SOURCE_FILE)
assets.write_to(BUILD_DIR.join(BUILD_FILE))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment