Skip to content

Commit

Permalink
Support includePaths in the JS API (#162)
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
nex3 authored Jul 7, 2017
1 parent f41df9f commit 74cb974
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

### Node JS API

* Add support for `data`, `lineFeed`, `indentWidth`, and `indentType` options to
`render()` and `renderSync()`.
* Add support for `data`, `includePaths`, `indentedSyntax`, `lineFeed`,
`indentWidth`, and `indentType` options to `render()` and `renderSync()`.

### Dart API

Expand Down
2 changes: 2 additions & 0 deletions lib/src/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ RenderResult _doRender(RenderOptions options) {
}

output = renderString(options.data,
loadPaths: options.includePaths,
indented: options.indentedSyntax ?? false,
useSpaces: options.indentType != 'tab',
indentWidth: _parseIndentWidth(options.indentWidth),
lineFeed: _parseLineFeed(options.linefeed));
} else if (options.file != null) {
output = render(options.file,
loadPaths: options.includePaths,
indented: options.indentedSyntax,
useSpaces: options.indentType != 'tab',
indentWidth: _parseIndentWidth(options.indentWidth),
Expand Down
2 changes: 2 additions & 0 deletions lib/src/node/render_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:js/js.dart';
class RenderOptions {
external String get file;
external String get data;
external List<String> get includePaths;
external bool get indentedSyntax;
external String get indentType;
external dynamic get indentWidth;
Expand All @@ -17,6 +18,7 @@ class RenderOptions {
external factory RenderOptions(
{String file,
String data,
List<String> includePaths,
bool indentedSyntax,
String indentType,
indentWidth,
Expand Down
7 changes: 5 additions & 2 deletions lib/src/render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ String render(String path,
{bool indented,
bool color: false,
SyncPackageResolver packageResolver,
Iterable<String> loadPaths,
bool useSpaces: true,
int indentWidth,
LineFeed lineFeed}) =>
renderString(readFile(path),
indented: indented ?? p.extension(path) == '.sass',
color: color,
packageResolver: packageResolver,
loadPaths: loadPaths,
useSpaces: useSpaces,
indentWidth: indentWidth,
lineFeed: lineFeed,
Expand All @@ -33,15 +35,16 @@ String renderString(String source,
{bool indented: false,
bool color: false,
SyncPackageResolver packageResolver,
Iterable<String> loadPaths,
bool useSpaces: true,
int indentWidth,
LineFeed lineFeed,
url}) {
var sassTree = indented
? new Stylesheet.parseSass(source, url: url, color: color)
: new Stylesheet.parseScss(source, url: url, color: color);
var cssTree =
evaluate(sassTree, color: color, packageResolver: packageResolver);
var cssTree = evaluate(sassTree,
color: color, packageResolver: packageResolver, loadPaths: loadPaths);
return toCss(cssTree,
useSpaces: useSpaces, indentWidth: indentWidth, lineFeed: lineFeed);
}
9 changes: 6 additions & 3 deletions lib/src/visitor/perform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,12 @@ class _PerformVisitor
? _tryImportPath
: _tryImportPathWithExtensions;

var base = p.dirname(p.fromUri(import.span.file.url));
var resolved = tryPath(p.join(base, path));
if (resolved != null) return resolved;
if (import.span.file.url != null) {
var base = p.dirname(p.fromUri(import.span.file.url));

var resolved = tryPath(p.join(base, path));
if (resolved != null) return resolved;
}

for (var loadPath in _loadPaths) {
var resolved = tryPath(p.join(loadPath, path));
Expand Down
4 changes: 4 additions & 0 deletions test/hybrid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Future<String> createTempDir() async => (await runHybridExpression(
Future writeTextFile(String path, String text) => runHybridExpression(
'new File(message[0]).writeAsString(message[1])', [path, text]);

/// Creates a directoy at [path].
Future createDirectory(String path) =>
runHybridExpression('new Directory(message).create()', path);

/// Recursively deletes the directoy at [path].
Future deleteDirectory(String path) =>
runHybridExpression('new Directory(message).delete(recursive: true)', path);
Expand Down
27 changes: 27 additions & 0 deletions test/node_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ a {
contains('Either options.data or options.file must be set.'));
});

test("supports load paths", () {
expect(
_renderSync(new RenderOptions(
data: "@import 'test'", includePaths: [sandbox])),
equals('''
a {
b: c;
}'''));
});

test("supports relative paths in preference to load paths", () async {
await createDirectory(p.join(sandbox, 'sub'));
var subPath = p.join(sandbox, 'sub/test.scss');
await writeTextFile(subPath, 'x {y: z}');

var importerPath = p.join(sandbox, 'importer.scss');
await writeTextFile(importerPath, '@import "test"');

expect(
_renderSync(new RenderOptions(
file: importerPath, includePaths: [p.join(sandbox, 'sub')])),
equals('''
a {
b: c;
}'''));
});

test("can render the indented syntax", () {
expect(
_renderSync(
Expand Down

0 comments on commit 74cb974

Please sign in to comment.