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