mirror of
https://github.com/danog/dart-sass.git
synced 2025-01-21 21:31:11 +01:00
Support compiling to node.js.
This commit is contained in:
parent
2289901df8
commit
d90b8e52ae
@ -2,14 +2,13 @@
|
||||
// MIT-style license that can be found in the LICENSE file or at
|
||||
// https://opensource.org/licenses/MIT.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:stack_trace/stack_trace.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
import 'package:sass/src/ast/sass.dart';
|
||||
import 'package:sass/src/exception.dart';
|
||||
import 'package:sass/src/io.dart';
|
||||
import 'package:sass/src/visitor/perform.dart';
|
||||
import 'package:sass/src/visitor/serialize.dart';
|
||||
|
||||
@ -22,13 +21,11 @@ void main(List<String> args) {
|
||||
allowed: ['expanded'],
|
||||
defaultsTo: 'expanded')
|
||||
..addFlag('color',
|
||||
abbr: 'c',
|
||||
help: 'Whether to emit terminal colors.',
|
||||
defaultsTo: stdout.hasTerminal && !Platform.isWindows)
|
||||
abbr: 'c', help: 'Whether to emit terminal colors.', defaultsTo: true)
|
||||
..addFlag('trace', help: 'Print full Dart stack traces for exceptions.')
|
||||
..addFlag('help',
|
||||
abbr: 'h', help: 'Print this usage information.', negatable: false);
|
||||
var options = argParser.parse(args);
|
||||
var options = argParser.parse(getArguments(args));
|
||||
|
||||
if (options['help']) {
|
||||
print("Compile Sass to CSS.\n");
|
||||
@ -39,7 +36,7 @@ void main(List<String> args) {
|
||||
|
||||
try {
|
||||
var file = options.rest.first;
|
||||
var contents = new File(file).readAsStringSync();
|
||||
var contents = readFile(file);
|
||||
var url = p.toUri(file);
|
||||
var sassTree = p.extension(file) == '.sass'
|
||||
? new Stylesheet.parseSass(contents, url: url)
|
||||
@ -52,7 +49,7 @@ void main(List<String> args) {
|
||||
|
||||
if (options['trace']) {
|
||||
stderr.writeln();
|
||||
stderr.write(new Trace.from(stackTrace));
|
||||
stderr.write(new Trace.from(stackTrace).toString());
|
||||
stderr.flush();
|
||||
}
|
||||
|
||||
|
7
lib/src/io.dart
Normal file
7
lib/src/io.dart
Normal file
@ -0,0 +1,7 @@
|
||||
// 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.
|
||||
|
||||
export 'io/interface.dart'
|
||||
if (dart.library.io) 'io/vm.dart'
|
||||
if (node) 'io/node.dart';
|
19
lib/src/io/interface.dart
Normal file
19
lib/src/io/interface.dart
Normal file
@ -0,0 +1,19 @@
|
||||
// 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.
|
||||
|
||||
class Stderr {
|
||||
void write(object) {}
|
||||
void writeln([object]) {}
|
||||
void flush() {}
|
||||
}
|
||||
|
||||
Stderr get stderr => null;
|
||||
|
||||
List<String> getArguments(List<String> mainArguments) => null;
|
||||
|
||||
String readFile(String path) => null;
|
||||
|
||||
bool fileExists(String path) => null;
|
||||
|
||||
void exit(int exitCode) {}
|
54
lib/src/io/node.dart
Normal file
54
lib/src/io/node.dart
Normal file
@ -0,0 +1,54 @@
|
||||
// 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';
|
||||
|
||||
@JS()
|
||||
class _FS {
|
||||
external String readFileSync(String path, String encoding);
|
||||
|
||||
external bool existsSync(String path);
|
||||
}
|
||||
|
||||
@JS()
|
||||
class _Stderr {
|
||||
external void write(String text);
|
||||
}
|
||||
|
||||
class Stderr {
|
||||
final _Stderr _stderr;
|
||||
|
||||
Stderr(this._stderr);
|
||||
|
||||
void write(object) => _stderr.write(object.toString());
|
||||
|
||||
void writeln([object]) {
|
||||
if (object != null) _stderr.write("$object\n");
|
||||
}
|
||||
|
||||
void flush() {}
|
||||
}
|
||||
|
||||
@JS("require")
|
||||
external _FS _require(String name);
|
||||
|
||||
final _fs = _require("fs");
|
||||
|
||||
String readFile(String path) => _fs.readFileSync(path, 'utf8');
|
||||
|
||||
bool fileExists(String path) => _fs.existsSync(path);
|
||||
|
||||
@JS("process.stderr")
|
||||
external _Stderr get _stderr;
|
||||
|
||||
final stderr = new Stderr(_stderr);
|
||||
|
||||
@JS("process.argv")
|
||||
external List<String> get _arguments;
|
||||
|
||||
List<String> getArguments(List<String> mainArguments) =>
|
||||
_arguments.skip(2).toList();
|
||||
|
||||
@JS("process.exit")
|
||||
external int exit(int exitCode);
|
15
lib/src/io/vm.dart
Normal file
15
lib/src/io/vm.dart
Normal file
@ -0,0 +1,15 @@
|
||||
// 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 'dart:io' as io;
|
||||
|
||||
io.Stdout get stderr => io.stderr;
|
||||
|
||||
List<String> getArguments(List<String> mainArguments) => mainArguments;
|
||||
|
||||
String readFile(String path) => new io.File(path).readAsStringSync();
|
||||
|
||||
bool fileExists(String path) => new io.File(path).existsSync();
|
||||
|
||||
void exit(int exitCode) => io.exit(exitCode);
|
@ -2,7 +2,6 @@
|
||||
// MIT-style license that can be found in the LICENSE file or at
|
||||
// https://opensource.org/licenses/MIT.
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:path/path.dart' as p;
|
||||
@ -17,6 +16,7 @@ import '../callable.dart';
|
||||
import '../environment.dart';
|
||||
import '../exception.dart';
|
||||
import '../extend/extender.dart';
|
||||
import '../io.dart';
|
||||
import '../utils.dart';
|
||||
import '../value.dart';
|
||||
import 'interface/statement.dart';
|
||||
@ -377,7 +377,7 @@ class PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
|
||||
}
|
||||
|
||||
return _importedFiles.putIfAbsent(path, () {
|
||||
var contents = new File(path).readAsStringSync();
|
||||
var contents = readFile(path);
|
||||
var url = p.toUri(path);
|
||||
return p.extension(path) == '.sass'
|
||||
? new Stylesheet.parseSass(contents, url: url)
|
||||
@ -390,8 +390,8 @@ class PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
|
||||
|
||||
String _tryImportPath(String path) {
|
||||
var partial = p.join(p.dirname(path), "_${p.basename(path)}");
|
||||
if (new File(partial).existsSync()) return partial;
|
||||
if (new File(path).existsSync()) return path;
|
||||
if (fileExists(partial)) return partial;
|
||||
if (fileExists(path)) return path;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,8 @@ dependencies:
|
||||
tuple: "^1.0.0"
|
||||
|
||||
dev_dependencies:
|
||||
test: "^0.12.0"
|
||||
grinder: "^0.8.0"
|
||||
dart_style: "^0.2.9"
|
||||
grinder: "^0.8.0"
|
||||
js: "^0.6.0"
|
||||
node_preamble: "^1.0.0"
|
||||
test: "^0.12.0"
|
||||
|
@ -2,14 +2,39 @@
|
||||
// MIT-style license that can be found in the LICENSE file or at
|
||||
// https://opensource.org/licenses/MIT.
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:grinder/grinder.dart';
|
||||
import 'package:node_preamble/preamble.dart' as preamble;
|
||||
|
||||
main(args) => grind(args);
|
||||
|
||||
@DefaultTask()
|
||||
@DefaultTask('Run the Dart formatter.')
|
||||
format() {
|
||||
Pub.run('dart_style',
|
||||
script: 'format',
|
||||
arguments: ['--overwrite']
|
||||
..addAll(existingSourceDirs.map((dir) => dir.path)));
|
||||
}
|
||||
|
||||
@Task('Build Dart snapshot.')
|
||||
snapshot() {
|
||||
_ensureBuild();
|
||||
Dart.run('bin/sass.dart', vmArgs: ['--snapshot=build/sass.dart.snapshot']);
|
||||
}
|
||||
|
||||
@Task('Compile to JS.')
|
||||
js() {
|
||||
_ensureBuild();
|
||||
var destination = new File('build/sass.dart.js');
|
||||
Dart2js.compile(new File('bin/sass.dart'),
|
||||
outFile: destination, extraArgs: ['-Dnode=true']);
|
||||
var text = destination.readAsStringSync();
|
||||
destination.writeAsStringSync("${preamble.getPreamble()}\n$text");
|
||||
}
|
||||
|
||||
/// Ensure that the `build/` directory exists.
|
||||
void _ensureBuild() {
|
||||
new Directory('build').createSync(recursive: true);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user