Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteMuse committed Jun 14, 2015
0 parents commit 34eb190
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript

# Mac shit
.DS_STORE

# Logs
server/logs/*
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 ByteMuse

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "cssnextify",
"version": "1.0.0",
"description": "A Browserify transform for css & cssnext",
"main": "src/index.js",
"directories": {
"test": "./node_modules/.bin/mocha tests"
},
"dependencies": {
"through2": "^2.0.0",
"cssnext": "^1.6.0"
},
"devDependencies": {
"browserify": "^10.2.4",
"jsdom": "^3.1.2",
"mocha": "^2.2.5"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/ByteMuse/cssnextify.git"
},
"keywords": [
"browserify",
"transform",
"css",
"cssnext"
],
"author": "ByteMuse",
"license": "MIT",
"bugs": {
"url": "https://github.com/ByteMuse/cssnextify/issues"
},
"homepage": "https://github.com/ByteMuse/cssnextify"
}
42 changes: 42 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var through = require('through2')
var cssnext = require('cssnext')

function canCompile(filename) {
return /\.css$/.test(filename)
}

function compile(data) {
function inject(css) {
var style = document.createElement('style')
style.type = 'text/css'
if(style.styleSheet) {
style.styleSheet.cssText = css
} else {
style.appendChild(document.createTextNode(css))
}

document.getElementsByTagName('head')[0].appendChild(style)
}

var style = cssnext(data)
return '(' + inject + ')(' + JSON.stringify(style) + ')'
}

module.exports = function(filename) {
if(!canCompile(filename)) {
return through()
}

var data = ''
function write(chunk, enc, cb) {
data += chunk
cb()
}

function end(cb) {
this.push(compile(data))
cb()
}

return through(write, end)
}
1 change: 1 addition & 0 deletions tests/fixtures/invalidStyle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {
3 changes: 3 additions & 0 deletions tests/fixtures/random.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function() {
return 2
}
9 changes: 9 additions & 0 deletions tests/fixtures/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:root {
--roses: red;
--violets: blue;
--iNeed: #C0FFEE;
}

body {
background-color: var(--violets);
}
39 changes: 39 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var assert = require('assert')
var cssnextify = require('../src/index')
var browserify = require('browserify')
var jsdom = require('jsdom')

describe('#nextify', function() {
it('should skip non-css files', function(done) {
var b = browserify()
b.add(__dirname + '/fixtures/random.js')
b.transform(cssnextify)
b.bundle(function(err, buf) {
assert.equal(err, null)
done()
})
})

it('should compile css input', function(done) {
var b = browserify()
b.add(__dirname + '/fixtures/style.css')
b.transform(cssnextify)
b.bundle(function(err, buf) {
assert.equal(err, null)
var html = '<html><head></head><body></body><script>' + buf.toString() + '</script></html>'
jsdom.env({
html: html,
features : {
FetchExternalResources : ['script'],
ProcessExternalResources : ['script']
},
done: function(err, window) {
assert.equal(err, null)
var bodyStyle = window.getComputedStyle(window.document.body)
assert.equal(bodyStyle.getPropertyValue('background-color'), 'blue')
done()
}
})
})
})
})

0 comments on commit 34eb190

Please sign in to comment.