Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace node-sassy with node-sass (eliminate Ruby requirement) #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions lib/modules/sass.coffee
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
fs = require 'fs'
pathutil = require 'path'

sassy = require "node-sassy"

path = require 'path'
{Asset} = require '../.'

try
sassy = false
sass = require 'node-sass'
catch err
sassy = true
sass = require 'node-sassy'
sass.render = (opts) ->
sassOpts = includeFrom: opts.includePaths, '--style': opts.outputStyle
sass.compile opts.file, sassOpts, (err, css) ->
return opts.error err if err?
opts.success css

urlRegex = /url\s*\(\s*(['"])((?:(?!\1).)+)\1\s*\)/
urlRegexGlobal = /url\s*\(\s*(['"])((?:(?!\1).)+)\1\s*\)/g

class exports.SassAsset extends Asset
mimetype: 'text/css'

create: (options) ->
@filename = pathutil.resolve options.filename
@toWatch = pathutil.dirname @filename
@paths = options.paths
@paths ?= []
@paths.push pathutil.dirname options.filename

@compress = options.compress
@compress ?= false

sassOpts = {}
if options.paths
sassOpts.includeFrom = options.paths

if @compress
sassOpts["--style"] = "compressed"

# Render the sass to css
sassy.compile @filename, sassOpts, (err, css) =>
return @emit 'error', err if err?

if @rack?
results = css.match urlRegexGlobal
if results
for result in results
match = urlRegex.exec result
quote = match[1]
url = match[2]
specificUrl = @rack.url url
if specificUrl?
css = css.replace result, "url(#{quote}#{specificUrl}#{quote})"

@emit 'created',
contents: css
@sassy: sassy
mimetype: 'text/css'

postProcess: (css) ->
return css unless @rack?
results = css.match urlRegexGlobal
return css unless results?
for result in results
match = urlRegex.exec result
quote = match[1]
url = match[2]
specificUrl = @rack.url url
css = css.replace result, "url(#{quote}#{specificUrl}#{quote})" if specificUrl?
css

create: (options) ->
throw new Error 'Invalid options' unless options? and options.filename?
@filename = path.resolve options.filename
@toWatch = path.dirname @filename
sass.render
file: @filename
includePaths: options.paths ? [path.dirname options.filename]
outputStyle: if options.compress then 'compressed' else 'nested'
error: (err) => @emit 'error', err
success: (css) => @emit 'created', contents: @postProcess css
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
"underscore": "1.4.4",
"coffee-script": "~1.6.2",
"markdown": "0.4.0",
"node-sassy": "0.0.1"
"node-sassy": "~0.0.1"
},
"devDependencies": {
"express.io": "1.1.8",
"request": "2.12.0",
"mocha": "1.8.1",
"chai": "1.4.2"
},
"optionalDependencies": {
"node-sass": "~0.5.0"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha test/test.coffee",
"compile": "./node_modules/coffee-script/bin/coffee -o compiled/ -c lib/",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/sass/another.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

body {
background-color: #33aa33;
background-image: url("/background.png"); }
background-image: url('/background.png'); }
12 changes: 12 additions & 0 deletions test/fixtures/sass/another.css.sassy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.content-navigation {
border-color: #33aa33;
color: #288728; }

.border {
padding: 8px;
margin: 8px;
border-color: #33aa33; }

body {
background-color: #33aa33;
background-image: url("/background.png"); }
2 changes: 1 addition & 1 deletion test/fixtures/sass/simple.min.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.content-navigation{border-color:#3bbfce;color:#2ca2af}.border{padding:8px;margin:8px;border-color:#3bbfce}
.content-navigation{border-color:#3bbfce;color:#2ca2af;}.border{padding:8px;margin:8px;border-color:#3bbfce;}
1 change: 1 addition & 0 deletions test/fixtures/sass/simple.min.css.sassy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.content-navigation{border-color:#3bbfce;color:#2ca2af}.border{padding:8px;margin:8px;border-color:#3bbfce}
11 changes: 0 additions & 11 deletions test/fixtures/sass/simple.sass

This file was deleted.

20 changes: 6 additions & 14 deletions test/sass.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,11 @@ describe 'a sass asset', ->
response.headers['content-type'].should.equal 'text/css'
body.should.equal compiled
done()

it 'should work with .sass', (done) ->
compiled = fs.readFileSync "#{__dirname}/fixtures/sass/simple.css", 'utf8'
app = express().http()
app.use new rack.SassAsset
filename: "#{__dirname}/fixtures/sass/simple.sass"
url: '/style.css'
app.listen 7076, ->
easyrequest 'http://localhost:7076/style.css', (error, response, body) ->
response.headers['content-type'].should.equal 'text/css'
body.should.equal compiled
done()

it 'should work compressed', (done) ->
compiled = fs.readFileSync "#{__dirname}/fixtures/sass/simple.min.css", 'utf8'
file = "#{__dirname}/fixtures/sass/simple.min.css"
file = "#{file}.sassy" if rack.SassAsset.sassy
compiled = fs.readFileSync file, 'utf8'
app = express().http()
app.use new rack.SassAsset
filename: "#{__dirname}/fixtures/sass/simple.scss"
Expand All @@ -46,7 +36,9 @@ describe 'a sass asset', ->
done()

it 'should work with paths', (done) ->
compiled = fs.readFileSync "#{__dirname}/fixtures/sass/another.css", 'utf8'
file = "#{__dirname}/fixtures/sass/another.css"
file = "#{file}.sassy" if rack.SassAsset.sassy
compiled = fs.readFileSync file, 'utf8'
app = express().http()
app.use new rack.SassAsset
filename: "#{__dirname}/fixtures/sass/another.scss"
Expand Down
4 changes: 1 addition & 3 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ require './dynamic'
require './stylus'
require './collection'
require './walk'

# Sass is commented out because it requires ruby
# require './sass'
require './sass'