@bravf
2015-07-23T08:09:54.000000Z
字数 2288
阅读 1110
前端架构
在fis-conf.js中添加如下插件代码
fis.config.set('modules.parser.js', function (content, file, settings){var fs = require('fs')var path = require('path')var crypto = require('crypto')var modTable = []var modLinkTable = {}var scanReg = /require\(['|"](.*?)['|"]\)/gfunction getMd5(str){var md5 = crypto.createHash('md5')md5.update(str)return md5.digest('hex').slice(-8)}function getFullPath(p){var fullPath = path.join(__dirname, p)return fullPath}function getModFile(p){var fullPath = getFullPath(p)var content = fs.readFileSync(fullPath) + ''if (p in modLinkTable){for (var relpath in modLinkTable[p]){var abspath = modLinkTable[p][relpath]content = content.replace(RegExp(relpath, 'g'), getMd5(abspath.replace(/\\/g, '/')))}}var windowFunc = 'window["' + getMd5(p.replace(/\\/g, '/')) + '"]'return '//#----------------mod start----------------\n' +'void function (module){\n\t' +windowFunc + '={};\n' +content.replace(/module\.exports/g, windowFunc).replace(/(^|\n)/g, '\n\t') +'\n}({exports:{}})\n' +'//#----------------mod end----------------\n\n'}function fillModLinkTable(subpath, requireNameA, requireNameB){if (!(subpath in modLinkTable)){modLinkTable[subpath] = {}}modLinkTable[subpath][requireNameA] = requireNameB}function scanMod(subpath){var modTable2 = []var modContent = fs.readFileSync(getFullPath(subpath)) + '';var execValuewhile ( (execValue = scanReg.exec(modContent)) != null ){var requireName = execValue[1]var modPath//如果rquire的是绝对路径if (requireName[0] == '/'){modPath = requireName}else {modPath = path.join(path.dirname(subpath), requireName)}fillModLinkTable(subpath, requireName, modPath)modTable2.push(modPath)}modTable2.forEach(function (mod){var idx = modTable.indexOf(mod)if (idx != -1){modTable.splice(idx, 1)}modTable.unshift(mod)scanMod(mod)})}//1、是js文件。2、文件名不能下划线打头(下划线的不被release出去)。3、min.js结尾的文件都直接被<script src>if ( (file.ext == '.js') && (file.filename[0] != '_') && (file.filename.slice(-4) != '.min') ){//console.log(file)modTable = []modLinkTable = {}scanMod(file.subpath)//把mods声明放到最前var modsContent = ''modTable.forEach(function (mod){modsContent += getModFile(mod)//watchfs.watch(getFullPath(mod), function (){var c = fs.readFileSync(file.fullname)fs.writeFileSync(file.fullname, c+'\n', 'utf-8')fs.writeFileSync(file.fullname, c, 'utf-8')})})content = modsContent + getModFile(file.subpath)//替换所有requirecontent = content.replace(scanReg, function (match, value){return 'window["' + value + '"]'})}return content})