-
Notifications
You must be signed in to change notification settings - Fork 347
/
compile.dart
161 lines (148 loc) · 4.51 KB
/
compile.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2018 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.
// DO NOT EDIT. This file was generated from async_compile.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: ef27d750f1d5305373fe9e712cf99697a21e0689
//
// ignore_for_file: unused_import
import 'async_compile.dart';
export 'async_compile.dart';
import 'dart:convert';
import 'package:path/path.dart' as p;
import 'package:source_maps/source_maps.dart';
import 'package:source_span/source_span.dart';
import 'ast/sass.dart';
import 'import_cache.dart';
import 'callable.dart';
import 'importer.dart';
import 'importer/node.dart';
import 'io.dart';
import 'logger.dart';
import 'syntax.dart';
import 'utils.dart';
import 'visitor/evaluate.dart';
import 'visitor/serialize.dart';
/// Like [compile] in `lib/sass.dart`, but provides more options to support
/// the node-sass compatible API and the executable.
///
/// At most one of `importCache` and `nodeImporter` may be provided at once.
CompileResult compile(String path,
{Syntax syntax,
Logger logger,
ImportCache importCache,
NodeImporter nodeImporter,
Iterable<Callable> functions,
OutputStyle style,
bool useSpaces = true,
int indentWidth,
LineFeed lineFeed,
bool sourceMap = false,
bool charset = true}) {
// If the syntax is different than the importer would default to, we have to
// parse the file manually and we can't store it in the cache.
Stylesheet stylesheet;
if (nodeImporter == null &&
(syntax == null || syntax == Syntax.forPath(path))) {
importCache ??= ImportCache.none(logger: logger);
stylesheet = importCache.importCanonical(
FilesystemImporter('.'), p.toUri(canonicalize(path)), p.toUri(path));
} else {
stylesheet = Stylesheet.parse(
readFile(path), syntax ?? Syntax.forPath(path),
url: p.toUri(path), logger: logger);
}
return _compileStylesheet(
stylesheet,
logger,
importCache,
nodeImporter,
FilesystemImporter('.'),
functions,
style,
useSpaces,
indentWidth,
lineFeed,
sourceMap,
charset);
}
/// Like [compileString] in `lib/sass.dart`, but provides more options to
/// support the node-sass compatible API.
///
/// At most one of `importCache` and `nodeImporter` may be provided at once.
CompileResult compileString(String source,
{Syntax syntax,
Logger logger,
ImportCache importCache,
NodeImporter nodeImporter,
Iterable<Importer> importers,
Iterable<String> loadPaths,
Importer importer,
Iterable<Callable> functions,
OutputStyle style,
bool useSpaces = true,
int indentWidth,
LineFeed lineFeed,
Object url,
bool sourceMap = false,
bool charset = true}) {
var stylesheet =
Stylesheet.parse(source, syntax ?? Syntax.scss, url: url, logger: logger);
return _compileStylesheet(
stylesheet,
logger,
importCache,
nodeImporter,
importer ?? FilesystemImporter('.'),
functions,
style,
useSpaces,
indentWidth,
lineFeed,
sourceMap,
charset);
}
/// Compiles [stylesheet] and returns its result.
///
/// Arguments are handled as for [compileString].
CompileResult _compileStylesheet(
Stylesheet stylesheet,
Logger logger,
ImportCache importCache,
NodeImporter nodeImporter,
Importer importer,
Iterable<Callable> functions,
OutputStyle style,
bool useSpaces,
int indentWidth,
LineFeed lineFeed,
bool sourceMap,
bool charset) {
var evaluateResult = evaluate(stylesheet,
importCache: importCache,
nodeImporter: nodeImporter,
importer: importer,
functions: functions,
logger: logger,
sourceMap: sourceMap);
var serializeResult = serialize(evaluateResult.stylesheet,
style: style,
useSpaces: useSpaces,
indentWidth: indentWidth,
lineFeed: lineFeed,
sourceMap: sourceMap,
charset: charset);
if (serializeResult.sourceMap != null && importCache != null) {
// TODO(nweiz): Don't explicitly use a type parameter when dart-lang/sdk#25490
// is fixed.
mapInPlace<String>(
serializeResult.sourceMap.urls,
(url) => url == ''
? Uri.dataFromString(stylesheet.span.file.getText(0),
encoding: utf8)
.toString()
: importCache.sourceMapUrl(Uri.parse(url)).toString());
}
return CompileResult(evaluateResult, serializeResult);
}