-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove temp files and scratch space in favor of an Importer (#37)
Add BuildImporter class to handle canonicalization and reading of imports. Add a check for `.sass` extension on inputId and use indented syntax.
- Loading branch information
Showing
7 changed files
with
141 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:build/build.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:sass/sass.dart' as sass; | ||
|
||
/// A [sass.AsyncImporter] for use during a [BuildStep] that supports Dart | ||
/// package imports of Sass files. | ||
/// | ||
/// All methods are heavily inspired by functions from for import priorities: | ||
/// https://github.com/sass/dart-sass/blob/f8b2c9111c1d5a3c07c9c8c0828b92bd87c548c9/lib/src/importer/utils.dart | ||
class BuildImporter extends sass.AsyncImporter { | ||
final BuildStep _buildStep; | ||
|
||
BuildImporter(this._buildStep); | ||
|
||
@override | ||
FutureOr<Uri> canonicalize(Uri url) async => | ||
(await _resolveImport(url.toString()))?.uri; | ||
|
||
@override | ||
FutureOr<sass.ImporterResult> load(Uri url) async { | ||
final id = new AssetId.resolve(url.toString(), from: _buildStep.inputId); | ||
final sourceMapId = id.addExtension('.map'); | ||
return new sass.ImporterResult( | ||
await _buildStep.readAsString(id), | ||
sourceMapUrl: sourceMapId.uri, | ||
indented: id.extension == '.sass', | ||
); | ||
} | ||
|
||
/// Resolves [import] using the same logic as the filesystem importer. | ||
/// | ||
/// This tries to fill in extensions and partial prefixes and check if a | ||
/// directory default. If no file can be found, it returns `null`. | ||
Future<AssetId> _resolveImport(String import) async { | ||
final extension = p.extension(import); | ||
if (extension == '.sass' || extension == '.scss') { | ||
return _exactlyOne(await _tryImport(import)); | ||
} | ||
|
||
return _exactlyOne(await _tryImportWithExtensions(import)) ?? | ||
_tryImportAsDirectory(import); | ||
} | ||
|
||
/// Like [_tryImport], but checks both `.sass` and `.scss` extensions. | ||
Future<List<AssetId>> _tryImportWithExtensions(String import) async => | ||
await _tryImport(import + '.sass') + await _tryImport(import + '.scss'); | ||
|
||
/// Returns the [AssetId] for [import] and/or the partial with the same name, | ||
/// if either or both exists. | ||
/// | ||
/// If neither exists, returns an empty list. | ||
Future<List<AssetId>> _tryImport(String import) async { | ||
final imports = <AssetId>[]; | ||
final partialId = new AssetId.resolve( | ||
p.join(p.dirname(import), '_${p.basename(import)}'), | ||
from: _buildStep.inputId); | ||
if (await _buildStep.canRead(partialId)) imports.add(partialId); | ||
final importId = new AssetId.resolve(import, from: _buildStep.inputId); | ||
if (await _buildStep.canRead(importId)) imports.add(importId); | ||
return imports; | ||
} | ||
|
||
/// Returns the resolved index file for [import] if [import] is a directory | ||
/// and the index file exists. | ||
/// | ||
/// Otherwise, returns `null`. | ||
Future<AssetId> _tryImportAsDirectory(String import) async => | ||
_exactlyOne(await _tryImportWithExtensions(p.join(import, 'index'))); | ||
|
||
/// If [imports] contains exactly one import [AssetId], returns that import. | ||
/// | ||
/// If it contains no assets, returns `null`. If it contains more than one, | ||
/// throws an exception. | ||
AssetId _exactlyOne(List<AssetId> imports) { | ||
if (imports.isEmpty) return null; | ||
if (imports.length == 1) return imports.first; | ||
|
||
throw new FormatException('It is not clear which file to import. Found:\n' + | ||
imports.map((import) => ' ${p.prettyUri(import.uri)}').join('\n')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: sass_builder | ||
version: 2.0.0 | ||
version: 2.0.1 | ||
authors: | ||
- Luis Vargas <[email protected]> | ||
- Kevin Moore <[email protected]> | ||
|
@@ -11,11 +11,8 @@ environment: | |
dependencies: | ||
build: ^0.12.5 | ||
build_config: ^0.2.6 | ||
logging: ^0.11.3 | ||
package_resolver: ^1.0.2 | ||
path: ^1.4.1 | ||
sass: ^1.0.0 | ||
scratch_space: ^0.0.1 | ||
sass: ^1.5.0 | ||
dev_dependencies: | ||
build_test: '>=0.9.1 <0.11.0' | ||
build_test: ^0.10.2 | ||
test: ^0.12.0 |
Oops, something went wrong.