dart-sass/lib/src/node.dart

51 lines
1.6 KiB
Dart
Raw Normal View History

2016-10-15 02:33:51 +02:00
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:js/js.dart';
import '../sass.dart';
2016-10-29 00:23:14 +02:00
import 'exception.dart';
2016-10-15 02:33:51 +02:00
import 'executable.dart' as executable;
import 'node/error.dart';
import 'node/exports.dart';
import 'node/options.dart';
import 'node/result.dart';
2016-10-15 02:33:51 +02:00
/// The entrypoint for Node.js.
///
/// This sets up exports that can be called from JS. These include a private
/// export that runs the normal `main()`, which is called from `package/sass.js`
2016-10-25 01:45:51 +02:00
/// to run the executable when installed from npm.
2016-10-15 02:33:51 +02:00
void main() {
exports.run_ = allowInterop(executable.main);
exports.render = allowInterop(_render);
2016-10-29 20:14:16 +02:00
exports.info =
"dart-sass\t${const String.fromEnvironment('version')}\t(Sass Compiler)\t"
"[Dart]\n"
"dart2js\t${const String.fromEnvironment('dart-version')}\t"
"(Dart Compiler)\t[Dart]";
}
/// Converts Sass to CSS.
///
/// This attempts to match the [node-sass `render()` API][render] as closely as
/// possible.
///
/// [render]: https://github.com/sass/node-sass#options
NodeResult _render(NodeOptions options,
[void callback(NodeError error, NodeResult result)]) {
try {
var result = newNodeResult(render(options.file));
if (callback == null) return result;
callback(null, result);
2016-10-29 00:23:14 +02:00
} on SassException catch (error) {
// TODO: should this also be a NodeError?
if (callback == null) rethrow;
// TODO: populate the error more thoroughly if possible.
callback(new NodeError(message: error.message), null);
}
return null;
2016-10-15 02:33:51 +02:00
}