Skip to content

Commit ca7bad9

Browse files
author
Alexander Svensson
committed
First commit
1 parent aa93fb5 commit ca7bad9

File tree

16 files changed

+196
-0
lines changed

16 files changed

+196
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
npm-debug.log
3+
tmp

index.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'use strict';
2+
var gutil = require('gulp-util');
3+
var through = require('through2');
4+
var glob = require('glob');
5+
var _ = require('lodash');
6+
var PluginError = gutil.PluginError;
7+
8+
var PLUGIN_NAME = 'gulp-cdr-inject';
9+
10+
var searchPattern = /<!---?\s*inject:\s+(.*)\s*-?--\s*>\n/gi;
11+
var templates = {
12+
js: '<script src="{file}"></script>',
13+
css: '<link href="{file}" rel="stylesheet" type="text/css" />'
14+
};
15+
var includedFiles = null;
16+
17+
function processPatterns(patterns, process) {
18+
var result = [];
19+
_.flatten(patterns).forEach(function (pattern) {
20+
var exclusion = pattern.indexOf('!') === 0;
21+
if (exclusion) {
22+
pattern = pattern.slice(1);
23+
}
24+
var matches = process(pattern);
25+
if (exclusion) {
26+
result = _.difference(result, matches);
27+
} else {
28+
result = _.union(result, matches);
29+
}
30+
});
31+
return result;
32+
}
33+
34+
function replacePattern(input, baseDir) {
35+
var option = JSON.parse('{' + input + '}');
36+
var tmpl = templates[option.type];
37+
var files = processPatterns(option.files, function (pattern) {
38+
return glob.sync(pattern, {
39+
cwd: baseDir
40+
});
41+
});
42+
43+
if (option.order) {
44+
var sorted = [];
45+
files.map(function (f, i) {
46+
return {
47+
index: i,
48+
value: f
49+
};
50+
}).sort(function (a, b) {
51+
var aName = a.value.split('/').pop();
52+
var bName = b.value.split('/').pop();
53+
var aIdx = option.order.indexOf(aName);
54+
var bIdx = option.order.indexOf(bName);
55+
return aIdx > bIdx ? 1 : -1;
56+
}).map(function (obj, i) {
57+
sorted[i] = obj.value;
58+
});
59+
files = sorted;
60+
}
61+
var out = '';
62+
files.forEach(function (f) {
63+
if (includedFiles.indexOf(f) === -1) {
64+
includedFiles.push(f);
65+
out += tmpl.replace('{file}', f) + '\n';
66+
}
67+
});
68+
return out;
69+
}
70+
71+
var cdrInject = function (baseDir) {
72+
baseDir = baseDir || './';
73+
74+
return through.obj(function (file, enc, cb) {
75+
if (file.isNull()) {
76+
this.push(file);
77+
return cb();
78+
}
79+
80+
if (file.isStream()) {
81+
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
82+
return cb();
83+
}
84+
85+
if (file.isBuffer()) {
86+
includedFiles = [];
87+
try {
88+
file.contents = new Buffer(file.contents.toString().replace(searchPattern, function (match, p0) {
89+
return replacePattern(p0, baseDir);
90+
}));
91+
} catch (err) {
92+
this.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
93+
}
94+
95+
this.push(file);
96+
return cb();
97+
}
98+
99+
this.push(file);
100+
cb();
101+
});
102+
};
103+
104+
module.exports = cdrInject;

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "gulp-cdr-inject",
3+
"version": "0.1.0",
4+
"description": "gulp-cdr-inject",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git://github.com/coder-se/gulp-cdr-inject.git"
15+
},
16+
"author": "coder.se <[email protected]>",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/coder-se/gulp-cdr-inject/issues"
20+
},
21+
"homepage": "https://github.com/coder-se/gulp-cdr-inject",
22+
"dependencies": {
23+
"gulp": "^3.6.1",
24+
"gulp-util": "^2.2.14",
25+
"lodash": "^2.4.1",
26+
"through2": "^0.4.1",
27+
"glob": "^3.2.9"
28+
},
29+
"devDependencies": {
30+
"should": "^3.3.1",
31+
"mocha": "^1.18.2"
32+
}
33+
}

test/.DS_Store

6 KB
Binary file not shown.

test/expected/default_options.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
<link href="styles/head.css" rel="stylesheet" type="text/css" />
7+
</head>
8+
<body>
9+
<script src="scripts/vendor/modernizr.js"></script>
10+
<script src="scripts/vendor/jQuery.js"></script>
11+
<script src="scripts/main.js"></script>
12+
<script src="scripts/module.js"></script>
13+
<link href="styles/vendor/vendor.css" rel="stylesheet" type="text/css" />
14+
<link href="styles/footer.css" rel="stylesheet" type="text/css" />
15+
</body>
16+
</html>

test/fixtures/scripts/exclude.js

Whitespace-only changes.

test/fixtures/scripts/main.js

Whitespace-only changes.

test/fixtures/scripts/module.js

Whitespace-only changes.

test/fixtures/scripts/vendor/jQuery.js

Whitespace-only changes.

test/fixtures/scripts/vendor/modernizr.js

Whitespace-only changes.

0 commit comments

Comments
 (0)