Skip to content

Commit eb4ab3f

Browse files
committed
Adds File based source map (in addition to inline source map) - SOURCE_MAP_FILE, using code from less.php; ONLY if there is output from the scss file.
1 parent 2e1120b commit eb4ab3f

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

scss.inc.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
include_once __DIR__ . '/src/Compiler/Environment.php';
1212
include_once __DIR__ . '/src/Exception/CompilerException.php';
1313
include_once __DIR__ . '/src/Exception/ParserException.php';
14+
include_once __DIR__ . '/src/Exception/RangeException.php'; //exp
1415
include_once __DIR__ . '/src/Exception/ServerException.php';
1516
include_once __DIR__ . '/src/Formatter.php';
1617
include_once __DIR__ . '/src/Formatter/Compact.php';
@@ -23,6 +24,9 @@
2324
include_once __DIR__ . '/src/Node.php';
2425
include_once __DIR__ . '/src/Node/Number.php';
2526
include_once __DIR__ . '/src/Parser.php';
27+
// include_once __DIR__ . '/src/example/Server.php';
28+
include_once __DIR__ . '/src/SourceMap/Base64VLQEncoder.php';
29+
include_once __DIR__ . '/src/SourceMap/SourceMapGenerator.php';
2630
include_once __DIR__ . '/src/Type.php';
2731
include_once __DIR__ . '/src/Util.php';
2832
include_once __DIR__ . '/src/Version.php';

src/Compiler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Compiler
6767

6868
const SOURCE_MAP_NONE = 0;
6969
const SOURCE_MAP_INLINE = 1;
70+
const SOURCE_MAP_FILE = 2;
7071

7172
/**
7273
* @var array
@@ -205,12 +206,14 @@ public function compile($code, $path = null)
205206
$sourceMapGenerator = new SourceMapGenerator($this->sourceMapOptions);
206207
}
207208
$out = $this->formatter->format($this->scope, $sourceMapGenerator);
208-
if($this->sourceMap && $this->sourceMap !== self::SOURCE_MAP_NONE) {
209+
if(!empty($out) && $this->sourceMap && $this->sourceMap !== self::SOURCE_MAP_NONE) {
209210
$sourceMap = $sourceMapGenerator->generateJson();
210211

211212
$sourceMapUrl = null;
212213
if($this->sourceMap == self::SOURCE_MAP_INLINE) {
213214
$sourceMapUrl = sprintf('data:application/json,%s', self::encodeURIComponent($sourceMap));
215+
} elseif ($this->sourceMap == self::SOURCE_MAP_FILE) {
216+
$sourceMapUrl = $sourceMapGenerator->saveMap($sourceMap);
214217
}
215218

216219
$out .= sprintf('/*# sourceMappingURL=%s */', $sourceMapUrl);

src/SourceMap/SourceMapGenerator.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Leafo\ScssPhp\SourceMap;
1010

11+
use Leafo\ScssPhp\Exception\CompilerException;
12+
1113
class SourceMapGenerator {
1214
/**
1315
* What version of source map does the generator generate?
@@ -94,6 +96,28 @@ public function addMapping($generatedLine, $generatedColumn, $originalLine, $ori
9496
$this->sources[$sourceFile] = $sourceFile;
9597
}
9698

99+
/**
100+
* Saves the source map to a file
101+
*
102+
* @param string $file The absolute path to a file
103+
* @param string $content The content to write
104+
* @throws Exception If the file could not be saved
105+
*/
106+
public function saveMap($content){
107+
$file = $this->options['sourceMapWriteTo'];
108+
$dir = dirname($file);
109+
// directory does not exist
110+
if( !is_dir($dir) ){
111+
// FIXME: create the dir automatically?
112+
throw new CompilerException(sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir));
113+
}
114+
// FIXME: proper saving, with dir write check!
115+
if(file_put_contents($file, $content) === false){
116+
throw new CompilerException(sprintf('Cannot save the source map to "%s"', $file));
117+
}
118+
return $this->options['sourceMapURL'];
119+
}
120+
97121
/**
98122
* Generates the JSON source map
99123
*

0 commit comments

Comments
 (0)