Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit ac15bed

Browse files
committed
sourcemap !
Close #9
1 parent 30b7689 commit ac15bed

File tree

7 files changed

+89
-17
lines changed

7 files changed

+89
-17
lines changed

README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,12 @@ cssnext accept 2 arguments: a css string and an object of options.
9898

9999
#### Node.js options
100100

101-
_For now, options are passed to all postcss plugins._ This mean you should be able to any specific plugin option.
102-
103-
##### `from` (default: `null`)
104-
105-
Source of the file. Need to be provided to enable sourcemap.
106-
107-
```js
108-
var cssnext = require("cssnext")
109-
var fs = require("fs")
110-
var source = "./index.css"
111-
var output = cssnext(fs.readFileSync(source, "utf8"), {from: source})
112-
fs.writeFileSync("dist/index.css", output)
113-
```
101+
_For now, all options are passed to all postcss plugins._ This mean you should be able to any specific plugin options.
114102

115103
##### `features` (default: all features)
116104

117-
Object containing key of features to enable. _No key means feature is enabled_
105+
Object containing key of features to enable/disable.
106+
_No key means feature is enabled_.
118107

119108
```js
120109
//eg: disable color support
@@ -141,6 +130,30 @@ See [autoprefixer documentation of this option for more details](https://github.
141130

142131
Default to something like `["> 1%", "last 2 versions", "Firefox ESR"]`.
143132

133+
##### `sourcemap` (default: `false`)
134+
135+
**If you want a accurate sourcemap, please use instead the `from` option.**
136+
137+
This option is a shortcut to enable inlined sourcemap in the output.
138+
Just pass `true` to get the sourcemap at the end of the output.
139+
If you want better control on sourcemap, use [postcss `map` option](https://github.com/postcss/postcss#source-map-1) directly.
140+
141+
##### `from` (default: `null`)
142+
143+
Source of the file. **Enable `sourcemap` option automatically** (except if you provide both `from` & `sourcemap`).
144+
145+
```js
146+
var cssnext = require("cssnext")
147+
var fs = require("fs")
148+
149+
var source = "./index.css"
150+
var output = cssnext(
151+
fs.readFileSync(source, "utf8"),
152+
{from: source}
153+
)
154+
fs.writeFileSync("dist/index.css", output)
155+
```
156+
144157
### Usage with other tools
145158

146159
Here are some tools that will help you to use cssnext in your current workflow

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ function cssnext(string, options) {
3131
options = options || {}
3232
var features = options.features || {}
3333

34+
// default sourcemap
35+
// if `map` option is passed, sourcemap option is ignored
36+
// if `sourcemap` option is passed, a default map is used (insert content in the output)
37+
// if `from` option is passed, we assume sourcemap is wanted
38+
options.map = options.map ||
39+
(
40+
(options.sourcemap || (options.sourcemap !== false && options.from)) ?
41+
{
42+
inline: true,
43+
sourcesContent: true
44+
} :
45+
null
46+
)
47+
3448
var postcss = Postcss()
3549

3650
Object.keys(cssnext.features).forEach(function(key) {

test/index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ test("cssnext", function(t) {
88
t.end()
99
})
1010

11+
/**
12+
* Features tests
13+
*/
1114
var toSlug = require("to-slug-case")
1215
Object.keys(cssnext.features).forEach(function(name) {
1316
var slug = toSlug(name)
@@ -16,18 +19,40 @@ Object.keys(cssnext.features).forEach(function(name) {
1619
var expected = read("features/" + slug + ".expected")
1720

1821
test(slug, function(t) {
19-
t.equal(cssnext(input, {from: source}), expected, "should add " + slug + " support")
22+
var options = {from: source, sourcemap: false, features: {}}
23+
24+
// disable all features
25+
Object.keys(cssnext.features).forEach(function(key) { options.features[key] = false })
2026

21-
var options = {from: source, features: {}}
22-
options.features[name] = false
2327
var css = cssnext(input, options)
2428
t.notEqual(css, expected, "should not add " + slug + " support if disabled")
2529
t.equal(css, input, "should not modify input if " + slug + " is disabled")
2630

31+
// enable only the one we want to test
32+
options.features[name] = true
33+
t.equal(cssnext(input, options), expected, "should add " + slug + " support")
34+
2735
t.end()
2836
})
2937
})
3038

39+
/**
40+
* Sourcemap tests
41+
*/
42+
43+
test("sourcemap", function(t) {
44+
t.equal(
45+
cssnext(
46+
read("sourcemap/input"),
47+
{from: "./test/sourcemap/input.css"}
48+
),
49+
read("sourcemap/expected"),
50+
"should contain a correct sourcemap"
51+
)
52+
53+
t.end()
54+
})
55+
3156
/**
3257
* Resolve a fixture by `filename`.
3358
*

test/sourcemap/expected.css

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/sourcemap/imported.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
html {
2+
background: blue;
3+
}

test/sourcemap/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!doctype>
2+
<link rel="stylesheet" href="out.css" />
3+
<h1>Please view source &amp; check sourcemap</h1>

test/sourcemap/input.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import "imported.css";
2+
3+
body {
4+
color: red
5+
}

0 commit comments

Comments
 (0)