punchcutter 2.0.13 → 2.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/buildJsFont.js +47 -28
- package/package.json +1 -1
package/lib/buildJsFont.js
CHANGED
|
@@ -13,48 +13,67 @@ const svgMin = require('./svgMin');
|
|
|
13
13
|
* @param {Object} config
|
|
14
14
|
* @returns {Promise}
|
|
15
15
|
*/
|
|
16
|
-
module.exports = function (config) {
|
|
16
|
+
module.exports = async function (config) {
|
|
17
17
|
console.log(`Building JS font: ${chalk.cyan(config.name)}`);
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
25
|
-
})
|
|
26
|
-
.then(function (files) {
|
|
27
|
-
const map = {};
|
|
28
|
-
|
|
29
|
-
_.forEach(files, function (file) {
|
|
30
|
-
const extname = path.extname(file.path);
|
|
31
|
-
const basename = path.basename(file.path, extname);
|
|
32
|
-
const {data} = file;
|
|
19
|
+
const {
|
|
20
|
+
build: {concat = true, idTransform = (id) => id}
|
|
21
|
+
} = config;
|
|
22
|
+
|
|
23
|
+
const srcFiles = await loadFiles(config.src);
|
|
33
24
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
const files = await Promise.mapSeries(srcFiles, (file) => {
|
|
26
|
+
// Minify SVG.
|
|
27
|
+
return Promise.props(_.assign(file, {data: svgMin(file.data)}));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const map = Object.fromEntries(
|
|
31
|
+
files
|
|
32
|
+
.map((file) => {
|
|
33
|
+
const basename = idTransform(
|
|
34
|
+
path.basename(file.path, path.extname(file.path))
|
|
35
|
+
);
|
|
36
|
+
const {data} = file;
|
|
38
37
|
|
|
39
38
|
const $svg = cheerio.load(data, {
|
|
40
39
|
xmlMode: true
|
|
41
40
|
})('svg');
|
|
42
41
|
|
|
43
42
|
if ($svg.length) {
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
return [
|
|
44
|
+
basename,
|
|
45
|
+
{
|
|
46
|
+
viewBox: $svg.attr('viewBox'),
|
|
47
|
+
data: $svg.contents().toString()
|
|
48
|
+
}
|
|
49
|
+
];
|
|
46
50
|
}
|
|
47
|
-
})
|
|
51
|
+
})
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (concat) {
|
|
56
|
+
const distPath = path.resolve(config.build.dist, config.name + '.js');
|
|
57
|
+
const distDir = path.dirname(distPath);
|
|
58
|
+
|
|
59
|
+
await fs.mkdirp(distDir);
|
|
60
|
+
|
|
61
|
+
const data =
|
|
62
|
+
config.build.prepend + JSON.stringify(map, null, 2) + config.build.append;
|
|
63
|
+
|
|
64
|
+
return fs.writeFile(distPath, data, 'utf-8');
|
|
65
|
+
} else {
|
|
66
|
+
const distDir = path.resolve(config.build.dist, config.name);
|
|
67
|
+
|
|
68
|
+
await fs.mkdirp(distDir);
|
|
48
69
|
|
|
70
|
+
return Promise.mapSeries(Object.entries(map), ([basename, iconData]) => {
|
|
49
71
|
const data =
|
|
50
72
|
config.build.prepend +
|
|
51
|
-
JSON.stringify(
|
|
73
|
+
JSON.stringify(iconData, null, 2) +
|
|
52
74
|
config.build.append;
|
|
53
|
-
const distPath = path.resolve(config.build.dist, config.name + '.js');
|
|
54
|
-
const distDir = path.dirname(distPath);
|
|
55
75
|
|
|
56
|
-
return fs.
|
|
57
|
-
return fs.writeFile(distPath, data, 'utf-8');
|
|
58
|
-
});
|
|
76
|
+
return fs.writeFile(path.join(distDir, basename + '.js'), data, 'utf-8');
|
|
59
77
|
});
|
|
78
|
+
}
|
|
60
79
|
};
|