Skip to content

Commit 00c17b9

Browse files
committed
bin/pscss: add --sourcemap command line option
1 parent da60a39 commit 00c17b9

File tree

4 files changed

+97
-13
lines changed

4 files changed

+97
-13
lines changed

bin/pscss

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ include __DIR__ . '/../scss.inc.php';
2020

2121
use Leafo\ScssPhp\Compiler;
2222
use Leafo\ScssPhp\Parser;
23+
use Leafo\ScssPhp\SourceMap;
2324
use Leafo\ScssPhp\Version;
2425

25-
$style = null;
26-
$loadPaths = null;
2726
$precision = null;
27+
$style = null;
2828
$dumpTree = false;
29-
$inputFile = null;
29+
$loadPaths = null;
3030
$changeDir = false;
31+
$inputFile = null;
32+
$outputFile = null;
33+
$sourceMapFile = null;
34+
$sourceMap = SourceMap::NONE;
3135
$debugInfo = false;
3236
$lineNumbers = false;
3337

@@ -63,18 +67,20 @@ for ($i = 1; $i < $argc; $i++) {
6367
$exe = $argv[0];
6468

6569
$HELP = <<<EOT
66-
Usage: $exe [options] [input-file]
70+
Usage: $exe [options] [input-file [output-file.css [sourcemap-file.map]]]
6771
6872
Options include:
6973
70-
--debug-info Annotate selectors with CSS referring to the source file and line number
71-
-h, --help Show this message
72-
-f=format Set the output format (compact, compressed, crunched, expanded, or nested)
73-
-i=path Set import path
74-
--line-numbers Annotate selectors with comments referring to the source file and line number
75-
-p=precision Set decimal number precision (default 5)
76-
-T Dump formatted parse tree
77-
-v, --version Print the version
74+
--debug-info Annotate selectors with CSS referring to the source file and line number
75+
-h, --help Show this message
76+
-f=format Set the output format (compact, compressed, crunched, expanded, or nested)
77+
-i=path Set import path
78+
--line-numbers Annotate selectors with comments referring to the source file and line number
79+
-p=precision Set decimal number precision (default 5)
80+
--sourcemap Generate sourcemap (default: auto)
81+
--sourcemap=option Set the sourcmap option (auto, file, inline, or none)
82+
-T Dump formatted parse tree
83+
-v, --version Print the version
7884
7985
EOT;
8086
exit($HELP);
@@ -99,6 +105,18 @@ EOT;
99105
continue;
100106
}
101107

108+
if ($argv[$i] === '--sourcemap') {
109+
$sourceMap = SourceMap::AUTO;
110+
continue;
111+
}
112+
113+
$value = parseArgument($i, array('--sourcemap'));
114+
115+
if (isset($value)) {
116+
$sourceMap = $value;
117+
continue;
118+
}
119+
102120
$value = parseArgument($i, array('-f', '--style'));
103121

104122
if (isset($value)) {
@@ -120,12 +138,35 @@ EOT;
120138
continue;
121139
}
122140

123-
if (file_exists($argv[$i])) {
141+
if ($inputFile === null) {
124142
$inputFile = $argv[$i];
125143
continue;
126144
}
145+
146+
if ($outputFile === null) {
147+
$outputFile = $argv[$i];
148+
continue;
149+
}
150+
151+
if ($sourceMapFile === null) {
152+
$sourceMapFile = $argv[$i];
153+
continue;
154+
}
155+
156+
exit("{$argv[0]}: Unrecognized parameter: {$argv[$i]}\n");
157+
}
158+
159+
if (! in_array($sourceMap, array(SourceMap::NONE, SourceMap::AUTO, SourceMap::FILE, SourceMap::INLINE))) {
160+
exit("{$argv[0]}: Unrecognized sourcemap option: $sourceMap\n");
127161
}
128162

163+
if ($inputFile) {
164+
if ($inputFile === '-') {
165+
$inputFile = null;
166+
} elseif (! file_exists($inputFile)) {
167+
exit("{$argv[0]}: Input file: $inputFile doesn't exist\n");
168+
}
169+
}
129170

130171
if ($inputFile) {
131172
$data = file_get_contents($inputFile);
@@ -154,6 +195,7 @@ if ($dumpTree) {
154195
}
155196

156197
$scss = new Compiler();
198+
$scss->setSourceMap($sourceMap);
157199

158200
if ($debugInfo && $inputFile) {
159201
$scss->setLineNumberStyle(Compiler::DEBUG_INFO);

scss.inc.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
include_once __DIR__ . '/src/Node.php';
2121
include_once __DIR__ . '/src/Node/Number.php';
2222
include_once __DIR__ . '/src/Parser.php';
23+
include_once __DIR__ . '/src/SourceMap.php';
24+
include_once __DIR__ . '/src/SourceMap/Base64.php';
25+
include_once __DIR__ . '/src/SourceMap/Base64VLQ.php';
2326
include_once __DIR__ . '/src/Type.php';
2427
include_once __DIR__ . '/src/Util.php';
2528
include_once __DIR__ . '/src/Version.php';

src/Compiler.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Leafo\ScssPhp\Node;
2020
use Leafo\ScssPhp\Type;
2121
use Leafo\ScssPhp\Parser;
22+
use Leafo\ScssPhp\SourceMap;
2223
use Leafo\ScssPhp\Util;
2324

2425
/**
@@ -117,6 +118,7 @@ class Compiler
117118
);
118119

119120
protected $lineNumberStyle = null;
121+
protected $sourceMap = SourceMap::NONE;
120122

121123
protected $formatter = 'Leafo\ScssPhp\Formatter\Nested';
122124

@@ -3089,6 +3091,18 @@ public function setLineNumberStyle($lineNumberStyle)
30893091
$this->lineNumberStyle = $lineNumberStyle;
30903092
}
30913093

3094+
/**
3095+
* Set source map
3096+
*
3097+
* @api
3098+
*
3099+
* @param string $sourceMap
3100+
*/
3101+
public function setSourceMap($sourceMap)
3102+
{
3103+
$this->sourceMap = $sourceMap;
3104+
}
3105+
30923106
/**
30933107
* Register function
30943108
*

src/SourceMap.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* SCSSPHP
4+
*
5+
* @copyright 2012-2015 Leaf Corcoran
6+
*
7+
* @license http://opensource.org/licenses/MIT MIT
8+
*
9+
* @link http://leafo.github.io/scssphp
10+
*/
11+
12+
namespace Leafo\ScssPhp;
13+
14+
/**
15+
* SCSSPHP source map
16+
*
17+
* @author Anthon Pang <anthon.pang@gmail.com>
18+
*/
19+
class SourceMap
20+
{
21+
const AUTO = 'auto'; // relative URI's where possible
22+
const FILE = 'file'; // file: URIs
23+
const INLINE = 'inline'; // include full source text in the source map
24+
const NONE = 'none'; // default
25+
}

0 commit comments

Comments
 (0)