diff --git a/index.js b/index.js index 0983d71..af84a18 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,37 @@ var cssnext = require("cssnext") var assign = require("object-assign") +var loaderUtils = require("loader-utils") -function cssnextLoader(contents){ +function defaultOptions(context, map){ + var options = {} + options.from = loaderUtils.getRemainingRequest(context) + options.to = loaderUtils.getRemainingRequest(context) + if (context.sourceMap) { + options.map = { + inline: false, + annotation: false, + prev: map + } + } + options.compress = context.minimize + return options +} + +function cssnextLoader(contents, map){ this.cacheable() - var options = assign({}, this.options.cssnext) + var options = assign({}, defaultOptions(this, map), this.options.cssnext) options.features = assign({}, this.options.cssnext ? this.options.cssnext.features : null) options.features.import = assign({}, options.features.import || null) options.features.import.onImport = function(files){ files.forEach(this.addDependency) }.bind(this) try { - return cssnext(contents, options) + var result = cssnext(contents, options) + if (result.css) { + this.callback(null, result.css, result.map) + } else { + return result + } } catch(err) { this.emitError(err) } diff --git a/package.json b/package.json index 6c5b7db..20b0c7b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "webpack loader for cssnext", "main": "index.js", "scripts": { - "start" : "npm install && npm test", + "start": "npm install && npm test", "test": "tape test/**.js" }, "repository": { @@ -25,6 +25,7 @@ "homepage": "https://github.com/cssnext/cssnext-loader", "dependencies": { "cssnext": "^0.6.0", + "loader-utils": "^0.2.5", "object-assign": "^2.0.0" }, "devDependencies": { diff --git a/test/index.js b/test/index.js index 05ec6a1..878ca1f 100644 --- a/test/index.js +++ b/test/index.js @@ -24,8 +24,42 @@ tape("cssnext-loader", function(test){ file += chunk }) .on("end", function(){ - test.equal(/color\s*:\s*red/.test(file), true) + test.ok(/color\s*:\s*red/.test(file), "css transformed") + test.notOk(/# sourceMappingURL=.*\s*$/.test(file), "source map annotation not added") test.end() }) }) }) + +tape("cssnext-loader source maps", function(test){ + webpack({ + entry: "./test/fixtures/index.js", + output: { + path: "./test/output/", + filename: "bundle.js" + }, + debug: true, + devtool: 'source-map', + cssnext : { + features : { + import : { + path : ["test/fixtures/"] + } + } + } + }, function(err, stat){ + var file = "" + test.plan(3) + fs.createReadStream("test/output/bundle.js") + .on("data", function(chunk){ + file += chunk + }) + .on("end", function(){ + test.ok(/color\s*:\s*red/.test(file), "css transformed") + test.ok(/# sourceMappingURL=.*\s*$/.test(file), "source map annotation added") + }) + fs.exists("test/output/bundle.js.map", function(exists){ + test.ok(exists, "source map exists") + }) + }) +})