Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

Commit

Permalink
Ensure that file property of an input source map always exists
Browse files Browse the repository at this point in the history
  • Loading branch information
nDmitry committed Dec 27, 2013
1 parent 87c6185 commit 3c2d71b
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions tasks/autoprefixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,24 @@ module.exports = function(grunt) {
}
});

function updateSources(sources, to) {
/**
* @param {string} map
* @param {string} fileValue
* @returns {string}
*/
function fixFile(map, filePath) {
map = JSON.parse(map);
map.file = path.basename(filePath);

return JSON.stringify(map);
}

/**
* @param {array} sources
* @param {string} to
* @returns {array}
*/
function fixSources(sources, to) {
sources.forEach(function(source, index) {

// Set the correct path to a source file
Expand All @@ -54,6 +71,19 @@ module.exports = function(grunt) {
return sources;
}

/**
* @param {string} mapPath
* @param {string} from
*/
function getMapParam(mapPath, from) {

if (grunt.file.exists(mapPath)) {
return fixFile(grunt.file.read(mapPath), from);
} else {
return true;
}
}

function compile(original, from, to) {
var result;

Expand All @@ -75,20 +105,17 @@ module.exports = function(grunt) {
from = path.basename(from);

result = processor.process(original, {
map: (grunt.file.exists(mapPath)) ? grunt.file.read(mapPath) : true,
map: getMapParam(mapPath, from),
from: from,
to: to
});

var map = JSON.parse(result.map);

// Set the correct name of the generated code
map.file = path.basename(to);
var map = JSON.parse(fixFile(result.map, to));

if (grunt.file.exists(mapPath)) {
map.sources = updateSources(JSON.parse(grunt.file.read(mapPath)).sources, to);
map.sources = fixSources(JSON.parse(grunt.file.read(mapPath)).sources, to);
} else {
updateSources(map.sources, to);
fixSources(map.sources, to);

// PostCSS doesn't add the annotation yet
result.css = result.css.concat(
Expand Down

0 comments on commit 3c2d71b

Please sign in to comment.