Support includePaths in the JS API (#162)

Closes #7
This commit is contained in:
Natalie Weizenbaum 2017-07-07 14:50:12 -07:00 committed by GitHub
parent f41df9f7bd
commit 74cb974455
7 changed files with 48 additions and 7 deletions

View File

@ -8,8 +8,8 @@
### Node JS API ### Node JS API
* Add support for `data`, `lineFeed`, `indentWidth`, and `indentType` options to * Add support for `data`, `includePaths`, `indentedSyntax`, `lineFeed`,
`render()` and `renderSync()`. `indentWidth`, and `indentType` options to `render()` and `renderSync()`.
### Dart API ### Dart API

View File

@ -79,12 +79,14 @@ RenderResult _doRender(RenderOptions options) {
} }
output = renderString(options.data, output = renderString(options.data,
loadPaths: options.includePaths,
indented: options.indentedSyntax ?? false, indented: options.indentedSyntax ?? false,
useSpaces: options.indentType != 'tab', useSpaces: options.indentType != 'tab',
indentWidth: _parseIndentWidth(options.indentWidth), indentWidth: _parseIndentWidth(options.indentWidth),
lineFeed: _parseLineFeed(options.linefeed)); lineFeed: _parseLineFeed(options.linefeed));
} else if (options.file != null) { } else if (options.file != null) {
output = render(options.file, output = render(options.file,
loadPaths: options.includePaths,
indented: options.indentedSyntax, indented: options.indentedSyntax,
useSpaces: options.indentType != 'tab', useSpaces: options.indentType != 'tab',
indentWidth: _parseIndentWidth(options.indentWidth), indentWidth: _parseIndentWidth(options.indentWidth),

View File

@ -9,6 +9,7 @@ import 'package:js/js.dart';
class RenderOptions { class RenderOptions {
external String get file; external String get file;
external String get data; external String get data;
external List<String> get includePaths;
external bool get indentedSyntax; external bool get indentedSyntax;
external String get indentType; external String get indentType;
external dynamic get indentWidth; external dynamic get indentWidth;
@ -17,6 +18,7 @@ class RenderOptions {
external factory RenderOptions( external factory RenderOptions(
{String file, {String file,
String data, String data,
List<String> includePaths,
bool indentedSyntax, bool indentedSyntax,
String indentType, String indentType,
indentWidth, indentWidth,

View File

@ -15,6 +15,7 @@ String render(String path,
{bool indented, {bool indented,
bool color: false, bool color: false,
SyncPackageResolver packageResolver, SyncPackageResolver packageResolver,
Iterable<String> loadPaths,
bool useSpaces: true, bool useSpaces: true,
int indentWidth, int indentWidth,
LineFeed lineFeed}) => LineFeed lineFeed}) =>
@ -22,6 +23,7 @@ String render(String path,
indented: indented ?? p.extension(path) == '.sass', indented: indented ?? p.extension(path) == '.sass',
color: color, color: color,
packageResolver: packageResolver, packageResolver: packageResolver,
loadPaths: loadPaths,
useSpaces: useSpaces, useSpaces: useSpaces,
indentWidth: indentWidth, indentWidth: indentWidth,
lineFeed: lineFeed, lineFeed: lineFeed,
@ -33,6 +35,7 @@ String renderString(String source,
{bool indented: false, {bool indented: false,
bool color: false, bool color: false,
SyncPackageResolver packageResolver, SyncPackageResolver packageResolver,
Iterable<String> loadPaths,
bool useSpaces: true, bool useSpaces: true,
int indentWidth, int indentWidth,
LineFeed lineFeed, LineFeed lineFeed,
@ -40,8 +43,8 @@ String renderString(String source,
var sassTree = indented var sassTree = indented
? new Stylesheet.parseSass(source, url: url, color: color) ? new Stylesheet.parseSass(source, url: url, color: color)
: new Stylesheet.parseScss(source, url: url, color: color); : new Stylesheet.parseScss(source, url: url, color: color);
var cssTree = var cssTree = evaluate(sassTree,
evaluate(sassTree, color: color, packageResolver: packageResolver); color: color, packageResolver: packageResolver, loadPaths: loadPaths);
return toCss(cssTree, return toCss(cssTree,
useSpaces: useSpaces, indentWidth: indentWidth, lineFeed: lineFeed); useSpaces: useSpaces, indentWidth: indentWidth, lineFeed: lineFeed);
} }

View File

@ -620,9 +620,12 @@ class _PerformVisitor
? _tryImportPath ? _tryImportPath
: _tryImportPathWithExtensions; : _tryImportPathWithExtensions;
if (import.span.file.url != null) {
var base = p.dirname(p.fromUri(import.span.file.url)); var base = p.dirname(p.fromUri(import.span.file.url));
var resolved = tryPath(p.join(base, path)); var resolved = tryPath(p.join(base, path));
if (resolved != null) return resolved; if (resolved != null) return resolved;
}
for (var loadPath in _loadPaths) { for (var loadPath in _loadPaths) {
var resolved = tryPath(p.join(loadPath, path)); var resolved = tryPath(p.join(loadPath, path));

View File

@ -15,6 +15,10 @@ Future<String> createTempDir() async => (await runHybridExpression(
Future writeTextFile(String path, String text) => runHybridExpression( Future writeTextFile(String path, String text) => runHybridExpression(
'new File(message[0]).writeAsString(message[1])', [path, text]); '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]. /// Recursively deletes the directoy at [path].
Future deleteDirectory(String path) => Future deleteDirectory(String path) =>
runHybridExpression('new Directory(message).delete(recursive: true)', path); runHybridExpression('new Directory(message).delete(recursive: true)', path);

View File

@ -79,6 +79,33 @@ a {
contains('Either options.data or options.file must be set.')); 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", () { test("can render the indented syntax", () {
expect( expect(
_renderSync( _renderSync(