Skip to content

Commit 1bad988

Browse files
committed
Merge remote-tracking branch 'origin/source-maps'
2 parents 40b1a5e + 53d4da3 commit 1bad988

File tree

7 files changed

+74
-6
lines changed

7 files changed

+74
-6
lines changed

Makefile

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ all: clean lint test
33
lint:
44
./node_modules/.bin/jshint *.js
55

6-
TESTS = opts stdout stdin config config-all js-config js-config-all invalid
6+
TESTS = opts source-maps source-maps-file stdout stdin config config-all js-config js-config-all invalid
7+
8+
79
DIFF = diff -q
810

9-
test: test/build test-help test-version $(patsubst %,test/build/%.css,$(TESTS)) test-multi test-replace test-local-plugins
11+
test: test/build \
12+
test-help \
13+
test-version \
14+
$(patsubst %,test/build/%.css,$(TESTS)) \
15+
test-multi \
16+
test-replace \
17+
test-local-plugins
1018

1119
test-help:
1220
./bin/postcss --help
@@ -41,6 +49,15 @@ test/build/opts.css: test/in.css
4149
./bin/postcss -u postcss-url --postcss-url.url=rebase -o $@ $<
4250
$(DIFF) $@ $(subst build,ref,$@)
4351

52+
test/build/source-maps.css: test/in.css
53+
./bin/postcss -u postcss-url --postcss-url.url=rebase --map -o $@ $<
54+
$(DIFF) $@ $(subst build,ref,$@)
55+
56+
test/build/source-maps-file.css: test/in.css
57+
./bin/postcss -u postcss-url --postcss-url.url=rebase --map file -o $@ $<
58+
$(DIFF) $@ $(subst build,ref,$@)
59+
$(DIFF) ${@}.map $(subst build,ref,${@}.map)
60+
4461
test/build/stdout.css: test/in.css
4562
./bin/postcss --use ./test/dummy-plugin $< > $@
4663
$(DIFF) $@ $(subst build,ref,$@)

Readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ Replace input file(s) with generated output. Either `--output`, `--dir` or
4343

4444
Plugin to be used. Multiple plugins can be specified. At least one plugin needs to be specified either with `--use` option or in the config file.
4545

46+
#### `--map|-m`
47+
48+
Activate source map generation. By default inline maps are generated. To generate source maps
49+
in a separate _.map_ file use `--map file` or `--no-map.inline`.
50+
51+
You can use [advances source map options][source-map-options] - some examples:
52+
53+
- `--no-map` - do not generated source maps - even if previous maps exist
54+
- `--map.annotation <path>` - specify alternaive path to be used in source map annotation appended to CSS
55+
- `--no-map.annotation` - supress adding annotation to CSS
56+
- `--no-map.sourcesContent` - remove origin CSS from maps
57+
4658
#### `--local-plugins`
4759

4860
Look up plugins starting from `node_modules` located in the current working
@@ -153,3 +165,4 @@ MIT
153165

154166
[postcss]: https://github.com/postcss/postcss
155167
[postcss-import]: https://github.com/postcss/postcss-import
168+
[source-map-options]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md

index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var argv = require("yargs")
2121
.describe('o', 'Output file (stdout if not provided)')
2222
.alias('d', 'dir')
2323
.describe('d', 'Output directory')
24+
.alias('m', 'map')
25+
.describe('m', 'Source map')
2426
.boolean('r')
2527
.alias('r', 'replace')
2628
.describe('r', 'Replace input file(s) with generated output')
@@ -119,6 +121,13 @@ var customSyntaxOptions = ['syntax', 'parser', 'stringifier']
119121
return cso;
120122
}, Object.create(null));
121123

124+
125+
var mapOptions = argv.map;
126+
// treat `--map file` as `--no-map.inline`
127+
if (mapOptions === 'file') {
128+
mapOptions = { inline: false };
129+
}
130+
122131
var async = require('neo-async');
123132
var fs = require('fs');
124133
var readFile = require('read-file-stdin');
@@ -170,6 +179,7 @@ function compile(input, fn) {
170179
} else if (argv.replace) {
171180
output = input;
172181
}
182+
173183
processCSS(processor, input, output, fn);
174184
}
175185

@@ -179,7 +189,7 @@ function processCSS(processor, input, output, fn) {
179189
if (typeof result.warnings === 'function') {
180190
result.warnings().forEach(console.error);
181191
}
182-
fn(null, result.css);
192+
fn(null, result);
183193
}
184194

185195
var options = {
@@ -191,18 +201,23 @@ function processCSS(processor, input, output, fn) {
191201
options[opt] = customSyntaxOptions[opt];
192202
});
193203

204+
if (typeof mapOptions !== 'undefined') {
205+
options.map = mapOptions;
206+
}
207+
194208
var result = processor.process(css, options);
209+
195210
if (typeof result.then === 'function') {
196211
result.then(onResult).catch(fn);
197-
} else{
212+
} else {
198213
process.nextTick(onResult.bind(null, result));
199214
}
200215
}
201216

202217
async.waterfall([
203218
async.apply(readFile, input),
204219
doProcess,
205-
async.apply(writeFile, output)
220+
async.apply(writeResult, output)
206221
], fn);
207222
}
208223

@@ -219,6 +234,16 @@ function onError(err, keepAlive) { // XXX: avoid overloaded signature?
219234
}
220235
}
221236

237+
function writeResult (name, content, fn) {
238+
var funcs = [
239+
async.apply(writeFile, name, content.css)
240+
];
241+
if (content.map && name) {
242+
funcs.push(async.apply(writeFile, name + '.map', content.map.toString()));
243+
}
244+
async.parallel(funcs, fn);
245+
}
246+
222247
function writeFile(name, content, fn) {
223248
if (!name) {
224249
process.stdout.write(content);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
"postcss-import": "^7.1.0",
3434
"postcss-url": "^4.0.0"
3535
}
36-
}
36+
}

test/ref/source-maps-file.css

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

test/ref/source-maps-file.css.map

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

test/ref/source-maps.css

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

0 commit comments

Comments
 (0)