Add support for the --load-path flag (#251)

Partially addresses #105
This commit is contained in:
Natalie Weizenbaum 2018-03-11 20:39:00 -07:00 committed by GitHub
parent 07b02174e0
commit da32588af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 7 deletions

View File

@ -3,6 +3,9 @@
* Add support for importing an `_index.scss` or `_index.sass` file when
importing a directory.
* Add a `--load-path` command-line option (alias `-I`) for passing additional
paths to search for Sass files to import.
### Node JS API
* Import URLs passed to importers are no longer normalized. For example, if a

View File

@ -17,8 +17,16 @@ main(List<String> args) async {
var argParser = new ArgParser(allowTrailingOptions: true)
..addOption('precision', hide: true)
..addFlag('stdin', help: 'Read the stylesheet from stdin.')
..addOption('load-path',
abbr: 'I',
valueHelp: 'PATH',
help: 'A path to use when resolving imports.\n'
'May be passed multiple times.',
allowMultiple: true,
splitCommas: false)
..addOption('style',
abbr: 's',
valueHelp: 'NAME',
help: 'Output style.',
allowed: ['expanded', 'compressed'],
defaultsTo: 'expanded')
@ -62,19 +70,23 @@ main(List<String> args) async {
var style = options['style'] == 'compressed'
? OutputStyle.compressed
: OutputStyle.expanded;
var loadPaths = options['load-path'] as List<String>;
var asynchronous = options['async'] as bool;
try {
String css;
if (stdinFlag) {
css = await _compileStdin(style: style, asynchronous: asynchronous);
css = await _compileStdin(
style: style, loadPaths: loadPaths, asynchronous: asynchronous);
} else {
var input = options.rest.first;
if (input == '-') {
css = await _compileStdin(style: style, asynchronous: asynchronous);
css = await _compileStdin(
style: style, loadPaths: loadPaths, asynchronous: asynchronous);
} else if (asynchronous) {
css = await compileAsync(input, color: color, style: style);
css = await compileAsync(input,
color: color, style: style, loadPaths: loadPaths);
} else {
css = compile(input, color: color, style: style);
css = compile(input, color: color, style: style, loadPaths: loadPaths);
}
}
@ -138,14 +150,18 @@ Future<String> _loadVersion() async {
/// Compiles Sass from standard input and returns the result.
Future<String> _compileStdin(
{bool color: false, OutputStyle style, bool asynchronous: false}) async {
{bool color: false,
OutputStyle style,
List<String> loadPaths,
bool asynchronous: false}) async {
var text = await readStdin();
var importer = new FilesystemImporter('.');
if (asynchronous) {
return await compileStringAsync(text,
color: color, style: style, importer: importer);
color: color, style: style, importer: importer, loadPaths: loadPaths);
} else {
return compileString(text, color: color, style: style, importer: importer);
return compileString(text,
color: color, style: style, importer: importer, loadPaths: loadPaths);
}
}

View File

@ -48,6 +48,81 @@ void sharedTests(Future<TestProcess> runSass(Iterable<String> arguments)) {
await sass.shouldExit(0);
});
group("can import files", () {
test("relative to the entrypoint", () async {
await d.file("test.scss", "@import 'dir/test'").create();
await d.dir("dir", [d.file("test.scss", "a {b: 1 + 2}")]).create();
var sass = await runSass(["test.scss", "test.css"]);
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: 3;",
"}",
]));
await sass.shouldExit(0);
});
test("from the load path", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.dir("dir", [d.file("test2.scss", "a {b: c}")]).create();
var sass = await runSass(["--load-path", "dir", "test.scss", "test.css"]);
expect(
sass.stdout,
emitsInOrder([
"a {",
" b: c;",
"}",
]));
await sass.shouldExit(0);
});
test("relative in preference to from the load path", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.file("test2.scss", "x {y: z}").create();
await d.dir("dir", [d.file("test2.scss", "a {b: c}")]).create();
var sass = await runSass(["--load-path", "dir", "test.scss", "test.css"]);
expect(
sass.stdout,
emitsInOrder([
"x {",
" y: z;",
"}",
]));
await sass.shouldExit(0);
});
test("in load path order", () async {
await d.file("test.scss", "@import 'test2'").create();
await d.dir("dir1", [d.file("test2.scss", "a {b: c}")]).create();
await d.dir("dir2", [d.file("test2.scss", "x {y: z}")]).create();
var sass = await runSass([
"--load-path",
"dir2",
"--load-path",
"dir1",
"test.scss",
"test.css"
]);
expect(
sass.stdout,
emitsInOrder([
"x {",
" y: z;",
"}",
]));
await sass.shouldExit(0);
});
});
test("compiles from stdin with --stdin", () async {
var sass = await runSass(["--stdin"]);
sass.stdin.writeln("a {b: 1 + 2}");