@@ -28,6 +28,7 @@ import 'node/render_result.dart';
28
28
import 'node/types.dart' ;
29
29
import 'node/value.dart' ;
30
30
import 'node/utils.dart' ;
31
+ import 'node/worker_threads.dart' ;
31
32
import 'parse/scss.dart' ;
32
33
import 'syntax.dart' ;
33
34
import 'value.dart' ;
@@ -68,56 +69,54 @@ void main() {
68
69
/// [render] : https://github.com/sass/node-sass#options
69
70
void _render (
70
71
RenderOptions options, void callback (JSError error, RenderResult result)) {
71
- if (options.fiber != null ) {
72
- options.fiber.call (allowInterop (() {
73
- try {
74
- callback (null , _renderSync (options));
75
- } catch (error) {
76
- callback (error as JSError , null );
77
- }
78
- return null ;
79
- })).run ();
80
- } else {
81
- _renderAsync (options).then ((result) {
82
- callback (null , result);
83
- }, onError: (Object error, StackTrace stackTrace) {
84
- if (error is SassException ) {
85
- callback (_wrapException (error), null );
86
- } else {
87
- callback (_newRenderError (error.toString (), status: 3 ), null );
88
- }
89
- });
90
- }
72
+ _renderAsync (options).then ((result) {
73
+ callback (null , result);
74
+ }, onError: (Object error, StackTrace stackTrace) {
75
+ if (error is SassException ) {
76
+ callback (_wrapException (error), null );
77
+ } else {
78
+ callback (_newRenderError (error.toString (), status: 3 ), null );
79
+ }
80
+ });
91
81
}
92
82
93
83
/// Converts Sass to CSS asynchronously.
94
84
Future <RenderResult > _renderAsync (RenderOptions options) async {
95
85
var start = DateTime .now ();
96
86
var file = options.file == null ? null : p.absolute (options.file);
97
87
CompileResult result;
98
- if (options.data != null ) {
99
- result = await compileStringAsync (options.data,
100
- nodeImporter: _parseImporter (options, start),
101
- functions: _parseFunctions (options, asynch: true ),
102
- syntax: isTruthy (options.indentedSyntax) ? Syntax .sass : null ,
103
- style: _parseOutputStyle (options.outputStyle),
104
- useSpaces: options.indentType != 'tab' ,
105
- indentWidth: _parseIndentWidth (options.indentWidth),
106
- lineFeed: _parseLineFeed (options.linefeed),
107
- url: options.file == null ? 'stdin' : p.toUri (file).toString (),
108
- sourceMap: _enableSourceMaps (options));
109
- } else if (options.file != null ) {
110
- result = await compileAsync (file,
111
- nodeImporter: _parseImporter (options, start),
112
- functions: _parseFunctions (options, asynch: true ),
113
- syntax: isTruthy (options.indentedSyntax) ? Syntax .sass : null ,
114
- style: _parseOutputStyle (options.outputStyle),
115
- useSpaces: options.indentType != 'tab' ,
116
- indentWidth: _parseIndentWidth (options.indentWidth),
117
- lineFeed: _parseLineFeed (options.linefeed),
118
- sourceMap: _enableSourceMaps (options));
88
+ if (isMainThread == true ) {
89
+ var worker = Worker (p.current, WorkerOptions (workerData: {options}));
90
+ worker.on ('message' , (CompileResult msg) => result = msg);
91
+ worker.on ('error' , (JSError error) {
92
+ jsThrow (_wrapException (error));
93
+ });
119
94
} else {
120
- throw ArgumentError ("Either options.data or options.file must be set." );
95
+ if (options.data != null ) {
96
+ result = await compileStringAsync (options.data,
97
+ nodeImporter: _parseImporter (options, start),
98
+ functions: _parseFunctions (options, asynch: true ),
99
+ syntax: isTruthy (options.indentedSyntax) ? Syntax .sass : null ,
100
+ style: _parseOutputStyle (options.outputStyle),
101
+ useSpaces: options.indentType != 'tab' ,
102
+ indentWidth: _parseIndentWidth (options.indentWidth),
103
+ lineFeed: _parseLineFeed (options.linefeed),
104
+ url: options.file == null ? 'stdin' : p.toUri (file).toString (),
105
+ sourceMap: _enableSourceMaps (options));
106
+ } else if (options.file != null ) {
107
+ result = await compileAsync (file,
108
+ nodeImporter: _parseImporter (options, start),
109
+ functions: _parseFunctions (options, asynch: true ),
110
+ syntax: isTruthy (options.indentedSyntax) ? Syntax .sass : null ,
111
+ style: _parseOutputStyle (options.outputStyle),
112
+ useSpaces: options.indentType != 'tab' ,
113
+ indentWidth: _parseIndentWidth (options.indentWidth),
114
+ lineFeed: _parseLineFeed (options.linefeed),
115
+ sourceMap: _enableSourceMaps (options));
116
+ } else {
117
+ throw ArgumentError ("Either options.data or options.file must be set." );
118
+ }
119
+ ParentPort .postMessage (result, PortOptions ());
121
120
}
122
121
123
122
return _newRenderResult (options, result, start);
0 commit comments