@@ -28,6 +28,7 @@ import 'node/render_result.dart';
2828import 'node/types.dart' ;
2929import 'node/value.dart' ;
3030import 'node/utils.dart' ;
31+ import 'node/worker_threads.dart' ;
3132import 'parse/scss.dart' ;
3233import 'syntax.dart' ;
3334import 'value.dart' ;
@@ -68,56 +69,54 @@ void main() {
6869/// [render] : https://github.com/sass/node-sass#options
6970void _render (
7071 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+ });
9181}
9282
9383/// Converts Sass to CSS asynchronously.
9484Future <RenderResult > _renderAsync (RenderOptions options) async {
9585 var start = DateTime .now ();
9686 var file = options.file == null ? null : p.absolute (options.file);
9787 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+ });
11994 } 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 ());
121120 }
122121
123122 return _newRenderResult (options, result, start);
0 commit comments