]> git.proxmox.com Git - mirror_xterm.js.git/blob - gulpfile.js
Create `terminal.buffer` convenience attribute
[mirror_xterm.js.git] / gulpfile.js
1 /**
2 * @license MIT
3 */
4
5 const browserify = require('browserify');
6 const buffer = require('vinyl-buffer');
7 const coveralls = require('gulp-coveralls');
8 const fs = require('fs-extra');
9 const gulp = require('gulp');
10 const istanbul = require('gulp-istanbul');
11 const merge = require('merge-stream');
12 const mocha = require('gulp-mocha');
13 const sorcery = require('sorcery');
14 const source = require('vinyl-source-stream');
15 const sourcemaps = require('gulp-sourcemaps');
16 const ts = require('gulp-typescript');
17
18
19 let buildDir = process.env.BUILD_DIR || 'build';
20 let tsProject = ts.createProject('tsconfig.json');
21 let tsProjectSearchAddon = ts.createProject('./src/addons/search/tsconfig.json');
22 let srcDir = tsProject.config.compilerOptions.rootDir;
23 let outDir = tsProject.config.compilerOptions.outDir;
24
25 /**
26 * Compile TypeScript sources to JavaScript files and create a source map file for each TypeScript
27 * file compiled.
28 */
29 gulp.task('tsc', function () {
30 // Remove the ${outDir}/ directory to prevent confusion if files were deleted in ${srcDir}/
31 fs.emptyDirSync(`${outDir}`);
32
33 // Build all TypeScript files (including tests) to ${outDir}/, based on the configuration defined in
34 // `tsconfig.json`.
35 let tsResult = tsProject.src().pipe(sourcemaps.init()).pipe(tsProject());
36 let tsc = tsResult.js.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})).pipe(gulp.dest(outDir));
37
38 fs.emptyDirSync(`${outDir}/addons/search`);
39 let tsResultSearchAddon = tsProjectSearchAddon.src().pipe(sourcemaps.init()).pipe(tsProjectSearchAddon());
40 let tscSearchAddon = tsResultSearchAddon.js.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})).pipe(gulp.dest(`${outDir}/addons/search`));
41
42 // Copy all addons from ${srcDir}/ to ${outDir}/
43 let copyAddons = gulp.src([`${srcDir}/addons/**/*`, `!${srcDir}/addons/search`, `!${srcDir}/addons/search/**`]).pipe(gulp.dest(`${outDir}/addons`));
44
45 // Copy stylesheets from ${srcDir}/ to ${outDir}/
46 let copyStylesheets = gulp.src(`${srcDir}/**/*.css`).pipe(gulp.dest(outDir));
47
48 return merge(tsc, tscSearchAddon, copyAddons, copyStylesheets);
49 });
50
51 /**
52 * Bundle JavaScript files produced by the `tsc` task, into a single file named `xterm.js` with
53 * Browserify.
54 */
55 gulp.task('browserify', ['tsc'], function() {
56 // Ensure that the build directory exists
57 fs.ensureDirSync(buildDir);
58
59 let browserifyOptions = {
60 basedir: buildDir,
61 debug: true,
62 entries: [`${outDir}/xterm.js`],
63 standalone: 'Terminal',
64 cache: {},
65 packageCache: {}
66 };
67 let bundleStream = browserify(browserifyOptions)
68 .bundle()
69 .pipe(source('xterm.js'))
70 .pipe(buffer())
71 .pipe(sourcemaps.init({loadMaps: true, sourceRoot: '..'}))
72 .pipe(sourcemaps.write('./'))
73 .pipe(gulp.dest(buildDir));
74
75 // Copy stylesheets from ${outDir}/ to ${buildDir}/
76 let copyStylesheets = gulp.src(`${outDir}/**/*.css`).pipe(gulp.dest(buildDir));
77
78 return merge(bundleStream, copyStylesheets);
79 });
80
81 gulp.task('browserify-addons', ['tsc'], function() {
82 let searchOptions = {
83 basedir: `${buildDir}/addons/search`,
84 debug: true,
85 entries: [`${outDir}/addons/search/search.js`],
86 cache: {},
87 packageCache: {}
88 };
89 let searchBundle = browserify(searchOptions)
90 .bundle()
91 .pipe(source('./addons/search/search.js'))
92 .pipe(buffer())
93 .pipe(sourcemaps.init({loadMaps: true, sourceRoot: ''}))
94 .pipe(sourcemaps.write('./'))
95 .pipe(gulp.dest(buildDir));
96
97 // Copy all add-ons from outDir to buildDir
98 let copyAddons = gulp.src([
99 // Copy JS addons
100 `${outDir}/addons/**/*`,
101 // Exclude TS addons from copy as they are being built via browserify
102 `!${outDir}/addons/search`,
103 `!${outDir}/addons/search/**`
104 ]).pipe(gulp.dest(`${buildDir}/addons`));
105
106 return merge(searchBundle, copyAddons);
107 });
108
109 gulp.task('instrument-test', function () {
110 return gulp.src([`${outDir}/**/*.js`])
111 // Covering files
112 .pipe(istanbul())
113 // Force `require` to return covered files
114 .pipe(istanbul.hookRequire());
115 });
116
117 gulp.task('mocha', ['instrument-test'], function () {
118 return gulp.src([`${outDir}/*test.js`, `${outDir}/**/*test.js`], {read: false})
119 .pipe(mocha())
120 .once('error', () => process.exit(1))
121 .pipe(istanbul.writeReports());
122 });
123
124 /**
125 * Use `sorcery` to resolve the source map chain and point back to the TypeScript files.
126 * (Without this task the source maps produced for the JavaScript bundle points into the
127 * compiled JavaScript files in ${outDir}/).
128 */
129 gulp.task('sorcery', ['browserify'], function () {
130 var chain = sorcery.loadSync(`${buildDir}/xterm.js`);
131 chain.apply();
132 chain.writeSync();
133 });
134
135 gulp.task('sorcery-addons', ['browserify-addons'], function () {
136 var chain = sorcery.loadSync(`${buildDir}/addons/search/search.js`);
137 chain.apply();
138 chain.writeSync();
139 });
140
141 /**
142 * Submit coverage results to coveralls.io
143 */
144 gulp.task('coveralls', function () {
145 gulp.src('coverage/**/lcov.info')
146 .pipe(coveralls());
147 });
148
149 gulp.task('build', ['sorcery', 'sorcery-addons']);
150 gulp.task('test', ['mocha']);
151 gulp.task('default', ['build']);