UNPKG

4.18 kBJavaScriptView Raw
1/**
2 * @file jdists
3 *
4 * Code block processing tools
5 * @author
6 * zswang (http://weibo.com/zswang)
7 * @version 2.2.3
8 * @date 2018-05-30
9 */
10var fs = require('fs');
11var path = require('path');
12var scope = require('./scope');
13var colors = require('colors/safe');
14global.require = require; // ejs, jhtmls use
15var defaultProcessors = {
16 "ejs": require('../processor/processor-ejs'),
17 "extract": require('../processor/processor-extract'),
18 "fndep": require('../processor/processor-fndep'),
19 "glob": require('../processor/processor-glob'),
20 "html": require('../processor/processor-html'),
21 "jhtmls": require('../processor/processor-jhtmls'),
22 "jphps": require('../processor/processor-jphps'),
23 "quoted": require('../processor/processor-quoted'),
24 "regex": require('../processor/processor-regex'),
25 "toggle": require('../processor/processor-toggle'),
26};
27var defaultTags = {
28 jdists: {
29 encoding: 'original'
30 }
31};
32var defaultExclude = [
33 '**/*.+(png|jpeg|jpg|mp3|ogg|gif|eot|ttf|woff)',
34 '**/*.min.+(js|css)'
35];
36var defaultRemove = 'remove,test,debug';
37var defaultTrigger = 'release';
38/**
39 * 编译 jdists 文件
40 *
41 * @param {string} filename 文件名或者是内容
42 * @param {Object} argv 配置项
43 * @param {boolean} argv.clean 是否清除连续空行,默认 true
44 * @param {string} argv.remove 需要移除的标签列表,默认 "remove,test"
45 * @param {string} argv.fromString 当为 true 时 filename 参数则看作内容
46 * @param {Function} hook 预处理作用域
47 * @return {string} 返回编译后的结果
48 */
49function build(filename, argv, hook) {
50 // 处理默认值
51 argv = argv || {};
52 argv.trigger = argv.trigger || defaultTrigger;
53 argv.remove = argv.remove || defaultRemove;
54 var scopes = {};
55 var variants = {};
56 var tags = JSON.parse(JSON.stringify(defaultTags));
57 var excludeList = defaultExclude.slice();
58 var removeList = argv.remove.split(/\s*,\s*/);
59 var processors = {};
60 var clean = true;
61 for (var key in defaultProcessors) {
62 processors[key] = defaultProcessors[key];
63 }
64 // 处理配置文件
65 var configFilename = argv.config || '.jdistsrc';
66 if (fs.existsSync(configFilename)) {
67 var config = JSON.parse(fs.readFileSync(configFilename));
68 if (typeof config.clean !== 'undefined') {
69 clean = config.clean;
70 }
71 if (config.exclude instanceof Array) {
72 excludeList = excludeList.concat(config.exclude);
73 }
74 if (config.processors) {
75 for (var encoding in config.processors) {
76 registerProcessor(encoding, config.processors[encoding]);
77 }
78 }
79 if (config.tags) {
80 for (var name in config.tags) {
81 tags[name] = config.tags[name];
82 }
83 }
84 }
85 else if (configFilename !== '.jdistsrc') {
86 console.error(
87 colors.red('Config file "%s" not exists.'), configFilename
88 );
89 }
90 if (typeof argv.clean !== 'undefined') {
91 clean = argv.clean;
92 }
93 var content;
94 if (argv.fromString) {
95 content = filename;
96 filename = argv.path || 'none';
97 }
98 var rootScope = scope.create({
99 fromString: argv.fromString,
100 content: content,
101 clean: clean,
102 removeList: removeList,
103 excludeList: excludeList,
104 filename: filename,
105 tags: tags,
106 argv: argv,
107 env: process.env,
108 scopes: scopes,
109 variants: variants,
110 processors: processors
111 });
112 if (typeof hook === 'function') {
113 hook(rootScope);
114 }
115 return rootScope.build();
116}
117exports.build = build;
118/**
119 * 注册默认处理器
120 *
121 * @param {string} encoding 编码名称
122 * @param {Function|string} processor 处理函数
123 */
124function registerProcessor(encoding, processor) {
125 if (!processor || !encoding) {
126 return;
127 }
128 if (typeof processor === 'function') {
129 defaultProcessors[encoding] = processor;
130 return true;
131 }
132 else if (typeof processor === 'string' && processor) {
133 if (fs.existsSync(processor)) {
134 return registerProcessor(
135 encoding,
136 scope.buildProcessor(fs.readFileSync(processor))
137 );
138 }
139 else {
140 return registerProcessor(
141 encoding,
142 scope.buildProcessor(processor)
143 );
144 }
145 }
146}
147exports.registerProcessor = registerProcessor;
148exports.createScope = scope.create;
\No newline at end of file