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

Import #46

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ function parseCss (src, filename, prefix, options, next) {
assert.equal(typeof options, 'object', 'options must be a object')
assert.equal(typeof next, 'function', 'done must be a function')

const processedCss = postcss()
.use(cssPrefix('.' + prefix))
var p = postcss()
if (options.global !== true) p = p.use(cssPrefix('.' + prefix))

const processedCss = p
.process(src.toString())
.toString()

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
"mkdirp": "^0.5.1",
"postcss": "^5.0.10",
"postcss-prefix": "^1.0.3",
"resolve": "^1.1.6",
"through2": "^2.0.0"
"resolve": "^1.1.7",
"through2": "^2.0.0",
"xtend": "^4.0.1"
},
"devDependencies": {
"browserify": "^13.0.0",
"concat-stream": "^1.5.1",
"css-type-base": "^1.0.2",
"dependency-check": "^2.5.1",
"istanbul": "^0.3.19",
"jsdom": "^8.0.2",
Expand Down
1 change: 1 addition & 0 deletions test/coverage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('./basic')
require('./disk')
require('./import')
require('./plugins')
require('./server')
require('./transform')
27 changes: 27 additions & 0 deletions test/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const browserify = require('browserify')
const concat = require('concat-stream')
const test = require('tape')
const path = require('path')
const fs = require('fs')

const sheetify = require(path.join(__dirname, '../transform'))
const expath = require.resolve('css-type-base/index.css')
const expected = fs.readFileSync(expath, 'utf8').trim()

test('import', function (t) {
t.plan(2)

const b = browserify(path.join(__dirname, 'import/source.js'), {
browserField: false
})
b.transform(sheetify, {
basedir: path.join(__dirname, 'plugins'),
out: concat(function (buf) {
const res = String(buf).trim()
t.equal(res, expected, 'package was imported')
})
})
b.bundle(function (err) {
t.ifError(err, 'no error')
})
})
3 changes: 3 additions & 0 deletions test/import/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const sf = require('sheetify')

sf('css-type-base')
31 changes: 11 additions & 20 deletions test/plugins.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
const browserify = require('browserify')
const jsdom = require('jsdom')
const concat = require('concat-stream')
const test = require('tape')
const path = require('path')
const fs = require('fs')

const sheetify = require(path.join(__dirname, '../transform'))
const expath = path.join(__dirname, 'plugins/expected.css')
const expected = fs.readFileSync(expath, 'utf8').trim()

test('plugins', function (t) {
t.plan(3)
t.plan(2)

const b = browserify(path.join(__dirname, 'plugins/source.js'), {
browserField: false
})
b.transform(sheetify, {
basedir: path.join(__dirname, 'plugins'),
use: [ [ 'sheetify-cssnext', { sourcemap: false } ] ]
use: [ [ 'sheetify-cssnext', { sourcemap: false } ] ],
out: concat(function (buf) {
const res = String(buf).trim()
t.equal(res, expected, 'css is transformed')
})
})
b.bundle(function (err, src) {
b.bundle(function (err) {
t.ifError(err, 'no error')

const virtualConsole = jsdom.createVirtualConsole()
virtualConsole.on('log', function (log) {
t.equal(log, '_08548a3e', 'reports prefix')
})

jsdom.env({
html: '<body></body>',
src: [ String(src) ],
done: world,
virtualConsole: virtualConsole
})

function world (err, window) {
t.ifError(err, 'no error')
}
})
})
4 changes: 4 additions & 0 deletions test/plugins/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
._08548a3e hello, ._08548a3e world {
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
}
29 changes: 29 additions & 0 deletions test/prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const browserify = require('browserify')
const concat = require('concat-stream')
const test = require('tape')
const path = require('path')
const fs = require('fs')

const sheetify = require(path.join(__dirname, '../transform'))

test('test global prefix', function (t) {
t.plan(2)

const b = browserify(path.join(__dirname, 'prefix/global-true.js'), {
browserField: false
})

b.transform(sheetify, {
basedir: path.join(__dirname, 'transform'),
o: concat(function (buf) {
const result = String(buf).trim()
const expectpath = path.join(__dirname, 'prefix/source.css')
const expected = fs.readFileSync(expectpath, 'utf8').trim()
t.equal(result, expected, 'exorcised to stream')
})
})

b.bundle(function (err, src) {
t.ifError(err, 'no error')
})
})
3 changes: 3 additions & 0 deletions test/prefix/global-true.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const sf = require('sheetify')

sf('./source.css', { global: true })
Empty file added test/prefix/input.css
Empty file.
2 changes: 2 additions & 0 deletions test/prefix/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1 {}
h1.title {}
60 changes: 55 additions & 5 deletions transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const isStream = require('is-stream')
const eos = require('end-of-stream')
const through = require('through2')
const falafel = require('falafel')
const resolve = require('resolve')
const assert = require('assert')
const mkdirp = require('mkdirp')
const xtend = require('xtend')
const path = require('path')
const fs = require('fs')

Expand All @@ -25,6 +27,8 @@ function transform (filename, opts) {
const nodes = []
var mname = null

opts = xtend(opts)

// argv parsing
if (opts.o) opts.out = opts.o
if (opts.out) {
Expand Down Expand Up @@ -56,12 +60,13 @@ function transform (filename, opts) {
self.push(null)
})

// asynchronously update css and apply to node
// or exorcise to file
// find sheetify call
// - read from file, read from inline or resolve npm package
// - detect if should be prefixed or not
function iterate (args, done) {
const transform = args[0]
const transformFn = args[0]
const node = args[1]
transform(function (err, css, prefix) {
transformFn(function (err, css, prefix) {
if (err) return done(err)
if (opts.out) {
// exorcise to external file
Expand Down Expand Up @@ -108,9 +113,11 @@ function transform (filename, opts) {
node.parent.tag.name === mname) {
const tmplCss = [ node.quasis.map(cooked) ]
.concat(node.expressions.map(expr)).join('').trim()

sheetify(tmplCss, filename, opts, function (tf) {
nodes.push([ tf, node.parent ])
})

return
}

Expand All @@ -119,15 +126,58 @@ function transform (filename, opts) {
if (node.type === 'CallExpression' &&
node.callee && node.callee.type === 'Identifier' &&
node.callee.name === mname) {
const fnp = path.join(path.dirname(filename), node.arguments[0].value)
// determine path
// - check if module import
// - don't prefix by default if module import
// - check if local file
const resolvePath = cssResolve(node.arguments[0].value, opts.basedir)
const fnp = resolvePath ||
path.join(path.dirname(filename), node.arguments[0].value)
if (resolvePath) opts.global = true
const fnCss = fs.readFileSync(fnp, 'utf8').trim()

// read optional arguments passed in to node
// e.g. { global: false }
if (node.arguments[1] && node.arguments[1].properties) {
const props = node.arguments[1].properties
props.forEach(function (prop) {
opts[prop.key.name] = prop.value.value
})
}

sheetify(fnCss, filename, opts, function (tf) {
nodes.push([ tf, node ])
})

return
}
}
}

function cooked (node) { return node.value.cooked }
function expr (ex) { return { _expr: ex.source() } }

// find a file in CSS with either a {style} field
// or main with `.css` in it
// (str, str) -> str
function cssResolve (pkgname, basedir) {
try {
const res = resolve.sync(pkgname, {
basedir: pkgname,
packageFilter: packageFilter
})

if (path.extname(res) !== '.css') {
throw new Error('path ' + res + ' is not a CSS file')
}

return res
} catch (e) {
return
}

function packageFilter (pkg, path) {
if (pkg.style) pkg.main = pkg.style
return pkg
}
}