Created
March 4, 2014 05:47
-
-
Save otakustay/9340933 to your computer and use it in GitHub Desktop.
模块合并
This file contains hidden or 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
/** | |
* @file build默认配置 | |
* @author errorrik[[email protected]] | |
*/ | |
var AddViewName = require('./tool/build/AddViewName'); | |
var package = require('./tool/util').getModulesFromPackage; | |
var namespace = require('./tool/util').getModulesFromNamespace; | |
var getBizNamespaces = require('./tool/util').getBizNamespaces; | |
var cwd = process.cwd(); | |
var path = require('path'); | |
var args = {}; | |
(function () { | |
for (var i = 4; i < process.argv.length; i++) { | |
var arg = process.argv[i]; | |
console.log(arg); | |
var pair = arg.split('='); | |
var key = pair[0].substring(2); | |
var value = pair[1]; | |
args[key] = value; | |
} | |
}()); | |
var svnRevision = args.revision || ''; | |
/** | |
* 输入目录 | |
* | |
* @type {string} | |
*/ | |
exports.input = cwd; | |
/** | |
* 输出目录 | |
* | |
* @type {string} | |
*/ | |
exports.output = path.resolve(cwd, args.output || 'output'); | |
/** | |
* 排除文件pattern列表 | |
* | |
* @type {Array} | |
*/ | |
exports.exclude = [ | |
'/src/static/tpl/*', | |
'/tool', | |
'/doc', | |
'/test', | |
'/mockup', | |
'/demo', | |
'/copyright.txt', | |
// '/index-debug.html', | |
'/index-qa.html', | |
'/module.conf', | |
'/dep/packages.manifest', | |
'/dep/*/*/test', | |
'/dep/*/*/doc', | |
'/dep/*/*/demo', | |
'/dep/*/*/example', | |
'/dep/*/*/tool', | |
'/dep/*/*/jsduck', | |
'/dep/*/*/*.md', | |
'/dep/*/*/package.json', | |
'/dep/echarts/*/src/util/mapData/rawData/china/*.js', | |
'/dep/echarts/*/src/util/mapData/rawData/*.js', | |
'/edp-*', | |
'/.edpproj', | |
'.svn', | |
'.git', | |
'.gitignore', | |
'.idea', | |
'.project', | |
'Desktop.ini', | |
'Thumbs.db', | |
'.DS_Store', | |
'*.md', | |
'*.tmp', | |
'*.bak', | |
'*.swp' | |
]; | |
var moduleEntries = 'html,htm,phtml,tpl,vm,js'; | |
var pageEntries = 'html,htm,phtml,tpl,vm'; | |
/** | |
* 获取构建processors的方法 | |
* | |
* @return {Array} | |
*/ | |
exports.getProcessors = function () { | |
return [ | |
new AddViewName(), | |
new LessCompiler({ | |
exclude: [ | |
'src/ui/css/*.less', | |
'src/common/css/*.less', | |
'src/*/css/*.less', | |
'src/*/*/css/*.less', | |
'src/*/ui/css/*.less', | |
'src/*/*/ui/css/*.less' | |
], | |
include: [ | |
'src/common/css/extern.less', | |
'src/common/css/main.less', | |
'src/common/css/reset.less' | |
], | |
entryExtnames: pageEntries | |
}), | |
new CssCompressor(), | |
new ModuleCompiler({ | |
exclude: [ | |
'dep/etpl/*/src/main.js', | |
'dep/jquery/*/src/jquery.js', | |
'dep/jquery/*/src/jquery.min.js', | |
'src/external/esl.js', | |
'src/external/h.js', | |
'src/external/json2.js', | |
'src/static/footer.js', | |
'src/static/header.js' | |
], | |
getCombineConfig: function () { | |
var bizNamespaces = getBizNamespaces(); | |
var bizUIModules = []; | |
bizNamespaces | |
.map(function (namespace) { return namespace + '/ui'; }) | |
.forEach(function (bizNamespace) { bizUIModules.push.apply(bizUIModules, namespace(bizNamespace)); }); | |
// 3个启动脚本 | |
var config = { | |
'startup/ui': { | |
include: ['moment/lang/zh-cn'].concat(package('esui')), | |
exclude: [ | |
'esui/Sidebar' // `Sidebar`用自己的 | |
] | |
}, | |
'startup/ria': { | |
include: ['echarts', 'echarts/chart/line', 'echarts/chart/bar'] | |
.concat(package('er', 'ef', 'ub-ria', 'er-track', 'etpl', 'saber-cookie')) | |
.concat('common/extension') | |
.concat(namespace('common/extension')), | |
exclude: package('mini-event', 'moment', 'underscore', 'esui') | |
}, | |
'startup/biz': { | |
include: ['report/delivery/Dashboard', 'report/slot/Dashboard', 'report/order/Dashboard'] | |
.concat(namespace('common', 'ui', 'error', 'bulletin')) | |
.concat(namespace('dashboard', 'slot', 'order', 'delivery')) | |
.concat(bizUIModules), | |
exclude: package( | |
'mini-event', 'moment', 'underscore', 'saber-cookie', | |
'etpl', 'er', 'er-track', 'ef', 'echarts', 'esui', 'ub-ria' | |
) | |
} | |
}; | |
var baseModules = namespace('common', 'ui') | |
.concat(package('er', 'ef', 'ub-ria', 'er-track', 'etpl', 'saber-cookie')) | |
.concat(package('esui', 'echarts', 'zrender', 'mini-event', 'underscore', 'moment')); | |
bizNamespaces.forEach(function (bizNamespace) { | |
var exclude = baseModules.concat(bizNamespace + '/ui'); | |
// 通用 | |
config[bizNamespace + '/List'] = { exclude: exclude }; | |
config[bizNamespace + '/Form'] = { exclude: exclude }; | |
config[bizNamespace + '/Detail'] = { exclude: exclude }; | |
config[bizNamespace + '/Read'] = { exclude: exclude }; | |
// 报表 | |
config[bizNamespace + '/Date'] = { exclude: exclude }; | |
config[bizNamespace + '/General'] = { exclude: exclude }; | |
config[bizNamespace + '/Hour'] = { exclude: exclude }; | |
}); | |
return config; | |
}, | |
configFile: 'module.conf', | |
entryExtnames: moduleEntries | |
}), | |
new TplMerge({ | |
exclude: [ | |
'src/external/*', | |
'src/static/*' | |
] | |
}), | |
// new JsCompressor(), | |
new PathMapper({ | |
replacements: [ | |
{ type: 'html', tag: 'link', attribute: 'href', extnames: pageEntries }, | |
{ type: 'html', tag: 'img', attribute: 'src', extnames: pageEntries }, | |
{ type: 'html', tag: 'script', attribute: 'src', extnames: pageEntries }, | |
{ type: 'html', tag: 'a', attribute: 'href', extnames: pageEntries }, | |
{ extnames: moduleEntries, replacer: 'module-config' }, | |
{ extnames: 'css,less', replacer: 'css' } | |
], | |
from: 'src', | |
to: 'asset-' + svnRevision | |
}), | |
new PathMapper({ | |
replacements: [ | |
{ type: 'html', tag: 'link', attribute: 'href', extnames: pageEntries }, | |
{ type: 'html', tag: 'img', attribute: 'src', extnames: pageEntries }, | |
{ type: 'html', tag: 'script', attribute: 'src', extnames: pageEntries }, | |
{ type: 'html', tag: 'a', attribute: 'href', extnames: pageEntries }, | |
{ extnames: moduleEntries, replacer: 'module-config' }, | |
{ extnames: 'css,less', replacer: 'css' } | |
], | |
from: 'dep', | |
to: 'dep-' + svnRevision | |
}), | |
new AddCopyright(), | |
new ReplaceDebug({ | |
exclude: ['*.tpl.html'] | |
}) | |
]; | |
}; | |
/** | |
* builder主模块注入processor构造器的方法 | |
* | |
* @param {Object} processors | |
*/ | |
exports.injectProcessor = function (processors) { | |
for (var key in processors) { | |
global[key] = processors[key]; | |
} | |
}; |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> | |
<title>XXXX</title> | |
<link rel="stylesheet" href="dep/esui/3.1.0-alpha.6/src/css/main.css"> | |
<link rel="stylesheet" href="src/common/css/main.css"> | |
<link rel="icon" href="http://www.baidu.com/favicon.ico" /> | |
<script src="src/external/esl.js"></script> | |
<script> | |
try { | |
document.domain = 'baidu.com'; | |
} | |
catch (domainSecurityError) { | |
} | |
</script> | |
</head> | |
<body> | |
<script> | |
document.createElement('aside'); | |
document.createElement('footer'); | |
document.createElement('header'); | |
document.createElement('nav'); | |
document.createElement('section'); | |
document.createElement('article'); | |
document.createElement('figure'); | |
document.createElement('figcaption'); | |
</script> | |
<header id="header"> | |
<h1 id="logo"><a href="#">百度广告管家</a></h1> | |
<nav id="nav"></nav> | |
<div id="accounts"></div> | |
<div id="message"></div> | |
</header> | |
<div id="main"></div> | |
<footer id="foot"></footer> | |
<div data-ui-type="ToolBox" data-ui-id="toolbox" data-ui-title="我的工具箱" data-ui-hidden="hidden"></div> | |
<div id="global-mask">正在为您服务……</div> | |
<script> | |
window.DEBUG = true; | |
if (location.search.indexOf('DEBUG') >= 0) { | |
window.DEBUG = !0; | |
} | |
require.config({ | |
'baseUrl': 'src', | |
'packages': [ | |
{ | |
'name': 'underscore', | |
'location': '../dep/underscore/1.5.2/src', | |
'main': 'underscore' | |
}, | |
{ | |
'name': 'ef', | |
'location': '../dep/ef/3.1.0-alpha.3/src', | |
'main': 'main' | |
}, | |
{ | |
'name': 'er', | |
'location': '../dep/er/3.1.0-alpha.6/src', | |
'main': 'main' | |
}, | |
{ | |
'name': 'esui', | |
'location': '../dep/esui/3.1.0-alpha.6/src', | |
'main': 'main' | |
}, | |
{ | |
'name': 'echarts', | |
'location': '../dep/echarts/1.3.6/src', | |
'main': 'echarts' | |
}, | |
{ | |
'name': 'zrender', | |
'location': '../dep/zrender/1.0.8/src', | |
'main': 'zrender' | |
}, | |
{ | |
'name': 'jquery', | |
'location': '../dep/jquery/1.9.1/src', | |
'main': 'jquery.min.js' | |
}, | |
{ | |
'name': 'moment', | |
'location': '../dep/moment/2.0.0/src', | |
'main': 'moment' | |
}, | |
{ | |
'name': 'saber-cookie', | |
'location': '../dep/saber-cookie/0.1.0/src', | |
'main': 'cookie' | |
}, | |
{ | |
'name': 'mini-event', | |
'location': '../dep/mini-event/1.0.0/src', | |
'main': 'main' | |
}, | |
{ | |
'name': 'er-track', | |
'location': '../dep/er-track/0.9.1/src', | |
'main': 'main' | |
}, | |
{ | |
'name': 'etpl', | |
'location': '../dep/etpl/2.0.8/src', | |
'main': 'main' | |
}, | |
{ | |
'name': 'est', | |
'location': '../dep/est/1.1.0/src' | |
}, | |
{ | |
'name': 'ub-ria', | |
'location': '../dep/ub-ria/0.8.0-alpha.4/src', | |
'main': 'main' | |
}, | |
{ | |
'name': 'test', | |
'location': '../test' | |
} | |
], | |
'paths': { | |
'tpl': 'common/tpl', | |
'js': 'external/js' | |
} | |
}); | |
</script> | |
<script src="src/startup/ui.js"></script> | |
<script src="src/startup/ria.js"></script> | |
<script> | |
// 2个扩展必须在业务代码前启用,但`define`是异步的,因此不能直接`require` | |
require( | |
['ub-ria', 'common/extension'], | |
function (ria, extension) { | |
ria.enableExtensions(); | |
extension.enable(); | |
// 压缩脚本要要任何模块前加载 | |
require( | |
['startup/biz'], | |
function () { | |
require( | |
['common/main', 'common/gray'], | |
function (main) { | |
main.start(); | |
} | |
); | |
} | |
) | |
} | |
); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
var amd = require('edp-core').esl; | |
var fs = require('fs'); | |
var path = require('path'); | |
var u = require('underscore'); | |
var moduleConfigFile = path.resolve(__dirname, '..', 'module.conf'); | |
function getModulesFromDirectory(directory) { | |
var modules = []; | |
var files = require('glob').sync(directory + '/**') | |
.filter(function (file) { return path.extname(file) === '.js' }); | |
u.each( | |
files, | |
function (file) { | |
modules.push.apply(modules, amd.getModuleId(file, moduleConfigFile)); | |
} | |
); | |
return modules; | |
} | |
exports.getModulesFromPackage = function () { | |
var modules = []; | |
u.each( | |
arguments, | |
function (package) { | |
var file = amd.getModuleFile(package, moduleConfigFile); | |
var directory = path.dirname(file); | |
modules.push.apply(modules, getModulesFromDirectory(directory)); | |
var packageConfigFile = path.join(directory, '..', 'package.json'); | |
if (fs.existsSync(packageConfigFile)) { | |
var packageConfig = JSON.parse(fs.readFileSync(packageConfigFile, 'utf-8')); | |
if (packageConfig.main) { | |
modules.push(package + '/' + packageConfig.main); | |
} | |
} | |
} | |
); | |
return modules; | |
}; | |
exports.getModulesFromNamespace = function () { | |
var modules = []; | |
u.each( | |
arguments, | |
function (namespace) { | |
var directory = path.resolve(__dirname, '..', 'src', namespace); | |
modules.push.apply(modules, getModulesFromDirectory(directory)); | |
} | |
); | |
return modules; | |
}; | |
exports.getBizNamespaces = function () { | |
var src = path.resolve(__dirname, '..', 'src'); | |
var namespaces = fs.readdirSync(src) | |
.filter(function (file) { return fs.statSync(path.join(src, file)).isDirectory(); }); | |
var toolNamespaces = fs.readdirSync(path.join(src, 'tool')) | |
.filter(function (file) { return fs.statSync(path.join(src, 'tool', file)).isDirectory(); }) | |
.map(function (namespace) { return 'tool/' + namespace }); | |
return namespaces.concat(toolNamespaces); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment