UNPKG

1.5 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const jdfUtils = require('jdf-utils');
5const $ = jdfUtils.base;
6const f = jdfUtils.file;
7const logger = require('jdf-log');
8const jdf = require('./jdf');
9const vfs = require('./VFS/VirtualFileSystem');
10
11/**
12 * 根据config.json的配置来合并文件,格式如下
13 * "concat": {
14 * "js/c1.js": ["js/c1.js", "js/c2.js", "widget/about/about.js"],
15 * "css/c1.css": ["css/c1.sass", "css/c2.less"]
16 * }
17 * @param rSource 项目输出的目录名称
18 */
19module.exports.init = function(rSource) {
20 const concatFiles = jdf.config.output.concat;
21 const source = path.resolve(f.realpath(rSource), jdf.getProjectPath());
22 logger.verbose(`concat: ${source}`);
23
24 Object.keys(concatFiles).forEach(desFile => {
25 let content = '';
26 desFile.forEach(singleFile => {
27 // 获取要输出的文件名称
28 const singleFilePath = path.resolve(source, singleFile);
29 logger.verbose(`singleFilePath is ${singleFilePath}`);
30
31 const vfsFile = vfs.queryFile(singleFilePath);
32 if (vfsFile) {
33 content += vfsFile.targetContent;
34 }
35 else {
36 logger.error(`${singleFilePath} is not exists`);
37 }
38 });
39 logger.verbose(`${desFilePath} file content is ${content}`);
40
41 if (content !== '') {
42 const desFilePath = path.resolve(source, desFile);
43 vfs.addFile(desFilePath, content);
44 }
45 });
46}