LESS fix for StealJS

by ryanwheale

JavaScript

(function() {
	var lessText = '',
		lessFiles = [],
		$inlineStyles,
		importRegex = /@import(?:-(?:once|multiple))?\s+(?:["']|url\()(.+)(?:["']|\));?/i; // DO NOT USE THE /g MODIFIER!!!;
		
	function updateStyles( options ) {
		lessFiles.push( options );
		lessText += '\n' + options.text;
		
		// TODO: Replace with real javascript
		if(!$inlineStyles || !$inlineStyles.length) {
			$inlineStyles = can.$('<style id="less:application-styles" />').appendTo('head');
		}
		
		// Always reset the the "type", as the LESS engine requires "text/less"
		$inlineStyles.attr('type', 'text/less').text( lessText );
		less.refreshStyles();
		
		if( steal.isRhino ) {
			new (less.Parser)({
	            optimization: less.optimization
	        }).parse(lessText, function (e, root) {
				// Update the first LESS file to hold the full CSS text
				// The CSS builder will only find this one file and compile the CSS
				lessFiles[0].text = root.toCSS();
				lessFiles[0].buildType = "css";
			});
		}
	}
		
	steal({id: "./less_engine.js",ignore: true}, function(){
		if(steal.isRhino) {
			// Some monkey patching of the LESS AST
			// For production builds we NEVER want the parser to add paths to a url(),
			// the CSS postprocessor is doing that already.
			(function(tree) {
				var oldProto = tree.URL.prototype;
				tree.URL = function (val, paths) {
					if (val.data) {
						this.attrs = val;
					} else {
						this.value = val;
						this.paths = paths;
					}
				};
				tree.URL.prototype = oldProto;
			})(less.tree);
		}
		
		
		steal.type("less fn", function(options, success, error){
			if( importRegex.test(options.text) ) {
				// 1) parse for @imports, 
	            // 2) keep track of all files to import... update the file paths
	            // 3) remove @import statements from file
	            // 4) steal() each import
	            // 5) append the remaining text onto the end
	            
	            var pathParts =...