This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 423
/
Gruntfile.js
204 lines (173 loc) · 7.43 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* eslint-env node, es6 */
'use strict';
module.exports = grunt => {
const
fs = require('fs'),
pathUtil = require('path'),
htmlclean = require('htmlclean'),
CleanCSS = require('clean-css'),
uglify = require('uglify-js'),
preProc = require('pre-proc'),
ROOT_PATH = __dirname,
SRC_DIR_PATH = pathUtil.join(ROOT_PATH, 'src'),
CSS_PATH = pathUtil.join(SRC_DIR_PATH, 'leader-line.css'),
DEST_DIR_PATH = pathUtil.join(ROOT_PATH, 'leader-line.min.js'),
PACK_LIBS = [
['anim', 'anim.js'],
['pathDataPolyfill', 'path-data-polyfill/path-data-polyfill.js'],
['AnimEvent', '../node_modules/anim-event/anim-event.min.js', /^[^]*?var\s+AnimEvent\s*=\s*([^]*)\s*;\s*$/]
],
// from leader-line.js
APP_ID = 'leader-line',
DEFS_ID = `${APP_ID}-defs`,
DEFAULT_LINE_SIZE = 4, // DEFAULT_OPTIONS.lineSize
DEFINED_VAR = {
PLUG_BEHIND: 'behind'
},
PKG = require('./package');
let code = {},
definedVar = Object.keys(DEFINED_VAR).reduce((definedVar, varName) => {
definedVar[varName] = `\f${varName}\x07`;
return definedVar;
}, {});
function minCss(content) {
return (new CleanCSS({level: {1: {specialComments: 0}}})).minify(content).styles;
}
function minJs(content) {
return uglify.minify(content).code;
}
grunt.initConfig({
taskHelper: {
getSvgDefs: {
options: {
handlerByContent: content => {
const cheerio = require('cheerio');
let $ = cheerio.load(content), defsSrc = '',
codeSrc = {
SYMBOLS: {},
PLUG_KEY_2_ID: {behind: definedVar.PLUG_BEHIND},
PLUG_2_SYMBOL: {}
}, cssSrc;
function getCode(value) {
let matches;
return typeof value === 'object' ?
`{${Object.keys(value).map(prop => `${prop}:${getCode(value[prop])}`).join(',')}}` :
typeof value === 'string' ? (
(matches = /^\f(.+)\x07$/.exec(value)) ? // eslint-disable-line no-control-regex
matches[1] : `'${value}'`
) : value;
}
$('svg').each((i, elm) => {
let symbol = $('.symbol', elm), size = $('.size', elm),
id, elmId, props, bBox, noOverhead, outlineBase, outlineMax;
if (symbol.length && size.length && (id = symbol.attr('id'))) {
elmId = `${APP_ID}-${id}`;
props = (symbol.attr('class') + '').split(' ');
defsSrc += $.xml(symbol.attr('id', elmId).removeAttr('class'));
codeSrc.SYMBOLS[id] = {elmId: elmId};
props.forEach(prop => {
let matches;
if ((matches = prop.match(/prop\-([^\s]+)/))) {
codeSrc.SYMBOLS[id][matches[1]] = true;
} else if ((matches = prop.match(/varId\-([^\s]+)/))) {
codeSrc[matches[1]] = id;
} else if (prop === 'no-overhead') {
noOverhead = true;
}
});
codeSrc.SYMBOLS[id].bBox = bBox = {
left: parseFloat(size.attr('x')),
top: parseFloat(size.attr('y')),
width: parseFloat(size.attr('width')),
height: parseFloat(size.attr('height'))
};
bBox.right = bBox.left + bBox.width;
bBox.bottom = bBox.top + bBox.height;
codeSrc.SYMBOLS[id].widthR = bBox.width / DEFAULT_LINE_SIZE;
codeSrc.SYMBOLS[id].heightR = bBox.height / DEFAULT_LINE_SIZE;
codeSrc.SYMBOLS[id].bCircle = Math.max(-bBox.left, -bBox.top, bBox.right, bBox.bottom);
codeSrc.SYMBOLS[id].sideLen = Math.max(-bBox.top, bBox.bottom);
codeSrc.SYMBOLS[id].backLen = -bBox.left;
codeSrc.SYMBOLS[id].overhead = noOverhead ? 0 : bBox.right;
if ((outlineBase = $('.outline-base', elm)).length &&
(outlineMax = $('.outline-max', elm)).length) {
codeSrc.SYMBOLS[id].outlineBase = parseFloat(outlineBase.attr('stroke-width')) / 2;
codeSrc.SYMBOLS[id].outlineMax =
parseFloat(outlineMax.attr('stroke-width')) / 2 / codeSrc.SYMBOLS[id].outlineBase;
}
codeSrc.PLUG_KEY_2_ID[id] = id;
codeSrc.PLUG_2_SYMBOL[id] = id;
}
});
cssSrc = minCss(
fs.readFileSync(CSS_PATH, {encoding: 'utf8'}).trim().replace(/^\s*@charset\s+[^;]+;/gm, ''));
// some version of cheerio have problem: <tag></tag> -> <tag/>
defsSrc = defsSrc.replace(/<([^>\s]+)([^>]*)><\/\1>/g, '<$1$2/>');
code.DEFS_HTML = '\'' +
htmlclean(`<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="${DEFS_ID}">` +
`<style><![CDATA[${cssSrc}]]></style><defs>${defsSrc}</defs></svg>`)
.replace(/\'/g, '\\\'') + '\'';
Object.keys(DEFINED_VAR).forEach(codeVar => { code[codeVar] = getCode(DEFINED_VAR[codeVar]); });
Object.keys(codeSrc).forEach(codeVar => { code[codeVar] = getCode(codeSrc[codeVar]); });
// for debug
fs.writeFileSync(`${SRC_DIR_PATH}/symbols.json`, JSON.stringify(codeSrc.SYMBOLS));
return `var ${Object.keys(code).map(codeVar => `${codeVar}=${code[codeVar]}`).join(',')};`;
}
},
src: `${SRC_DIR_PATH}/symbols.html`,
dest: `${SRC_DIR_PATH}/defs.js`
},
testFuncs: {
options: {
handlerByContent: content => {
content.replace(/@EXPORT\[file:([^\n]+?)\]@\s*(?:\*\/\s*)?([\s\S]*?)\s*(?:\/\*\s*|\/\/\s*)?@\/EXPORT@/g,
(s, file, content) => {
const path = pathUtil.join(SRC_DIR_PATH, file);
grunt.file.write(path, content);
grunt.log.writeln(`File "${path}" created.`);
});
return false;
}
},
src: `${SRC_DIR_PATH}/leader-line.js`,
dest: `${SRC_DIR_PATH}/dummy`
},
packJs: {
options: {
handlerByContent: content => {
const reEXPORT = /^[\s\S]*?@EXPORT@\s*(?:\*\/\s*)?([\s\S]*?)\s*(?:\/\*\s*|\/\/\s*)?@\/EXPORT@[\s\S]*$/;
PACK_LIBS.forEach(keyPath => {
code[keyPath[0]] = fs.readFileSync(pathUtil.join(SRC_DIR_PATH, keyPath[1]), {encoding: 'utf8'})
.replace(keyPath[2] || reEXPORT, '$1');
});
const banner = `/*! ${PKG.title || PKG.name} v${PKG.version} (c) ${PKG.author.name} ${PKG.homepage} */\n`;
return banner + minJs(preProc.removeTag('DEBUG',
content.replace(/@INCLUDE\[code:([^\n]+?)\]@/g,
(s, codeKey) => {
if (typeof code[codeKey] !== 'string') {
grunt.fail.fatal(`File doesn't exist code: ${codeKey}`);
}
return code[codeKey];
}
)
));
}
},
src: `${SRC_DIR_PATH}/leader-line.js`,
dest: DEST_DIR_PATH
}
}
});
grunt.loadNpmTasks('grunt-task-helper');
grunt.registerTask('defs', [
'taskHelper:getSvgDefs'
]);
grunt.registerTask('funcs', [
'taskHelper:testFuncs'
]);
grunt.registerTask('build', [
'defs',
'taskHelper:packJs'
]);
grunt.registerTask('default', ['build']);
};