Skip to content

Commit 9851d03

Browse files
committed
Added optional argument, inputContext (--x). When applied with --dir, output dir will represent nested folder structures found from a blobbed input.
1 parent 2d9c50f commit 9851d03

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ test: test/build \
1313
test-version \
1414
$(patsubst %,test/build/%.css,$(TESTS)) \
1515
test-multi \
16+
test-context \
1617
test-replace \
1718
test-local-plugins
1819

@@ -26,6 +27,10 @@ test-multi:
2627
./bin/postcss -u postcss-url --dir test/build test/multi*.css
2728
$(DIFF) test/build/multi*.css --to-file=test/ref
2829

30+
test-context:
31+
./bin/postcss -c test/config-context.js
32+
$(DIFF) test/build/test-context/in.css test/ref/test-context/in.css
33+
2934
test-replace:
3035
cp test/replace.css test/build/replace.css
3136
./bin/postcss -u postcss-url --postcss-url.url "inline" --replace test/build/replace.css

Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Output files location. Either `--output`, `--dir` or `--replace` option, but
3333
not all of them, need to be specified. `--dir` or `--replace` needs to be used
3434
if multiple input file is provided.
3535

36+
#### `--inputContext|-x`
37+
38+
Optional declaration of parent folder for input when `--dir` is used and an input glob may return nested directories. When applied, `--input` glob will be relative to `--inputContext` and folder structure inside `--inputContext` will be mimicked in `--dir`
39+
3640
#### `--replace|-r`
3741

3842
Replace input file(s) with generated output. Either `--output`, `--dir` or

index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var globby = require("globby");
22
var resolve = require("resolve");
3+
var path = require('path');
34
var argv = require("yargs")
45
.usage('Usage: $0 [--use|-u] plugin [--config|-c config.json] [--output|-o output.css] [input.css]')
56
.example('postcss --use autoprefixer -c options.json -o screen.css screen.css',
@@ -17,6 +18,8 @@ var argv = require("yargs")
1718
describe: 'lookup plugins in current node_modules directory'
1819
})
1920
.alias('i', 'input')
21+
.alias('x', 'inputContext')
22+
.describe('x', 'Directory context for input files when outputting to nested directories')
2023
.alias('o', 'output')
2124
.describe('o', 'Output file (stdout if not provided)')
2225
.alias('d', 'dir')
@@ -84,7 +87,9 @@ if (argv.use.indexOf("postcss-import") !== -1) {
8487
}
8588

8689
var inputFiles = argv._.length ? argv._ : argv.input;
87-
inputFiles = globby.sync(inputFiles);
90+
var inputPath = argv.inputContext ? path.join( argv.inputContext, inputFiles ) : inputFiles;
91+
inputFiles = globby.sync(inputPath);
92+
8893
if (!inputFiles.length) {
8994
// use stdin if nothing else is specified
9095
inputFiles = [undefined];
@@ -128,9 +133,7 @@ if (mapOptions === 'file') {
128133

129134
var async = require('neo-async');
130135
var fs = require('fs');
131-
var path = require('path');
132136
var readFile = require('read-file-stdin');
133-
var path = require('path');
134137
var postcss = require('postcss');
135138
var processor = postcss(plugins);
136139
var mkdirp = require('mkdirp');
@@ -173,9 +176,16 @@ function fsWatcher(entryPoints) {
173176
}
174177

175178
function compile(input, fn) {
179+
var inputPath;
176180
var output = argv.output;
177181
if (argv.dir) {
178-
output = path.join(argv.dir, path.basename(input));
182+
if (argv.inputContext) {
183+
inputPath = input.replace( argv.inputContext, '' );
184+
}
185+
else {
186+
inputPath = path.basename(input);
187+
}
188+
output = path.join( argv.dir, inputPath );
179189
} else if (argv.replace) {
180190
output = input;
181191
}

test/config-context.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
use: "postcss-url",
3+
input: "**/in.css",
4+
inputContext: "test",
5+
dir: "test/build",
6+
"postcss-url": {
7+
url: function(url) { return "http://example.com/" + url; }
8+
}
9+
};

test/ref/test-context/in.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: url(http://example.com/image.png);
3+
display: flex;
4+
}

test/test-context/in.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: url(image.png);
3+
display: flex;
4+
}

0 commit comments

Comments
 (0)