Skip to content

Commit

Permalink
add support of stylus,ejs,html
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoluming committed May 10, 2016
1 parent d44373a commit 27494d0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
60 changes: 57 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
var through = require("through2")
var gutil = require("gulp-util")
var minify = require('html-minifier').minify
var fs = require("fs")

module.exports = function (options) {
return through.obj(function (file, enc, cb) {
var contents = file.contents.toString(enc)
contents.replace(/require\((["'])([\.\w\d\/]+)\1\)/g, function(req, quote, url) {
fs.readFileSync(url, "utf8", callback)
var baseArr = file.base.split(/\/|\\/)
baseArr.pop()
var contents = file.contents.toString(enc).replace(/require\((["'])([\.\w\d\/]+)\1\)/g, function(req, quote, rawUrl) {
var urlArr = rawUrl.split("/")
var fileName = urlArr[urlArr.length-1]
if (fileName.split(".").length >= 2) {
var suffix = fileName.split(".").pop()
} else {
return req
}
var url
switch (urlArr[0]) {
case ".." :
baseArr.pop()
for (var i = 1; i < urlArr.length; i++) {
if (urlArr[i] == "..") {
baseArr.pop()
} else {
baseArr.push(urlArr[i])
}
}
url = baseArr.join("/")
break;
case "." :
urlArr.shift()
url = file.base + urlArr.join("/")
break;
default :
new gutil.PluginError({
plugin: 'module-bundler',
message: 'unsupported url'
});
}
gutil.log(url)
if (url) {
var text = fs.readFileSync(url, "utf8")
switch (suffix) {
case "html":
case "ejs":
text = minify(text, {collapseWhitespace:true})
break;
case "styl":
var stylus = require('stylus')
stylus.render(text, {compress: true}, function(err, css) {
text = css
})
break;
default:
return req
}
return "\"" + text.replace(/"/g, "\\\"") + "\""
} else {
return req
}
});
file.contents = new Buffer(contents, enc);
cb(null, file, enc)
});
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
"license": "MIT",
"dependencies": {
"gulp-util": "^3.0.7",
"html-minifier": "^2.1.2",
"through2": "^2.0.1"
},
"peerDependencies": {
"stylus": "*"
},
"devDependencies": {
"gulp": "^3.9.1",
"mocha": "^2.4.5"
Expand Down

0 comments on commit 27494d0

Please sign in to comment.