1- /*! sass.js - v0.8.2 (6df72b8 ) - built 2015-05-09
2- providing libsass 3.2.3 (3cf31ef )
3- via emscripten 1.31.3 (5ce6ee5 )
1+ /*! sass.js - v0.9.0 (59917af ) - built 2015-05-21
2+ providing libsass 3.2.4 (a6482aa )
3+ via emscripten 1.32.4 (ae2f801 )
44 */
55( function ( root , factory ) {
66 'use strict' ;
2222
2323 var noop = function ( ) { } ;
2424 var slice = [ ] . slice ;
25+ // defined upon first Sass.initialize() call
26+ var globalWorkerUrl ;
2527
26- var Sass = {
27- _worker : null ,
28- _callbacks : { } ,
28+ function Sass ( workerUrl ) {
29+ if ( ! workerUrl && ! globalWorkerUrl ) {
30+ throw new Error (
31+ 'Sass needs to be initialized with the URL of sass.worker.js - '
32+ + 'either via Sass.setWorkerUrl(url) or by new Sass(url)'
33+ ) ;
34+ }
35+
36+ if ( ! globalWorkerUrl ) {
37+ globalWorkerUrl = workerUrl ;
38+ }
39+
40+ // bind all functions
41+ // we're doing this because we used to have a single hard-wired instance that allowed
42+ // [].map(Sass.removeFile) and we need to maintain that for now (at least until 1.0.0)
43+ for ( var key in this ) {
44+ if ( typeof this [ key ] === 'function' ) {
45+ this [ key ] = this [ key ] . bind ( this ) ;
46+ }
47+ }
48+
49+ this . _callbacks = { } ;
50+ this . _worker = new Worker ( workerUrl || globalWorkerUrl ) ;
51+ this . _worker . addEventListener ( 'message' , this . _handleWorkerMessage , false ) ;
52+ }
53+
54+ // allow setting the workerUrl before the first Sass instance is initialized,
55+ // where registering the global workerUrl would've happened automatically
56+ Sass . setWorkerUrl = function ( workerUrl ) {
57+ globalWorkerUrl = workerUrl ;
58+ } ;
59+
60+ Sass . style = {
61+ nested : 0 ,
62+ expanded : 1 ,
63+ compact : 2 ,
64+ compressed : 3
65+ } ;
66+
67+ Sass . comments = {
68+ 'none' : 0 ,
69+ 'default' : 1
70+ } ;
71+
72+ Sass . prototype = {
73+ style : Sass . style ,
74+ comments : Sass . comments ,
2975
30- style : {
31- nested : 0 ,
32- expanded : 1 ,
33- compact : 2 ,
34- compressed : 3
76+ destroy : function ( ) {
77+ this . _worker && this . _worker . terminate ( ) ;
78+ this . _worker = null ;
79+ this . _callbacks = { } ;
80+ this . _importer = null ;
3581 } ,
36- comments : {
37- 'none' : 0 ,
38- 'default' : 1
82+
83+ _handleWorkerMessage : function ( event ) {
84+ if ( event . data . command ) {
85+ this [ event . data . command ] ( event . data . args ) ;
86+ }
87+
88+ this . _callbacks [ event . data . id ] && this . _callbacks [ event . data . id ] ( event . data . result ) ;
89+ delete this . _callbacks [ event . data . id ] ;
3990 } ,
4091
4192 _dispatch : function ( options , callback ) {
93+ if ( ! this . _worker ) {
94+ throw new Error ( 'Sass worker has been terminated' ) ;
95+ }
96+
4297 options . id = 'cb' + Date . now ( ) + Math . random ( ) ;
43- Sass . _callbacks [ options . id ] = callback ;
44- Sass . _worker . postMessage ( options ) ;
98+ this . _callbacks [ options . id ] = callback ;
99+ this . _worker . postMessage ( options ) ;
45100 } ,
46101
47102 _importerInit : function ( args ) {
48103 // importer API done callback pushing results
49104 // back to the worker
50105 var done = function done ( result ) {
51- Sass . _worker . postMessage ( {
106+ this . _worker . postMessage ( {
52107 command : '_importerFinish' ,
53108 args : [ result ]
54109 } ) ;
55- } ;
110+ } . bind ( this ) ;
56111
57112 try {
58- Sass . _importer ( args [ 0 ] , done ) ;
113+ this . _importer ( args [ 0 ] , done ) ;
59114 } catch ( e ) {
60115 done ( { error : e . message } ) ;
61116 throw e ;
68123 }
69124
70125 // callback is executed in the main EventLoop
71- Sass . _importer = importerCallback ;
126+ this . _importer = importerCallback ;
72127 // tell worker to activate importer callback
73- Sass . _worker . postMessage ( {
128+ this . _worker . postMessage ( {
74129 command : 'importer' ,
75130 args : [ Boolean ( importerCallback ) ]
76131 } ) ;
77132
78133 callback && callback ( ) ;
79134 } ,
80-
81- initialize : function ( workerUrl ) {
82- if ( Sass . _worker ) {
83- throw new Error ( 'Sass Worker is already initalized' ) ;
84- }
85-
86- Sass . _worker = new Worker ( workerUrl ) ;
87- Sass . _worker . addEventListener ( 'message' , function ( event ) {
88- if ( event . data . command ) {
89- Sass [ event . data . command ] ( event . data . args ) ;
90- }
91-
92- Sass . _callbacks [ event . data . id ] && Sass . _callbacks [ event . data . id ] ( event . data . result ) ;
93- delete Sass . _callbacks [ event . data . id ] ;
94- } , false ) ;
95- }
96135 } ;
97136
98137 var commands = 'writeFile readFile listFiles removeFile clearFiles lazyFiles preloadFiles options compile compileFile' ;
99138 commands . split ( ' ' ) . forEach ( function ( command ) {
100- Sass [ command ] = function ( ) {
139+ Sass . prototype [ command ] = function ( ) {
101140 var callback = slice . call ( arguments , - 1 ) [ 0 ] ;
102141 var args = slice . call ( arguments , 0 , - 1 ) ;
103142 if ( typeof callback !== 'function' ) {
104143 args . push ( callback ) ;
105144 callback = noop ;
106145 }
107146
108- Sass . _dispatch ( {
147+ this . _dispatch ( {
109148 command : command ,
110149 args : args
111150 } , callback ) ;
112151 } ;
113152 } ) ;
114153
154+ // automatically set the workerUrl in case we're loaded by a simple
155+ // <script src="path/to/sass.js"></script>
156+ // see https://github.com/medialize/sass.js/pull/32#issuecomment-103142214
157+ // we can only run this test in the browser,
158+ // so make sure we actually have a DOM to work with
159+ if ( typeof document !== 'undefined' && document . getElementsByTagName ) {
160+ // http://www.2ality.com/2014/05/current-script.html
161+ var currentScript = document . currentScript || ( function ( ) {
162+ var scripts = document . getElementsByTagName ( 'script' ) ;
163+ return scripts [ scripts . length - 1 ] ;
164+ } ) ( ) ;
165+
166+ var defaultWorkerUrl = currentScript && currentScript . src ;
167+ // make sure we're not running in some concatenated thing
168+ if ( defaultWorkerUrl && defaultWorkerUrl . slice ( - 8 ) === '/sass.js' ) {
169+ Sass . setWorkerUrl ( defaultWorkerUrl . slice ( 0 , - 8 ) + '/sass.worker.js' ) ;
170+ }
171+ }
172+
115173 return Sass ;
116174} ) ) ;
0 commit comments