Skip to content

Commit bb7a18e

Browse files
author
Andy VanWagoner
committed
Improve source map support
- Populate the from, to, map, and compress options from webpack info - Pass on generated source map
1 parent 700e58a commit bb7a18e

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

index.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
var cssnext = require("cssnext")
22
var assign = require("object-assign")
3+
var loaderUtils = require("loader-utils")
34

4-
function cssnextLoader(contents){
5+
function defaultOptions(context, map){
6+
var options = {}
7+
options.from = loaderUtils.getRemainingRequest(context)
8+
options.to = loaderUtils.getRemainingRequest(context)
9+
if (context.sourceMap) {
10+
options.map = {
11+
inline: false,
12+
annotation: false,
13+
prev: map
14+
}
15+
}
16+
options.compress = context.minimize
17+
return options
18+
}
19+
20+
function cssnextLoader(contents, map){
521
this.cacheable()
6-
var options = assign({}, this.options.cssnext)
22+
var options = assign({}, defaultOptions(this, map), this.options.cssnext)
723
options.features = assign({}, this.options.cssnext ? this.options.cssnext.features : null)
824
options.features.import = assign({}, options.features.import || null)
925
options.features.import.onImport = function(files){
1026
files.forEach(this.addDependency)
1127
}.bind(this)
1228
try {
13-
return cssnext(contents, options)
29+
var result = cssnext(contents, options)
30+
if (result.css) {
31+
this.callback(null, result.css, result.map)
32+
} else {
33+
return result
34+
}
1435
} catch(err) {
1536
this.emitError(err)
1637
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "webpack loader for cssnext",
55
"main": "index.js",
66
"scripts": {
7-
"start" : "npm install && npm test",
7+
"start": "npm install && npm test",
88
"test": "tape test/**.js"
99
},
1010
"repository": {
@@ -25,6 +25,7 @@
2525
"homepage": "https://github.com/cssnext/cssnext-loader",
2626
"dependencies": {
2727
"cssnext": "^0.6.0",
28+
"loader-utils": "^0.2.5",
2829
"object-assign": "^2.0.0"
2930
},
3031
"devDependencies": {

test/index.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,42 @@ tape("cssnext-loader", function(test){
2424
file += chunk
2525
})
2626
.on("end", function(){
27-
test.equal(/color\s*:\s*red/.test(file), true)
27+
test.ok(/color\s*:\s*red/.test(file), "css transformed")
28+
test.notOk(/# sourceMappingURL=.*\s*$/.test(file), "source map annotation not added")
2829
test.end()
2930
})
3031
})
3132
})
33+
34+
tape("cssnext-loader source maps", function(test){
35+
webpack({
36+
entry: "./test/fixtures/index.js",
37+
output: {
38+
path: "./test/output/",
39+
filename: "bundle.js"
40+
},
41+
debug: true,
42+
devtool: 'source-map',
43+
cssnext : {
44+
features : {
45+
import : {
46+
path : ["test/fixtures/"]
47+
}
48+
}
49+
}
50+
}, function(err, stat){
51+
var file = ""
52+
test.plan(3)
53+
fs.createReadStream("test/output/bundle.js")
54+
.on("data", function(chunk){
55+
file += chunk
56+
})
57+
.on("end", function(){
58+
test.ok(/color\s*:\s*red/.test(file), "css transformed")
59+
test.ok(/# sourceMappingURL=.*\s*$/.test(file), "source map annotation added")
60+
})
61+
fs.exists("test/output/bundle.js.map", function(exists){
62+
test.ok(exists, "source map exists")
63+
})
64+
})
65+
})

0 commit comments

Comments
 (0)