Skip to content

Commit ce58e9a

Browse files
author
Ondřej Dušek
committed
initial commit; build alias support
1 parent 601a592 commit ce58e9a

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/.idea/

index.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
'use strict';
2+
3+
var path = require('path'),
4+
fs = require('fs'),
5+
es = require('event-stream'),
6+
gulp = require('gulp'),
7+
cheerio = require('cheerio'),
8+
sprintf = require('sprintf'),
9+
10+
/**
11+
* Zkontroluje pøístup k souboru a vyhodí chybu v pøípadì, že nelze pøeèíst.
12+
* @param file Název souboru
13+
*/
14+
validateFile = function (file) {
15+
fs.accessSync && fs.accessSync(file, fs.R_OK);
16+
if (fs.existsSync && !fs.existsSync(file)) {
17+
throw new Error(sprintf('ENOENT, no such file or directory \'%s\'', file));
18+
}
19+
},
20+
21+
/**
22+
* Zkonvertuje pole názvù souborù na zdrojový stream Gulpu
23+
* @param cwd Pracovní adresáø
24+
* @returns {*}
25+
*/
26+
gulpSources = function (cwd) {
27+
cwd = cwd || '';
28+
var pathStream = es.through(),
29+
fileStream = es.through();
30+
pathStream.pipe(es.writeArray(function (err, files) {
31+
files = files.map(function (file) {
32+
file = path.join(cwd, file);
33+
validateFile(file);
34+
return file;
35+
});
36+
gulp.src(files).pipe(fileStream);
37+
}));
38+
return es.duplex(pathStream, fileStream);
39+
},
40+
41+
/**
42+
* Builder funkce pro script tag
43+
* @param builder Builder funkce
44+
* @param minified Použít minifikovanou verzi sriptu
45+
* @returns {Function}
46+
*/
47+
scriptBuilder = function (builder, minified) {
48+
minified = minified !== false;
49+
var extractSrc = function (line) {
50+
var element = cheerio('script[src]', line) || cheerio('<script></script>');
51+
return minified && element.attr('data-build-src') || element.attr('data-abs-src') || element.attr('src');
52+
};
53+
return function (block) {
54+
var templateScript = function (path) {
55+
var element = cheerio('script', '<script></script>');
56+
element.attr('src', path);
57+
return block.indent + cheerio.html(element);
58+
},
59+
extractScriptStream = es.mapSync(extractSrc),
60+
templateScriptStream = es.mapSync(templateScript);
61+
block.pipe(extractScriptStream);
62+
templateScriptStream.pipe(block);
63+
builder(es.duplex(templateScriptStream, extractScriptStream));
64+
};
65+
},
66+
67+
/**
68+
* Builder funkce pro link tag
69+
* @param builder Builder funkce
70+
* @param minified Použít minifikovanou verzi sriptu
71+
* @returns {Function}
72+
*/
73+
stylesheetBuilder = function (builder, minified) {
74+
minified = minified !== false;
75+
var extractLink = function (line) {
76+
var element = cheerio('link[rel=stylesheet][href]', line) || cheerio('<link>');
77+
return minified && element.attr('data-build-href') || element.attr('data-href') || element.attr('href');
78+
};
79+
return function (block) {
80+
var templateLink = function (path) {
81+
var element = cheerio('link', '<link>');
82+
element.attr('rel', 'stylesheet');
83+
element.attr('href', path);
84+
return block.indent + cheerio.html(element);
85+
},
86+
extractLinkStream = es.mapSync(extractLink),
87+
templateLinkStream = es.mapSync(templateLink);
88+
block.pipe(extractLinkStream);
89+
templateLinkStream.pipe(block);
90+
builder(es.duplex(templateLinkStream, extractLinkStream));
91+
};
92+
};
93+
94+
module.exports = {
95+
/**
96+
* Builder pro vytvoøení streamu se seznamem všech skriptù
97+
* @param stream Výstupní stream
98+
* @param cwd Pracovní adresáø
99+
* @param minified Použít minifikované verze sriptù
100+
* @returns {Function}
101+
*/
102+
scriptStream: function (stream, cwd, minified) {
103+
return scriptBuilder(function (block) {
104+
block.pipe(gulpSources(cwd)).pipe(stream);
105+
}, minified);
106+
},
107+
108+
/**
109+
* Builder pro nahrazení sekce scriptù za souhrnný script tag
110+
* @param src Hodnota src atributu
111+
* @returns {Function}
112+
*/
113+
scriptReplace: function (src) {
114+
return scriptBuilder(function (block) {
115+
block.end(src);
116+
});
117+
},
118+
119+
/**
120+
* Builder pro vytvoøení streamu se seznamem všech souborù se styly
121+
* @param stream Výstupní stream
122+
* @param cwd Pracovní adresáø
123+
* @param minified Použít minifikované verze souborù se styly
124+
* @returns {Function}
125+
*/
126+
stylesheetStream: function (stream, cwd, minified) {
127+
return stylesheetBuilder(function (block) {
128+
block.pipe(gulpSources(cwd)).pipe(stream);
129+
}, minified);
130+
},
131+
132+
/**
133+
* Builder pro nahrazení sekce stylù za souhrnný script link
134+
* @param href Hodnota href atributu
135+
* @returns {Function}
136+
*/
137+
stylesheetReplace: function (href) {
138+
return stylesheetBuilder(function (block) {
139+
block.end(href);
140+
});
141+
}
142+
};

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "htmlbuild-functions",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/winsite/htmlbuild-functions.git"
9+
},
10+
"author": "Winsite a.s.",
11+
"license": "MIT",
12+
"bugs": {
13+
"url": "https://github.com/winsite/htmlbuild-functions/issues"
14+
},
15+
"homepage": "https://github.com/winsite/htmlbuild-functions#readme",
16+
"devDependencies": {
17+
"cheerio": "^0.19.0",
18+
"event-stream": "^3.3.1",
19+
"gulp": "^3.9.0",
20+
"sprintf": "^0.1.5"
21+
}
22+
}

0 commit comments

Comments
 (0)