UNPKG

3.29 kBJavaScriptView Raw
1const fs = require('fs');
2const gulp = require('gulp');
3const gutil = require('gulp-util');
4const lang = require('zero-lang');
5const path = require('path');
6const plumber = require('gulp-plumber');
7const rename = require('gulp-rename');
8const through = require('through2');
9const tpl2mod = require('template2module');
10const config = require('./config');
11
12const underscoreEngine = tpl2mod.engines.underscore;
13underscoreEngine.outerScopeVars = {
14 JSON: true,
15 _: true,
16 __e: true,
17 __p: true,
18 __t: true,
19 lang: true,
20};
21
22const REGEXP = {
23 importTag: /<import\s+src="\S*"><\/import>/g,
24 svgSpriteTag: /<svg-sprite\/>/,
25 srcPath: /src="(\S*)"/,
26 spacesBetweenTags: />[\s|\r|\n]*</g,
27};
28
29const svgSprite = ''; // fs.readFileSync(path.resolve(__dirname, '../dist/zfinder/svg-symbols.svg'), 'utf8');
30
31function parsingSvgSprite(content) {
32 return content.replace(REGEXP.svgSpriteTag, svgSprite);
33}
34
35function importing(content, resourcePath) {
36 const match = content.match(REGEXP.importTag);
37 if (match) {
38 lang.each(match, (m) => {
39 const sourcePath = m.match(REGEXP.srcPath)[1];
40 const absoluteSourcePath = path.resolve(path.dirname(resourcePath), sourcePath);
41 const sourceOriginContent = fs.readFileSync(absoluteSourcePath, 'utf8');
42 const sourceDistContent = importing(sourceOriginContent, absoluteSourcePath);
43 content = content.replace(m, sourceDistContent);
44 });
45 }
46 return content;
47}
48
49function renderTemplates() {
50 return through.obj(function render(file, enc, cb) {
51 if (file.isNull()) {
52 this.push(file);
53 return cb();
54 }
55
56 if (file.isStream()) this.emit('error', new gutil.PluginError('template2module', 'Streaming not supported'));
57
58 try {
59 gutil.log(file.path);
60 // @TODO add svg sprite file as needed, instead of putting the whole evil-icons svg file
61 const templateContent = parsingSvgSprite(importing(file.contents.toString('utf8'), file.path))
62 .replace(REGEXP.spacesBetweenTags, '><');
63 // FIXME: if there are spaces between tags, it will not work in browser envs.
64 const content = underscoreEngine.render(templateContent, file.path, 'commonjs')
65 .replace(', helper', '')
66 .replace(/\s+helper =[^}]+};/, '')
67 .replace(/\s+var __j[^;]+;/, '')
68 .replace(/\s+var print[^{]+\{[^}]+};/, '')
69 .replace(/\s+return \{[^}]+}/, '')
70 .replace(/\s+return String[^{]+\{[^}]+}\);/, '')
71 .replace(/\s+var __e[^{]+\{[^}]+};/, '');
72 file.contents = new Buffer(`const lang = require('zero-lang');\nconst __e = require('../escape');\n${content}`);
73 } catch (err) {
74 this.emit('error', new gutil.PluginError('template2module', err.toString()));
75 }
76
77 this.push(file);
78 return cb();
79 });
80}
81
82lang.each(config.templateDirs, (dir) => {
83 gulp.task(`template2module-${dir}`, () =>
84 gulp.src(path.resolve(__dirname, `../${dir}/**/*.xml`))
85 .pipe(plumber())
86 .pipe(renderTemplates())
87 .on('error', (err) => {
88 gutil.log(gutil.colors.red(err.message));
89 })
90 .pipe(rename((pathname) => {
91 pathname.extname = '.js';
92 }))
93 .pipe(gulp.dest(path.resolve(__dirname, `../${dir}/`))));
94});
95
96gulp.task('template2module', lang.map(config.templateDirs, (dir) => `template2module-${dir}`));