tpack-concat
Version:
TPack 插件:合并文件。
83 lines (67 loc) • 3.45 kB
JavaScript
/*
* Copyright (C) 2016 xuld<xuld@vip.qq.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* 合并文件。
* @param {BuildFile} file 要生成的文件。
* @param {Object} options 相关的选项。
*/
module.exports = function concat(file, options) {
// 设置默认值。
options = Object.assign({
seperator: "\n", // 每个文件的分隔符。
modulePrepend: "", // 每个文件顶部追加的文本。可以是文本或函数。函数原型为:modulePrepend(file, index)。
moduleAppend: "", // 每个文件底部追加的文本。可以是文本或函数。函数原型为:moduleAppend(file, index)。
prepend: "", // 最终文件底部追加的文本。可以是文本或函数。函数原型为:prepend(count)。
append: "", // 最终文件底部追加的文本。可以是文本或函数。函数原型为:append(count)。
content: null, // 返回每个文件内容的回调函数。函数原型为:content(file),函数返回 null 则使用原始文件内容。
}, options);
var index = 0;
if (options.prepend) {
file.append(typeof options.prepend === "function" ? options.prepend(index) : options.prepend);
}
file.rule.walk(function (name) {
if (index && options.seperator) {
file.append(options.seperator);
}
var related = file.builder.getFile(name);
if (options.modulePrepend) {
file.append(typeof options.modulePrepend === "function" ? options.modulePrepend(related, index) : options.modulePrepend);
}
var content = typeof options.content === "function" ? options.content(related, index) : options.content;
if (content != null) {
file.append(content);
} else {
file.append(related.content, related, 1, 0);
}
if (options.moduleAppend) {
file.append(typeof options.moduleAppend === "function" ? options.moduleAppend(related) : options.moduleAppend);
}
file.include(related);
index++;
});
if (options.append) {
file.append(typeof options.append === "function" ? options.append(index) : options.append);
}
};
module.exports.join = true;