redoc/build/tasks/build.js

193 lines
6.0 KiB
JavaScript
Raw Normal View History

2015-10-03 11:54:09 +03:00
var gulp = require('gulp');
var runSequence = require('run-sequence');
var Builder = require('systemjs-builder');
var inlineNg2Template = require('gulp-inline-ng2-template');
2015-10-03 11:54:09 +03:00
var path = require('path');
2015-10-14 23:59:39 +03:00
var sourcemaps = require('gulp-sourcemaps');
2015-10-03 11:54:09 +03:00
var paths = require('../paths');
var fs= require('fs');
2015-10-14 23:59:39 +03:00
var concat = require('gulp-concat');
2015-11-22 14:49:31 +03:00
var gulp = require('gulp');
var sass = require('gulp-sass');
var replace = require('gulp-replace');
2016-01-06 18:21:33 +03:00
var rename = require('gulp-rename');
2016-05-18 22:18:06 +03:00
var argv = require('yargs').argv;
2016-05-20 20:12:54 +03:00
var gulpIf = require('gulp-if');
var sassCopm = require('node-sass');
2016-06-16 15:44:14 +03:00
var ts = require('typescript');
2015-10-03 11:54:09 +03:00
gulp.task('build', function (callback) {
2016-05-18 22:18:06 +03:00
if (argv.skipRebuild) {
console.log('>>> Rebuild skipped')
return callback();
}
2015-10-03 11:54:09 +03:00
return runSequence(
2016-06-06 19:32:20 +03:00
'clean',
2016-06-23 11:34:49 +03:00
'transpile',
2016-01-09 23:23:49 +03:00
'bundle',
2016-05-18 22:18:06 +03:00
'concatDeps',
2016-05-20 20:51:31 +03:00
'copyDebug',
2016-01-09 23:23:49 +03:00
callback
);
});
2016-06-23 11:34:49 +03:00
gulp.task('transpile', function(cb) {
return runSequence(
'tsc',
'inlineTemplates',
cb
);
});
2016-05-20 20:51:31 +03:00
gulp.task('copyDebug', () => {
if (!argv.prod) {
// copy for be accessible from demo for debug
cp(paths.redocBuilt + '.js', paths.redocBuilt + '.min.js');
}
});
2016-06-06 19:32:20 +03:00
gulp.task('tsc', function() {
exec('tsc -p ./tsconfig.json');
});
2016-06-16 15:44:14 +03:00
// function compileTs(files, es5) {
// var tsProject = ts.createProject('tsconfig.json');
// var allFiles = [].concat(files, ['typings/**/*.d.ts']);
// var res = gulp.src(allFiles, {
// base: config.src,
// outDir: config.tmp
// })
// .pipe(tslint())
// .pipe(tslint.report('prose', {
// summarizeFailureOutput: true,
// emitError: !watchMode
// }))
// .pipe(sourcemaps.init())
// .pipe(ts(tsProject))
// .on('error', function () {
// if (watchMode) {
// return;
// }
// process.exit(1);
// });
// return res.js
// .pipe(sourcemaps.write('.', {
// includeContent: inline
// }))
// .pipe(gulp.dest(config.tmp));
// }
2016-06-19 17:40:45 +03:00
gulp.task('inlineTemplates', ['sass'], function() {
2016-06-12 20:44:34 +03:00
return gulp.src('.tmp/**/*.js', { base: './tmp' })
.pipe(replace(/'(.*?)\.css'/g, '\'$1.scss\''))
2016-06-06 19:32:20 +03:00
.pipe(inlineNg2Template({
base: './',
useRelativePaths: true,
2016-06-06 19:32:20 +03:00
styleProcessor: compileSass,
customFilePath: function(ext, file) {
var cwd = process.cwd();
var relative = path.relative(cwd, file);
relative = relative.substring('5');
return path.join(cwd, relative);
}
}))
.pipe(gulp.dest(paths.tmp));
});
2016-06-23 11:34:49 +03:00
function compileSass(ext, file, cb) {
file = file.replace('../../shared/styles/variables', 'lib/shared/styles/variables');
file = file.replace('json-schema-common', 'lib/components/JsonSchema/json-schema-common');
file = file.replace('../../shared/styles/share-link', 'lib/shared/styles/share-link');
file = file.replace('../JsonSchema/lib/components/JsonSchema/json-schema-common', 'lib/components/JsonSchema/json-schema-common');
file = file.replace('../../styles/variables', 'lib/shared/styles/variables');
2016-06-06 19:32:20 +03:00
2016-06-23 11:34:49 +03:00
cb(null, sassCopm.renderSync({data: file}).css);
}
2016-05-18 22:18:06 +03:00
var JS_DEPS = argv.prod ? [
2016-05-20 19:27:47 +03:00
'lib/utils/browser-update.js',
'node_modules/zone.js/dist/zone.min.js',
'node_modules/reflect-metadata/Reflect.js',
'node_modules/babel-polyfill/dist/polyfill.min.js'
]: [
'lib/utils/browser-update.js',
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
2016-01-17 14:51:02 +03:00
'node_modules/reflect-metadata/Reflect.js',
'node_modules/babel-polyfill/dist/polyfill.js'
2016-05-18 22:18:06 +03:00
];
var outputFileName = paths.redocBuilt + (argv.prod ? '.min.js' : '.js');
2015-11-22 14:49:31 +03:00
gulp.task('sass', function () {
2015-11-22 15:08:28 +03:00
return gulp.src(paths.scss, { base: './' })
2016-01-23 15:19:22 +03:00
.pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
2015-11-22 14:49:31 +03:00
.pipe(gulp.dest(paths.tmp));
});
2015-10-14 23:59:39 +03:00
// concatenate angular2 deps
gulp.task('concatDeps', ['concatPrism'], function() {
return gulp.src(JS_DEPS.concat([path.join(paths.tmp, 'prismjs-bundle.js'), outputFileName]))
2016-05-20 20:12:54 +03:00
.pipe(gulpIf(!argv.prod, sourcemaps.init({loadMaps: true})))
2016-05-18 22:18:06 +03:00
.pipe(concat(outputFileName))
2016-05-20 20:12:54 +03:00
.pipe(gulpIf(!argv.prod, sourcemaps.write('.')))
2016-05-18 22:18:06 +03:00
.pipe(gulp.dest('.'))
});
gulp.task('bundle', ['injectVersionFile'], function bundle(done) {
2016-06-06 19:32:20 +03:00
mkdir('-p', 'dist');
cp('lib/index.js', path.join(paths.tmp, paths.sourceEntryPoint));
2015-10-03 11:54:09 +03:00
var builder = new Builder('./', 'system.config.js');
2015-10-26 22:34:32 +03:00
2015-10-03 11:54:09 +03:00
builder
.buildStatic(path.join(paths.tmp, paths.sourceEntryPoint),
2016-05-18 22:18:06 +03:00
outputFileName,
{ format:'umd', sourceMaps: !argv.prod, lowResSourceMaps: true, minify: argv.prod }
2015-10-07 10:18:02 +03:00
)
2016-05-20 20:51:31 +03:00
.then(() => {
// wait some time to allow flush
2016-05-20 20:51:31 +03:00
setTimeout(() => done(), 500);
2015-10-03 11:54:09 +03:00
})
2016-05-20 20:51:31 +03:00
.catch(err => done(err));
2016-05-18 22:18:06 +03:00
});
gulp.task('concatPrism', function() {
require('../../system.config.js');
var prismFolder = System.normalizeSync('prismjs').substring(8);
prismFolder = prismFolder.substring(0, prismFolder.length -3);
var prismFiles = [
'prism.js',
'components/prism-actionscript.js',
'components/prism-c.js',
'components/prism-cpp.js',
'components/prism-csharp.js',
'components/prism-php.js',
'components/prism-coffeescript.js',
'components/prism-go.js',
'components/prism-haskell.js',
'components/prism-java.js',
'components/prism-lua.js',
'components/prism-matlab.js',
'components/prism-perl.js',
'components/prism-python.js',
'components/prism-r.js',
'components/prism-ruby.js',
'components/prism-bash.js',
'components/prism-swift.js',
'components/prism-objectivec.js',
'components/prism-scala.js'
].map(file => path.join(prismFolder, file));
2016-05-18 22:18:06 +03:00
return gulp.src(prismFiles)
.pipe(concat(path.join(paths.tmp, 'prismjs-bundle.js')))
2016-06-06 19:32:20 +03:00
.pipe(gulp.dest('.'));
});
2016-05-20 20:36:10 +03:00
// needs inlineTemplates run before to create .tmp/lib folder
2016-06-23 11:34:49 +03:00
gulp.task('injectVersionFile', function() {
2016-05-20 20:36:10 +03:00
var version = require('../../package.json').version;
var exportStatement = `export var redocVersion = "${version}"`;
fs.writeFileSync(path.join(paths.tmp, 'lib/version.js'), exportStatement);
2016-05-20 20:36:10 +03:00
})