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