Skip to content

Commit 35f26bc

Browse files
committed
Added sourcemaps support
1 parent b5c32d6 commit 35f26bc

File tree

12 files changed

+92
-28
lines changed

12 files changed

+92
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/test
12
/node_modules/
23
__*
34
~*

gulp-cssimport.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var collect = require("collect-stream");
99
var hh = require("http-https");
1010
var minimatch = require("minimatch");
1111
var phpfn = require("phpfn");
12+
var applySourceMap = require("vinyl-sourcemaps-apply");
13+
var MagicString = require("magic-string");
1214

1315
var PLUGIN_NAME = "gulp-cssimport";
1416
var readFile = pify(fs.readFile);
@@ -39,7 +41,6 @@ module.exports = function cssImport(options) {
3941
var cssCount = 0;
4042

4143
function fileContents(vinyl, encoding, callback) {
42-
// console.log('fileContents ' , vinyl.path);
4344

4445
if (!stream) {
4546
stream = this;
@@ -69,18 +70,21 @@ module.exports = function cssImport(options) {
6970
}
7071

7172
(function(index) {
73+
var result = {index: index, importPath: importPath};
7274
if (!isUrl(importPath)) {
7375
var importFile = path.resolve(path.dirname(vinyl.path), importPath);
74-
// console.log('importFile %s from %s' , importFile, vinyl.path);
75-
promises.push(readFile(importFile, "utf8").then(function(data) {
76-
return {index: index, importFile: importFile, data: data};
76+
promises.push(readFile(importFile, "utf8").then(function(contents) {
77+
result.importFile = importFile;
78+
result.contents = contents;
79+
return result;
7780
}));
7881
} else {
7982
promises[promises.length] = new Promise(function(resolve, reject) {
8083
var req = hh.request(importPath, function (res) {
8184
collect(res, function (err, data) {
8285
if (err) return reject(err);
83-
resolve({index: index, data: data.toString()});
86+
result.contents = data.toString();
87+
resolve(result);
8488
});
8589
});
8690
req.on("error", reject);
@@ -99,29 +103,53 @@ module.exports = function cssImport(options) {
99103
// Waiting promises.
100104
Promise.all(promises).then(function(results) {
101105
for (var i = 0; i < results.length; i++) {
102-
var item = results[i];
103-
// file[item.index] = item.data;
106+
var result = results[i];
104107
var vfile = new gutil.File({
105-
path: item.importFile,
106-
contents: new Buffer(item.data)
108+
path: result.importFile,
109+
contents: new Buffer(result.contents)
107110
});
108-
(function(item) {
111+
(function(result) {
109112
results[i] = pify(fileContents)(vfile, null).then(function(vfile) {
110-
return {index: item.index, data: vfile.contents.toString()};
113+
result.contents = vfile.contents.toString();
114+
return result;
111115
});
112-
})(item);
116+
})(result);
113117
}
114118
return Promise.all(results);
115119
})
116120
.then(function(results) {
121+
var iterator = function() {};
122+
if (vinyl.sourceMap) {
123+
var bundle = new MagicString.Bundle();
124+
iterator = function(file, result) {
125+
bundle.addSource({
126+
filename: result.importPath,
127+
content: new MagicString(result.contents)
128+
});
129+
};
130+
}
117131
for (var i = 0; i < results.length; i++) {
118-
var index = results[i].index;
119-
file[index] = results[i].data;
132+
var result = results[i];
133+
var index = result.index;
134+
var contents = result.contents;
135+
file[index] = contents;
136+
iterator(file, result);
120137
}
121138
vinyl.contents = new Buffer(file.join(""));
139+
if (vinyl.sourceMap) {
140+
var map = bundle.generateMap({
141+
file: vinyl.relative,
142+
includeContent: true,
143+
hires: true
144+
});
145+
applySourceMap(vinyl, map);
146+
}
122147
callback(null, vinyl);
123148
})
124-
.catch(callback);
149+
.catch(function(err) {
150+
console.log('err ' , err);
151+
callback(new gutil.PluginError(PLUGIN_NAME, err));
152+
});
125153
}
126154

127155
return through.obj(fileContents);

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020
"deep-extend": "^0.4.0",
2121
"gulp-util": "2.2.X",
2222
"http-https": "^1.0.0",
23+
"magic-string": "^0.10.2",
2324
"minimatch": "^2.0.8",
2425
"phpfn": "^1.0.0",
2526
"pify": "^2.3.0",
26-
"through2": "0.4.X"
27+
"through2": "0.4.X",
28+
"vinyl-sourcemaps-apply": "^0.2.1"
2729
},
2830
"devDependencies": {
2931
"gulp": "^3.9.0",
3032
"gulp-bump": "0.1.X",
3133
"gulp-eslint": "0.1.X",
3234
"gulp-load-plugins": "0.7.X",
35+
"gulp-sourcemaps": "^1.6.0",
3336
"minimist": "1.1.X",
3437
"tape": "^4.0.0"
3538
},

readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ TIPS AND TRICKS
7979
@import "b.css";
8080
```
8181
If you will do `gulp.src("*.css")` gulp will read `a.css` and `b.css`,
82-
and plugin also will try to read these files. It is double job.
82+
and plugin also will try to read these files. It is extra job.
8383
Do instead: `gulp.src("main.css")`
8484

8585
**Use filter option:**
@@ -137,5 +137,6 @@ CHANGELOG
137137
- added option 'matchPattern'
138138

139139
3.0 [28 Feb 2016]
140-
- Removed node streams support, now only gulp
141-
- Removed directory option
140+
- removed node streams support, now only gulp
141+
- removed directory option
142+
- added sourcemaps support

test/complete/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ var gulp = require("gulp");
77
var options = {
88
};
99

10-
test("Gulp complete", {timeout: 5000}, function (t) {
10+
test("Complete", {timeout: 5000}, function (t) {
1111
var result = fs.readFileSync("result.css", { encoding: "utf8" });
1212
var stream = gulp.src("design/style.css")
13-
.pipe(plugin(options))
14-
.pipe(gulp.dest("/dev/null"));
13+
.pipe(plugin(options));
1514
collect(stream, function (err, data) {
1615
var file = data[0];
1716
data = file.contents.toString();

test/options-matchpattern/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ var options = {
88
matchPattern: '*.css',
99
};
1010

11-
test("Gulp parent", function(t) {
11+
test("Parent", function(t) {
1212
var stream;
1313
t.plan(1);
1414
var result = fs.readFileSync("result.css", {
1515
encoding: "utf8"
1616
});
1717
stream = gulp.src("design/style/css/main/style.css")
18-
.pipe(plugin(options))
19-
.pipe(gulp.dest("/dev/null"));
18+
.pipe(plugin(options));
2019
collect(stream, function(err, data) {
2120
var file = data[0];
2221
data = file.contents.toString();

test/parent/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ var gulp = require("gulp");
66

77
var options = { matchPattern: '*.css' };
88

9-
test("Gulp parent", function (t) {
9+
test("Parent", function (t) {
1010
var stream;
1111
var result = fs.readFileSync("result.css", { encoding: "utf8" });
1212
stream = gulp.src("design/style/css/main/style.css")
13-
.pipe(plugin(options))
14-
.pipe(gulp.dest("/dev/null"));
13+
.pipe(plugin(options));
1514
collect(stream, function (err, data) {
1615
var file = data[0];
1716
data = file.contents.toString();

test/sourcemaps/a.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
font-color: red;
3+
}

test/sourcemaps/b.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.b {
2+
font-weight: bold;
3+
}

test/sourcemaps/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var test = require("tape");
2+
var fs = require("fs");
3+
var collect = require("collect-stream");
4+
var plugin = require("../..");
5+
var gulp = require("gulp");
6+
var sourcemaps = require("gulp-sourcemaps");
7+
8+
var options = {
9+
};
10+
11+
test("Sourcemaps", function (t) {
12+
// var result = fs.readFileSync("result.css", { encoding: "utf8" });
13+
var stream = gulp.src("style*.css")
14+
.pipe(sourcemaps.init())
15+
.pipe(plugin(options))
16+
.pipe(sourcemaps.write())
17+
.pipe(gulp.dest("./~dst"))
18+
collect(stream, function (err, vinyls) {
19+
data = vinyls[0].contents.toString();
20+
t.notEqual(data.indexOf('# sourceMappingURL'), -1);
21+
t.end();
22+
});
23+
24+
});

0 commit comments

Comments
 (0)