1 | const fs = require('fs');
|
2 | const gulp = require('gulp');
|
3 | const gutil = require('gulp-util');
|
4 | const lang = require('zero-lang');
|
5 | const path = require('path');
|
6 | const plumber = require('gulp-plumber');
|
7 | const rename = require('gulp-rename');
|
8 | const through = require('through2');
|
9 | const tpl2mod = require('template2module');
|
10 | const config = require('./config');
|
11 |
|
12 | const underscoreEngine = tpl2mod.engines.underscore;
|
13 | underscoreEngine.outerScopeVars = {
|
14 | JSON: true,
|
15 | _: true,
|
16 | __e: true,
|
17 | __p: true,
|
18 | __t: true,
|
19 | lang: true,
|
20 | };
|
21 |
|
22 | const 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 |
|
29 | const svgSprite = '';
|
30 |
|
31 | function parsingSvgSprite(content) {
|
32 | return content.replace(REGEXP.svgSpriteTag, svgSprite);
|
33 | }
|
34 |
|
35 | function 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 |
|
49 | function 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 |
|
61 | const templateContent = parsingSvgSprite(importing(file.contents.toString('utf8'), file.path))
|
62 | .replace(REGEXP.spacesBetweenTags, '><');
|
63 |
|
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 |
|
82 | lang.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 |
|
96 | gulp.task('template2module', lang.map(config.templateDirs, (dir) => `template2module-${dir}`));
|