-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhaoluming
committed
May 10, 2016
1 parent
d44373a
commit 27494d0
Showing
2 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters