Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
/node/*/node_modules/node-sass/node_modules/jshint
/node/*/node_modules/node-sass/node_modules/mocha-lcov-reporter
/node/*/node_modules/node-sass/test
/node/*/node_modules/node-sass-binaries
/node/*/node_modules/node-sass-binaries
.sass-cache
140 changes: 99 additions & 41 deletions Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@
define(function (require, exports, module) {
"use strict";

var StatusBarUtil = require("StatusBarUtil");

// Load commonly used modules from Brackets
var _ = brackets.getModule("thirdparty/lodash"),
CodeInspection = brackets.getModule("language/CodeInspection"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
FileUtils = brackets.getModule("file/FileUtils"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
NodeDomain = brackets.getModule("utils/NodeDomain");
NodeDomain = brackets.getModule("utils/NodeDomain"),
ProjectManager = brackets.getModule("project/ProjectManager");

// Boilerplate to load NodeDomain
var _domainPath = ExtensionUtils.getModulePath(module, "node/1.1.4-3/SASSDomain"),
_nodeDomain = new NodeDomain("sass-v1.1.4-3", _domainPath);
var _domainPath = ExtensionUtils.getModulePath(module, "node/2.0.1/SASSDomain"),
_nodeDomain = new NodeDomain("sass-v2.0.1", _domainPath);

// Initialize temp folder on windows only
// This is to normalize windows paths instead of using Node's os.tmpdir()
Expand All @@ -53,6 +56,7 @@ define(function (require, exports, module) {
PREF_ENABLED = "enabled",
PREF_COMPILER = "compiler",
PREF_COMPASS = "compass",
PREF_TIMEOUT = "timeout",
PREF_OPTIONS = "options";

var extensionPrefs = PreferencesManager.getExtensionPrefs("sass"),
Expand All @@ -65,23 +69,34 @@ define(function (require, exports, module) {
sourceMapFilePath = prefs.outputSourceMapFile.fullPath;

// Output CSS file should be relative to the source map
json.file = PathUtils.makePathRelative(cssFilePath, sourceMapFilePath);
if (typeof prefs.options.sourceMap === "string") {
json.file = PathUtils.makePathRelative(cssFilePath, sourceMapFilePath);
}

// Replace backslashes in paths
json.sources = json.sources.map(function (source) {
return source.replace(/\\/g, "/");
});

// For some reason, sources are output relative to the CWD
// Add a sourceRoot to fix
json.sourceRoot = PathUtils.makePathRelative(inputFile.parentPath, sourceMapFilePath);
// json.sourceRoot = PathUtils.makePathRelative(inputFile.parentPath, sourceMapFilePath);

// TODO read tab/space preference?
return JSON.stringify(json, null, " ");
}

function _makeSourceMapRelativeToOutput(prefs) {
var sourceMapPath = prefs.outputSourceMapFile.fullPath,
cssFilePath = prefs.outputCSSFile.fullPath;
if (typeof prefs.options.sourceMap === "string") {
var sourceMapPath = prefs.outputSourceMapFile.fullPath,
cssFilePath = prefs.outputCSSFile.fullPath;

// sourceMap should be relative to the output file
// This is only used when generating sourceMappingURL
return PathUtils.makePathRelative(sourceMapPath, cssFilePath);
// sourceMap should be relative to the output file
// This is only used when generating sourceMappingURL
return PathUtils.makePathRelative(sourceMapPath, cssFilePath);
}

return prefs.options.sourceMap;
}

function _render(path, prefs) {
Expand All @@ -90,17 +105,18 @@ define(function (require, exports, module) {
sourceMap = _makeSourceMapRelativeToOutput(prefs);

var renderPromise = _nodeDomain.exec("render",
path,
options.includePaths,
options.imagePath,
options.outputStyle,
options.sourceComments,
sourceMap,
prefs.compiler,
prefs.compass);

path,
prefs.outputCSSFile.fullPath,
options.includePaths,
options.imagePath,
options.outputStyle,
options.sourceComments,
sourceMap,
prefs.compiler,
prefs.compass);

renderPromise.then(function (response) {
deferred.resolve(response.css, _fixSourceMap(response.map, prefs));
deferred.resolve(response.css, _fixSourceMap(response.map, prefs), response.error, response._compassOutFile);
}, deferred.reject);

return deferred.promise();
Expand All @@ -115,6 +131,7 @@ define(function (require, exports, module) {
outputName = (options && options.output) || file.name.replace(RE_FILE_EXT, ".css"),
outputDir = (options && options.outputDir),
parentPath = file.parentPath,
sourceMapPath,
outputFile;

if (outputDir) {
Expand All @@ -134,20 +151,25 @@ define(function (require, exports, module) {
options = _.defaults(options || {}, {
outputStyle: "nested",
sourceComments: true,
sourceMap: outputFile.name + ".map"
sourceMap: true
});

// Initialize sourceMap with full path
options.sourceMap = outputFile.parentPath + options.sourceMap;
if (typeof options.sourceMap === "string") {
options.sourceMap = outputFile.parentPath + options.sourceMap;
sourceMapPath = options.sourceMap;
} else {
sourceMapPath = outputFile.parentPath + outputFile.name + ".map";
}

return {
enabled: enabled,
compiler: compiler,
compass: compass,
compass: compass ? { projectRoot: ProjectManager.getProjectRoot().fullPath } : false,
options: options,
inputFile: file,
outputCSSFile: outputFile,
outputSourceMapFile: FileSystem.getFileForPath(options.sourceMap)
outputSourceMapFile: FileSystem.getFileForPath(sourceMapPath)
};
}

Expand Down Expand Up @@ -215,9 +237,13 @@ define(function (require, exports, module) {
}

_.each(errors, function (err) {
if (err && err.path) {
err.path = FileUtils.convertWindowsPathToUnixPath(err.path);
}

if (typeof err === "string") {
err = {
message: "Runtime error: " + err,
message: err,
pos: {
line: -1
}
Expand All @@ -228,12 +254,8 @@ define(function (require, exports, module) {
var clonedError = _.clone(err);
clonedError.pos = _.clone(err.pos);

// FIXME determine when to add underscore prefix to partials
// HACK libsass errors on partials don't include the file extension!
clonedError.path += sassFileExtension;

partialErrorMap[clonedError.path] = partialErrorMap[clonedError.path] || [];
partialErrorMap[clonedError.path].push(clonedError);
partialErrorMap[err.path] = partialErrorMap[err.path] || [];
partialErrorMap[err.path].push(clonedError);

// Omit position if the file path doesn't match
err.pos.line = undefined;
Expand All @@ -249,9 +271,14 @@ define(function (require, exports, module) {
scanDeferred.resolve(result);

// Resolve promises for partials
_.each(partialErrorMap, function (partialErrors, partialPath) {
_deferredForScannedPath(partialPath).resolve({
errors: partialErrors,
_.each(scannedFileMap, function (deferred, partialPath) {
// Only deal with pending files
if (deferred.state() !== "pending") {
return;
}

deferred.resolve({
errors: partialErrorMap[partialPath] || [],
aborted: false
});
});
Expand All @@ -274,7 +301,16 @@ define(function (require, exports, module) {

renderPromise = _render(sassFile.fullPath, prefs);

return renderPromise.then(function (css, map) {
StatusBarUtil.showBusyStatus("Compiling " + PathUtils.makePathRelative(sassFile.fullPath, ProjectManager.getProjectRoot().fullPath));

return renderPromise.then(function (css, map, error, _compassOutFile) {
// HACK deal with compass output
if (_compassOutFile) {
_compassOutFile = FileUtils.convertWindowsPathToUnixPath(_compassOutFile);
cssFile = FileSystem.getFileForPath(_compassOutFile);
mapFile = FileSystem.getFileForPath(_compassOutFile + ".map");
}

var eventData = {
css: {
file: cssFile,
Expand All @@ -288,7 +324,6 @@ define(function (require, exports, module) {

if (map) {
_mkdirp(mapFile.parentPath).done(function () {
// TODO relative paths in sourceMap?
FileUtils.writeText(mapFile, map, true);

eventData.sourceMap = {
Expand All @@ -298,26 +333,39 @@ define(function (require, exports, module) {
});
}

_finishScan(sassFile);
_finishScan(sassFile, error);
}, function (errors) {
_finishScan(sassFile, errors);
}).always(function () {
StatusBarUtil.hideBusyStatus();
});
}

function preview(sassFile, docs) {
var deferred = new $.Deferred(),
prefs = _getPreferencesForFile(sassFile),
cssFile = prefs.outputCSSFile,
prefs = _getPreferencesForFile(sassFile);

// TODO warnings for compass that live preview isn't supported yet
// TODO support compiler errors with compass
// Requires changes to config.rb?
if (prefs.compass) {
_finishScan(sassFile, []);
return deferred.resolve().promise();
}

var cssFile = prefs.outputCSSFile,
mapFile = prefs.outputSourceMapFile,
sourceMap = _makeSourceMapRelativeToOutput(prefs),
options = prefs.options,
previewPromise,
inMemoryFiles = _getInMemoryFiles(docs);
inMemoryFiles = _getInMemoryFiles(docs),
compass = prefs.compass;

$(exports).triggerHandler("sourceMapPreviewStart", [sassFile, cssFile]);

previewPromise = _nodeDomain.exec("preview",
sassFile.fullPath,
prefs.outputCSSFile.fullPath,
inMemoryFiles,
options.includePaths,
options.imagePath,
Expand All @@ -327,6 +375,8 @@ define(function (require, exports, module) {
prefs.compiler,
prefs.compass);

StatusBarUtil.showBusyStatus("Checking for errors");

previewPromise.then(function (response) {
var eventData = {
css: {
Expand All @@ -340,14 +390,16 @@ define(function (require, exports, module) {
};

$(exports).triggerHandler("sourceMapPreviewEnd", [sassFile, eventData]);
_finishScan(sassFile);
_finishScan(sassFile, response.error);

deferred.resolve(response.css, response.map);
}, function (errors) {
$(exports).triggerHandler("sourceMapPreviewError", [sassFile, cssFile, errors]);
_finishScan(sassFile, errors);

deferred.reject(errors);
}).always(function () {
StatusBarUtil.hideBusyStatus();
});

return deferred.promise();
Expand All @@ -360,6 +412,7 @@ define(function (require, exports, module) {
function _prefChangeHandler(event) {
// TODO compile all files?
// _compileWithPreferences();
_nodeDomain.exec("setCompilerTimeout", extensionPrefs.get(PREF_TIMEOUT));
}

// Register preferences
Expand All @@ -375,6 +428,11 @@ define(function (require, exports, module) {
extensionPrefs.definePreference(PREF_COMPASS, "boolean", false)
.on("change", _prefChangeHandler);

extensionPrefs.definePreference(PREF_TIMEOUT, "number", -1)
.on("change", _prefChangeHandler);

_prefChangeHandler();

// Public API
exports.compile = compile;
exports.preview = preview;
Expand Down
9 changes: 6 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ module.exports = function (grunt) {
'node/*/SASSDomain.js',
'!node/*/node_modules/node-sass-binaries/**',
'node/*/node_modules/fs-extra/**',
'node/*/node_modules/node-sass/{package.json,sass.js,binding.gyp}',
'node/*/node_modules/node-sass/bin/**',
'node/*/node_modules/node-sass/node_modules/object-assign/**'
'node/*/node_modules/compass-options/**',
'node/*/node_modules/node-sass/{package.json,binding.gyp}',
'node/*/node_modules/node-sass/bin/node-sass',
'node/*/node_modules/node-sass/node_modules/semver/**',
'node/*/node_modules/node-sass/lib/**',
'node/*/node_modules/node-sass/vendor/**'
]
}
]
Expand Down
4 changes: 0 additions & 4 deletions SASSAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ define(function (require, exports, module) {
var server,
mapSourceURLs = {};

var previewDebounce = _.debounce(function (root, inMemoryFiles) {
Compiler.preview(root, inMemoryFiles);
}, 500);

function _setStatus(status, err) {
// HACK expose LiveDevelopment._setStatus()
LiveDevelopment.status = status;
Expand Down
55 changes: 55 additions & 0 deletions StatusBarUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2015 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4 */
/*global define: true, $: true, brackets: true*/

define(function (require, exports, module) {
"use strict";

var StringUtils = brackets.getModule("utils/StringUtils"),
StatusBar = brackets.getModule("widgets/StatusBar");

var STATUS_INDICATOR_ID = "sass-status-indicator";

var StatusBarUtil = {

$statusIndicator: null,

showBusyStatus: function (statusText) {
StatusBar.showBusyIndicator();
if (!this.$statusIndicator) {
this.$statusIndicator = $("<div/>");
StatusBar.addIndicator(STATUS_INDICATOR_ID, this.$statusIndicator, false);
}
this.$statusIndicator.text(statusText);
StatusBar.updateIndicator(STATUS_INDICATOR_ID, true);
},
hideBusyStatus: function () {
StatusBar.hideBusyIndicator();
StatusBar.updateIndicator(STATUS_INDICATOR_ID, false);
}


};

return StatusBarUtil;
});
Loading