_concat-scriptlink.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. module.exports = function (){
  2. var path = require('path')
  3. var fs = require('fs')
  4. var _ = require('../node_modules/grunt-contrib-compress/node_modules/archiver/node_modules/lodash')
  5. // config
  6. var ASSETS_ROOT = 'http://assets.dwstatic.com/'
  7. var HOOK_SCRIPT = 'data-script'
  8. var cwd = path.join(__dirname, '..')
  9. var source = path.join(cwd, 'assets', 'dest')
  10. var target = path.join(cwd, 'assets', 'dest')
  11. var sourceIgnore = ['.svn', 'css', 'img', 'js']
  12. var errMsg
  13. // main
  14. // 1. sync read *.html, get [htmls]
  15. // 2. iterator each html file, do
  16. // get [links] by HOOK_SCRIPT and clacURL(url)
  17. // generate insertScriptSnippet by [links]
  18. // delete HOOK_SCRIPT lines, replace firstHit by insertScriptSnippet
  19. var patternScriptTag = new RegExp('<script([^>]*'+HOOK_SCRIPT+'[^>]*)>(</script>)?', 'g')
  20. var patternScriptSrc = new RegExp('src=[\'\"]'+'http://assets\.dwstatic\.com/'+'([^\'\"]*)[\'\"]', 'g')
  21. var files = listFiles(source, sourceIgnore)
  22. _.each(files, function(file){
  23. var outputText = transHTML(file)
  24. var outputFile = path.resolve(target, path.relative(source, file))
  25. fs.writeFileSync(outputFile, outputText, 'utf8')
  26. })
  27. // if no need to transform html, still return original file
  28. function transHTML(html){
  29. var links = []
  30. var text = fs.readFileSync(html, 'utf8')
  31. while (scripts = patternScriptTag.exec(text)){
  32. while (match = patternScriptSrc.exec(scripts[1])){
  33. links.push(calcURL(match[1]))
  34. }
  35. }
  36. var firstScript = patternScriptTag.exec(text)
  37. if(firstScript){
  38. // calcURL each [links] item, and get scriptInsert
  39. var fnReduce = function(last, next){return last+','+next}
  40. var linkStr = ASSETS_ROOT+'f='+_.reduce(links, fnReduce)
  41. var scriptSnippet = '<script src="'+linkStr+'"></script>'
  42. return text.replace(firstScript[0], scriptSnippet).replace(patternScriptTag, '')
  43. }
  44. return text
  45. }
  46. // resolve to absolute path url
  47. // var URL_TAG_ROOT = 'b='
  48. // var URL_TAG_PATH = 'f='
  49. function calcURL(url){
  50. var indexRoot = url.indexOf('b=')
  51. var indexPath = url.indexOf('f=')
  52. if(~indexPath){
  53. var urls = url.substring(indexPath+2).split(',')
  54. if(~indexRoot){
  55. var root = url.substring(indexRoot+2, indexPath-1)
  56. var urls = _.map(urls, function(p){
  57. return path.join(root, p)
  58. })
  59. }
  60. var resolveURL = _.reduce(urls, function(last, next){
  61. return last+','+next
  62. })
  63. return resolveURL
  64. }
  65. return url
  66. }
  67. // utils
  68. // excldList: 1)suffix file; 2)dir
  69. function listFiles (f, excldList) {
  70. var filelist = [], dirlist = [];
  71. var dir = fs.lstatSync(f).isDirectory() ? f : path.dirname(f);
  72. var filelistTemp = fs.readdirSync(dir).filter(function (file) {
  73. return fs.statSync(path.join(dir, file)).isFile() && !_hasItem(excldList, file);
  74. }).map(function(file){
  75. return path.join(dir, file)
  76. })
  77. filelist = filelist.concat(filelistTemp)
  78. var dirlist = fs.readdirSync(dir).filter(function (file) {
  79. return fs.statSync(path.join(dir, file)).isDirectory() && !_hasItem(excldList, file);
  80. }).map(function(file){
  81. return path.join(dir, file)
  82. }).forEach(function(f){
  83. var filelistTemp = listFiles.call(this, f, excldList)
  84. filelist = filelist.concat(filelistTemp)
  85. })
  86. return filelist
  87. }
  88. function _hasItem(arr, item){
  89. arr = arr||[]
  90. var result = false
  91. arr.forEach(function(e){
  92. if(e === item){result=true}
  93. })
  94. return result
  95. }
  96. }