-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
171 lines (139 loc) · 5.42 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
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
"use strict"
var gulp = require("gulp");
var merge2 = require('merge2');
var ts = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var gulpServer = require('gulp-live-server');
var tap = require('gulp-tap');
var path = require('path');
var del = require("del");
var tsExport = require('gulp-ts-export').default;
// Config
var packageJson = require("./package.json");
packageJson.config = packageJson.config || {};
var packageSourceDir = packageJson.config.sourceDir || "src";
var outputDir = packageJson.config.outputDir || "lib";
var rootExportPath = packageJson.config.rootExport || undefined;
var tsconfigPath = packageSourceDir + '/tsconfig.json';
let tsconfigToCodePath = "code";
var typescriptsDir = packageSourceDir + "/" + tsconfigToCodePath;
let externalTypesSrc = ["typings/index.d.ts"];
var libraryTsSrc = [typescriptsDir + "/**/*.ts", "!" + typescriptsDir + "/**/*.test.ts"];
var tsAndTestsSrc = [typescriptsDir + "/**/*.ts"];
var unitTestsSrc = [typescriptsDir + "/**/*.test.ts"];
let mainFilePath = packageJson.main;
let mainFileName = path.basename(mainFilePath);
let mainFileDir = path.dirname(mainFilePath);
let typingsFilePath = packageJson.typings || packageJson.types;
let typingsFileName = path.basename(typingsFilePath);
let typingsFileDir = path.dirname(typingsFilePath);
// Tasks names
let buildTask = 'build';
let cleanTask = "clean";
let compileTsTask = "compile";
let compileToMemoryTask = "compile ts to memory";
let injectTestsToPageTask = 'inject test to html';
let generateMainDeclerationFileTask = "generate main decleration file";
let compileTsBundleTask = "compile ts bundle";
// Help functions
function prefixPathByPackageName(file) {
file.path = file.path.replace(file.relative, packageJson.name + "/" + file.relative);
}
function prefixPathByPackageNameStream() {
return tap(file =>
file.path = file.path.replace(file.relative, packageJson.name + "/" + file.relative));
}
function getPackageTsStream() {
return gulp.src(libraryTsSrc, { base: typescriptsDir })
.pipe(prefixPathByPackageNameStream());
}
function replace_RootExport_Registration_With_PackageName(mainFile) {
let rootExportRelativePath = path.relative(typescriptsDir, rootExportPath);
let rootRegistration = packageJson.name + "/" + rootExportRelativePath.replace(".ts", "");
mainFile.contents = new Buffer(mainFile.contents.toString()
.replace(rootRegistration, packageJson.name));
}
// Tasks
gulp.task(buildTask, [generateMainDeclerationFileTask, injectTestsToPageTask, compileTsTask, compileTsBundleTask]);
gulp.task(cleanTask, function () {
return del([
outputDir + '/**/*'
]);
});
gulp.task(generateMainDeclerationFileTask, function () {
let src = rootExportPath ?
gulp.src(rootExportPath, { base: packageSourceDir }) :
gulp.src(libraryTsSrc, { base: packageSource, read: false });
return src
.pipe(tsExport(typingsFileName, { exportedBase: "./" }))
.pipe(gulp.dest(typingsFileDir))
;
});
gulp.task(compileTsBundleTask, () => {
let tsBundledProject = ts.createProject(tsconfigPath, {
outFile: mainFileName,
rootDir: tsconfigToCodePath
});
return merge2(
getPackageTsStream(),
gulp.src(externalTypesSrc))
.pipe(sourcemaps.init())
.pipe(ts(tsBundledProject))
.pipe(tap(file => replace_RootExport_Registration_With_PackageName(file)))
.pipe(sourcemaps.write())
.pipe(gulp.dest(mainFileDir))
;
});
let jsStream;
let dtsStream;
gulp.task(compileToMemoryTask, function () {
let tsProject = ts.createProject(tsconfigPath, {
declaration: true,
});
let tsResults = gulp.src(tsAndTestsSrc.concat(externalTypesSrc), { base: packageSourceDir })
.pipe(sourcemaps.init())
.pipe(ts(tsProject));
dtsStream = tsResults.dts
;
jsStream = tsResults.js
.pipe(sourcemaps.write())
;
});
gulp.task(compileTsTask, [compileToMemoryTask, cleanTask], function () {
return merge2([
jsStream.pipe(gulp.dest(outputDir)),
dtsStream.pipe(gulp.dest(outputDir))]);
});
// gulp.task('watch ts', function () {
// // TODO: Only compile changed files.
// gulp.watch('**/*.ts', [compileTsTask]);
// });
gulp.task(injectTestsToPageTask, function () {
var token = "// Unit tests token";
var packageNameToken = "<package name token>";
var scripts = [];
var isFirst = true;
gulp.src(unitTestsSrc)
.pipe(tap(function (file) {
var relativePath = path.relative(packageSourceDir, file.path);
var relativePathToJs = "./" + relativePath.replace(/\\/g, '/').replace(".ts", ".js");
var scriptToTest = scripts.length == 0 ?
"System.import(\"" + relativePathToJs + "\")" :
".then(()=>System.import(\"" + relativePathToJs + "\"))";
scripts.push(scriptToTest);
})).on('end', function () {
gulp.src('src/unit-tests.html')
.pipe(tap(function (file) {
file.contents = new Buffer(file.contents.toString()
.replace(token, scripts.join('\n')));
}))
.pipe(gulp.dest(outputDir));
});
});
gulp.task('start web server', function () {
var server = gulpServer.static("./", 8888);
server.start();
// gulp.watch([outputDir + '/**/*'], function (file) {
// server.notify.apply(server, [file]);
// });
});