]> git.proxmox.com Git - mirror_xterm.js.git/blob - gulpfile.js
Merge pull request #535 from Tyriar/534_phantomjs
[mirror_xterm.js.git] / gulpfile.js
1 const browserify = require('browserify');
2 const buffer = require('vinyl-buffer');
3 const fs = require('fs-extra');
4 const gulp = require('gulp');
5 const merge = require('merge-stream');
6 const mocha = require('gulp-mocha');
7 const mochaPhantomJs = require('gulp-mocha-phantomjs');
8 const sorcery = require('sorcery');
9 const source = require('vinyl-source-stream');
10 const sourcemaps = require('gulp-sourcemaps');
11 const ts = require('gulp-typescript');
12
13
14 let buildDir = process.env.BUILD_DIR || 'build';
15
16
17 /**
18 * Compile TypeScript sources to JavaScript files and create a source map file for each TypeScript
19 * file compiled.
20 */
21 gulp.task('tsc', function () {
22 // Remove the lib/ directory to prevent confusion if files were deleted in src/
23 fs.emptyDirSync('lib');
24
25 // Build all TypeScript files (including tests) to lib/, based on the configuration defined in
26 // `tsconfig.json`.
27 let tsProject = ts.createProject('tsconfig.json');
28 let tsResult = tsProject.src().pipe(sourcemaps.init()).pipe(tsProject());
29 let tsc = tsResult.js.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})).pipe(gulp.dest('lib'));
30
31 // Copy all addons from src/ to lib/
32 let copyAddons = gulp.src('src/addons/**/*').pipe(gulp.dest('lib/addons'));
33
34 // Copy stylesheets from src/ to lib/
35 let copyStylesheets = gulp.src('src/**/*.css').pipe(gulp.dest('lib'));
36
37 return merge(tsc, copyAddons, copyStylesheets);
38 });
39
40 /**
41 * Bundle JavaScript files produced by the `tsc` task, into a single file named `xterm.js` with
42 * Browserify.
43 */
44 gulp.task('browserify', ['tsc'], function() {
45 // Ensure that the build directory exists
46 fs.ensureDirSync(buildDir);
47
48 let browserifyOptions = {
49 basedir: buildDir,
50 debug: true,
51 entries: ['../lib/xterm.js'],
52 standalone: 'Terminal',
53 cache: {},
54 packageCache: {}
55 };
56 let bundleStream = browserify(browserifyOptions)
57 .bundle()
58 .pipe(source('xterm.js'))
59 .pipe(buffer())
60 .pipe(sourcemaps.init({loadMaps: true, sourceRoot: '..'}))
61 .pipe(sourcemaps.write('./'))
62 .pipe(gulp.dest(buildDir));
63
64 // Copy all add-ons from lib/ to buildDir
65 let copyAddons = gulp.src('lib/addons/**/*').pipe(gulp.dest(`${buildDir}/addons`));
66
67 // Copy stylesheets from src/ to lib/
68 let copyStylesheets = gulp.src('lib/**/*.css').pipe(gulp.dest(buildDir));
69
70 return merge(bundleStream, copyAddons, copyStylesheets);
71 });
72
73 gulp.task('test-mocha', function () {
74 return gulp.src(['lib/*test.js', 'lib/**/*test.js'], {read: false})
75 .pipe(mocha())
76 });
77
78 gulp.task('test-mocha-phantomjs', function () {
79 return gulp.src('test-harness.html')
80 .pipe(mochaPhantomJs());
81 });
82
83 /**
84 * Use `sorcery` to resolve the source map chain and point back to the TypeScript files.
85 * (Without this task the source maps produced for the JavaScript bundle points into the
86 * compiled JavaScript files in lib/).
87 */
88 gulp.task('sorcery', ['browserify'], function () {
89 var chain = sorcery.loadSync(`${buildDir}/xterm.js`);
90 var map = chain.apply();
91 chain.writeSync();
92 });
93
94 gulp.task('build', ['sorcery']);
95 gulp.task('test', ['test-mocha', 'test-mocha-phantomjs']);
96 gulp.task('default', ['build']);