diff --git a/index.js b/index.js index 63b5c79..f7520b4 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,14 @@ var through = require("through2") var cssnext = require("cssnext") var gutil = require("gulp-util") +var objectAssign = require("object-assign") var PluginError = gutil.PluginError function transform(opts) { return function(file, enc, cb) { var contents var transformed - var options = opts || {} + var options = objectAssign({}, opts) if (file.isStream()) { return cb( new PluginError("gulp-cssnext", "streaming not supported") diff --git a/package.json b/package.json index 6c7f2a0..c5b4899 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "dependencies": { "cssnext": "^1.0.0", "gulp-util": "^3.0.0", + "object-assign": "~4.0.1", "through2": "^0.6.1" }, "devDependencies": { diff --git a/test/cssnext.js b/test/cssnext.js index 74230ec..a477b35 100644 --- a/test/cssnext.js +++ b/test/cssnext.js @@ -1,31 +1,25 @@ var tape = require("tape") var gutil = require("gulp-util") +var through2 = require("through2") var cssnext = require("..") var fs = require("fs") var path = require("path") +function read(file, encoding) { + return fs.readFileSync(path.resolve(__dirname, file), encoding) +} + tape("cssnext", function(test) { var stream = cssnext() var file = new gutil.File({ base: ".", path: ".", - contents: new Buffer( - fs.readFileSync( - path.resolve(__dirname, "fixtures/index.css"), - {encoding: "utf8"} - ) - ), + contents: read("fixtures/index.css"), }) stream.on("data", function(data) { - test.equal( - data.contents.toString(), - fs.readFileSync( - path.resolve(__dirname, "expected/index.css"), - {encoding: "utf8"} - ) - ) + test.equal(data.contents.toString(), read("expected/index.css", "utf8")) test.end() }) stream.end(file) @@ -36,10 +30,7 @@ tape("cssnext throws if stream", function(test) { var file = new gutil.File({ base: ".", path: ".", - contents: fs.createReadStream( - path.resolve(__dirname, "fixtures/index.css"), - {encoding: "utf8"} - ), + contents: through2(), }) stream.on("error", function(err) { test.equal(err.message, "streaming not supported") @@ -72,45 +63,33 @@ tape("throws on cssnext error", function(test) { tape( "Resolves relative paths for consecutive files in different paths", function(test) { - var stream = cssnext() - var file = new gutil.File({ - base: "./test/fixtures/", - path: "./test/fixtures/import.css", - contents: new Buffer( - fs.readFileSync( - path.resolve(__dirname, "fixtures/import.css"), - {encoding: "utf8"} - ) - ), - }) - stream.write(file) + test.plan(4) - var file2 = new gutil.File({ - base: "./test/fixtures/import/", - path: "./test/fixtures/import/index.css", - contents: new Buffer( - fs.readFileSync( - path.resolve(__dirname, "fixtures/import/index.css"), - {encoding: "utf8"} - ) - ), - }) - stream.write(file2) + function testTwoFiles(options) { + var stream = cssnext(options) - var count = 0 - stream.on("data", function(data) { + var file1 = new gutil.File({ + base: "./test/fixtures/", + path: "./test/fixtures/import.css", + contents: read("fixtures/import.css"), + }) - test.equal( - data.contents.toString(), - fs.readFileSync( - path.resolve(__dirname, "expected/index.css"), - {encoding: "utf8"} - ) - ) - ++count - if (count === 2) { - test.end() - } - }) + var file2 = new gutil.File({ + base: "./test/fixtures/import/", + path: "./test/fixtures/import/index.css", + contents: read("fixtures/import/index.css"), + }) + + stream.on("data", function(data) { + var expected = read("expected/index.css", "utf8") + test.equal(data.contents.toString(), expected) + }) + + stream.write(file1) + stream.end(file2) + } + + testTwoFiles() + testTwoFiles({}) } )