initial work to allow post process plugins to work with sourcemaps

This commit is contained in:
Luke Page
2014-09-19 14:45:41 +01:00
parent b721c4f44f
commit d9a6b46234
5 changed files with 35 additions and 17 deletions

View File

@@ -338,9 +338,9 @@ var parseLessFile = function (e, data) {
if (options.lint) {
options.sourceMap = false;
}
options.writeSourceMap = writeSourceMap;
options.sourceMapFilename = options.sourceMap;
options.sourceMapRootpath = options.sourceMapRootpath || "";
options.sourceMapFileInline = sourceMapFileInline;
if (options.sourceMap) {
// todo seperate options - no need to send them to less any more?
@@ -361,6 +361,10 @@ var parseLessFile = function (e, data) {
} else {
process.stdout.write(css);
}
if (options.sourceMap && !sourceMapFileInline) {
// TODO change to return object instead of css.. so much simpler and can also return metrics
writeSourceMap(options.sourceMap.getExternalSourceMap());
}
}
},
function(err) {

View File

@@ -35,7 +35,7 @@ ParseTree.prototype.toCSS = function(options) {
for(var i = 0; i < postProcessors.length; i++) {
// TODO - pass source maps
// TODO - async
css = postProcessors[i].process(css, options);
css = postProcessors[i].process(css, options, this.imports);
}
}
return css;

View File

@@ -5,10 +5,9 @@ module.exports = function (SourceMapOutput) {
};
SourceMapBuilder.prototype.toCSS = function(rootNode, options, imports) {
this.sourceMapOutput = new SourceMapOutput(
var sourceMapOutput = new SourceMapOutput(
{
contentsIgnoredCharsMap: imports.contentsIgnoredChars,
writeSourceMap: this.options.writeSourceMap,
rootNode: rootNode,
contentsMap: imports.contents,
sourceMapFilename: this.options.sourceMapFilename,
@@ -17,10 +16,28 @@ module.exports = function (SourceMapOutput) {
sourceMapBasepath: this.options.sourceMapBasepath,
sourceMapRootpath: this.options.sourceMapRootpath,
outputSourceFiles: this.options.outputSourceFiles,
sourceMapGenerator: this.options.sourceMapGenerator
sourceMapGenerator: this.options.sourceMapGenerator,
sourceMapFileInline: this.options.sourceMapFileInline
});
return this.sourceMapOutput.toCSS(options);
var css = sourceMapOutput.toCSS(options);
this.sourceMap = sourceMapOutput.sourceMap;
this.sourceMapURL = sourceMapOutput.sourceMapURL;
return css;
};
SourceMapBuilder.prototype.getExternalSourceMap = function() {
return this.sourceMap;
};
SourceMapBuilder.prototype.setExternalSourceMap = function(sourceMap) {
this.sourceMap = sourceMap;
};
SourceMapBuilder.prototype.isInline = function() {
return this.options.sourceMapFileInline;
};
SourceMapBuilder.prototype.getSourceMapURL = function() {
return this.sourceMapURL;
};
return SourceMapBuilder;

View File

@@ -3,18 +3,18 @@ module.exports = function (environment) {
var SourceMapOutput = function (options) {
this._css = [];
this._rootNode = options.rootNode;
this._writeSourceMap = options.writeSourceMap;
this._contentsMap = options.contentsMap;
this._contentsIgnoredCharsMap = options.contentsIgnoredCharsMap;
this._sourceMapFilename = options.sourceMapFilename;
this._outputFilename = options.outputFilename;
this._sourceMapURL = options.sourceMapURL;
this.sourceMapURL = options.sourceMapURL;
if (options.sourceMapBasepath) {
this._sourceMapBasepath = options.sourceMapBasepath.replace(/\\/g, '/');
}
this._sourceMapRootpath = options.sourceMapRootpath;
this._outputSourceFiles = options.outputSourceFiles;
this._sourceMapGeneratorConstructor = environment.getSourceMapGenerator();
this._sourceMapFileInline = options.sourceMapFileInline;
if (this._sourceMapRootpath && this._sourceMapRootpath.charAt(this._sourceMapRootpath.length-1) !== '/') {
this._sourceMapRootpath += '/';
@@ -118,14 +118,15 @@ module.exports = function (environment) {
var sourceMapURL,
sourceMapContent = JSON.stringify(this._sourceMapGenerator.toJSON());
if (this._sourceMapURL) {
sourceMapURL = this._sourceMapURL;
if (this.sourceMapURL) {
sourceMapURL = this.sourceMapURL;
} else if (this._sourceMapFilename) {
sourceMapURL = this.normalizeFilename(this._sourceMapFilename);
}
this.sourceMapURL = sourceMapURL;
if (this._writeSourceMap) {
this._writeSourceMap(sourceMapContent);
if (!this._sourceMapFileInline) {
this.sourceMap = sourceMapContent;
} else {
sourceMapURL = "data:application/json;base64," + environment.encodeBase64(sourceMapContent);
}

View File

@@ -110,10 +110,6 @@ module.exports = function() {
totalTests++;
if (options.sourceMap) {
var sourceMapOutput;
options.writeSourceMap = function(output) {
sourceMapOutput = output;
};
options.sourceMapOutputFilename = name + ".css";
options.sourceMapBasepath = path.join(process.cwd(), "test/less");
options.sourceMapRootpath = "testweb/";
@@ -128,7 +124,7 @@ module.exports = function() {
toCSS(options, path.join('test/less/', foldername + file), function (err, less) {
if (verifyFunction) {
return verifyFunction(name, err, less, doReplacements, sourceMapOutput);
return verifyFunction(name, err, less, doReplacements, options.sourceMap.getExternalSourceMap());
}
var css_name = name;
if(nameModifier) { css_name = nameModifier(name); }