-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
90 lines (76 loc) · 2.89 KB
/
gulpfile.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
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const vfs = require('vinyl-fs');
const compileSass = require('gulp-sass');
const rename = require('gulp-rename');
const inlineTemplates = require('gulp-inline-ng2-template');
const tsc_wrapped = require('@angular/tsc-wrapped');
const gap = require('gulp-append-prepend');
const src = '.';
const dest = './tmp';
const lib = 'angular-draft-js';
const draftJsDist = './node_modules/draft-js/dist';
gulp.task('vendorDraftJsCss', function () {
return gulp
.src([`${draftJsDist}/Draft.css`])
.pipe(gap.prependText('::ng-deep { '))
.pipe(gap.prependText('/* This file is automatically generated from Draft.css upstream */'))
.pipe(gap.prependText('/* Do not edit */'))
.pipe(gap.appendText(' }'))
.pipe(compileSass())
.pipe(rename('draft-js.component.css'))
.pipe(gulp.dest(`${src}/${lib}`));
});
gulp.task('compileRichEditorComponentSass', function () {
return gulp
.src([`${src}/${lib}/editors/*.scss`])
.pipe(compileSass())
.pipe(gulp.dest(`${src}/${lib}/editors`));
});
gulp.task('inlineCss', ['copyEditorTs', 'copyLibTs', 'vendorDraftJsCss', 'compileRichEditorComponentSass'], function () {
inlineSync(
`${src}/${lib}/draft-js.component.ts`,
`${dest}/${lib}/draft-js.component.ts`);
inlineSync(
`${src}/${lib}/editors/rich.component.ts`,
`${dest}/${lib}/editors/rich.component.ts`);
});
gulp.task('copyEditorTs', function () {
return gulp
.src([`${src}/${lib}/editors/*.ts`, `!${src}/${lib}/editors/*.spec.ts`])
.pipe(gulp.dest(`${dest}/${lib}/editors`));
});
gulp.task('copyLibTs', function () {
return gulp
.src([`${src}/${lib}/*.ts`, `!${src}/${lib}/*.spec.ts`])
.pipe(gulp.dest(`${dest}/${lib}`));
});
gulp.task('copyTs', function () {
return gulp
.src([`${src}/*.ts`])
.pipe(gulp.dest(dest));
});
gulp.task('inlineComponent', ['copyTs', 'inlineCss']);
/* Inlining-related utilities */
function inlineSync(sourcePath, destPath) {
var fileContent = fs.readFileSync(sourcePath, 'utf-8');
fileContent = inlineStyles(fileContent, sourcePath);
fs.writeFileSync(destPath, fileContent, 'utf-8');
}
function inlineStyles(fileContent, filePath) {
return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, function (_match, styleUrlsValue) {
// The RegExp matches the array of external style files. This is a string right now and
// can to be parsed using the `eval` method. The value looks like "['AAA.css', 'BBB.css']"
var styleUrls = eval(styleUrlsValue);
var styleContents = styleUrls
.map(function (url) { return path.join(path.dirname(filePath), url); })
.map(function (path) { return loadResourceFile(path); });
return "styles: [\"" + styleContents.join(' ') + "\"]";
});
}
function loadResourceFile(filePath) {
return fs.readFileSync(filePath, 'utf-8')
.replace(/([\n\r]\s*)+/gm, ' ')
.replace(/"/g, '\\"');
}