2016-04-08 05:17:18 +02:00
|
|
|
const gulp = require('gulp');
|
|
|
|
const babel = require('gulp-babel');
|
|
|
|
const notify = require('gulp-notify');
|
|
|
|
const sass = require('gulp-sass');
|
|
|
|
const sourcemaps = require('gulp-sourcemaps');
|
|
|
|
const uglify = require('gulp-uglify');
|
|
|
|
const gulpUtil = require('gulp-util');
|
|
|
|
const browserify = require('browserify');
|
|
|
|
const babelify = require('babelify');
|
|
|
|
const watchify = require('watchify');
|
|
|
|
const source = require('vinyl-source-stream');
|
|
|
|
const buffer = require('vinyl-buffer');
|
|
|
|
const path = require('path');
|
|
|
|
const glob = require('glob');
|
|
|
|
const eventStream = require('event-stream');
|
|
|
|
const semver = require('semver');
|
|
|
|
const packageJson = require('./package.json');
|
2016-02-14 23:42:37 +01:00
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
const isDev = typeof process.env.npm_config_development !== 'undefined';
|
|
|
|
process.env.NODE_ENV = isDev ? 'development' : 'production';
|
2016-01-26 05:28:36 +01:00
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
const PATHS = {
|
|
|
|
MODULES: './node_modules',
|
2016-04-19 13:22:18 +02:00
|
|
|
CMS_JS_SRC: './client/src',
|
|
|
|
CMS_JS_DIST: './client/dist/js',
|
|
|
|
CMS_CSS_SRC: './client/src/styles',
|
|
|
|
CMS_CSS_DIST: './client/dist/styles',
|
2016-01-26 05:28:36 +01:00
|
|
|
};
|
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
const babelifyOptions = {
|
|
|
|
presets: ['es2015', 'es2015-ie', 'react'],
|
|
|
|
plugins: ['transform-object-assign'],
|
|
|
|
ignore: /(node_modules|thirdparty)/,
|
|
|
|
comments: false,
|
2016-04-04 23:26:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const browserifyOptions = {};
|
|
|
|
if (isDev) {
|
|
|
|
browserifyOptions.debug = true;
|
|
|
|
browserifyOptions.cache = {};
|
|
|
|
browserifyOptions.packageCache = {};
|
|
|
|
browserifyOptions.plugin = [watchify];
|
|
|
|
}
|
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
/**
|
|
|
|
* Transforms the passed JavaScript files to UMD modules.
|
|
|
|
*
|
|
|
|
* @param array files - The files to transform.
|
|
|
|
* @param string dest - The output directory.
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
function transformToUmd(files, dest) {
|
2016-04-08 05:17:18 +02:00
|
|
|
return eventStream.merge(files.map((file) => { // eslint-disable-line arrow-body-style
|
|
|
|
return gulp.src(file)
|
|
|
|
.pipe(babel({
|
|
|
|
presets: ['es2015'],
|
|
|
|
moduleId: `ss.${path.parse(file).name}`,
|
|
|
|
plugins: ['transform-es2015-modules-umd'],
|
|
|
|
comments: false,
|
|
|
|
}))
|
|
|
|
.on('error', notify.onError({
|
|
|
|
message: 'Error: <%= error.message %>',
|
|
|
|
}))
|
|
|
|
.pipe(gulp.dest(dest));
|
|
|
|
}));
|
2016-01-26 05:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the version of Node being used is valid.
|
|
|
|
if (!semver.satisfies(process.versions.node, packageJson.engines.node)) {
|
2016-04-08 05:17:18 +02:00
|
|
|
console.error( // eslint-disable-line no-console
|
|
|
|
`Invalid Node.js version. You need to be using ${packageJson.engines.node}` +
|
|
|
|
'. If you want to manage multiple Node.js versions try https://github.com/creationix/nvm'
|
|
|
|
);
|
|
|
|
process.exit(1);
|
2016-01-26 05:28:36 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 20:33:04 +01:00
|
|
|
gulp.task('build', ['umd-cms', 'umd-watch', 'bundle-legacy']);
|
2016-01-26 05:28:36 +01:00
|
|
|
|
2016-02-11 20:33:04 +01:00
|
|
|
gulp.task('bundle-legacy', function bundleLeftAndMain() {
|
2016-04-08 05:17:18 +02:00
|
|
|
const bundleFileName = 'bundle-legacy.js';
|
2016-01-26 05:28:36 +01:00
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
return browserify(Object.assign(
|
|
|
|
{},
|
|
|
|
browserifyOptions,
|
2016-04-19 13:22:18 +02:00
|
|
|
{ entries: `${PATHS.CMS_JS_SRC}/bundles/legacy.js` })
|
2016-04-08 05:17:18 +02:00
|
|
|
)
|
|
|
|
.on('update', bundleLeftAndMain)
|
|
|
|
.on('log', (msg) => gulpUtil.log('Finished', `bundled ${bundleFileName} ${msg}`))
|
|
|
|
.transform(babelify, babelifyOptions)
|
|
|
|
.external('jQuery')
|
|
|
|
.external('i18n')
|
|
|
|
.external('router')
|
|
|
|
.bundle()
|
|
|
|
.on('error', notify.onError({ message: `${bundleFileName}: <%= error.message %>` }))
|
|
|
|
.pipe(source(bundleFileName))
|
|
|
|
.pipe(buffer())
|
|
|
|
.pipe(sourcemaps.init({ loadMaps: true }))
|
|
|
|
.pipe(uglify())
|
|
|
|
.pipe(sourcemaps.write('./'))
|
2016-04-19 13:22:18 +02:00
|
|
|
.pipe(gulp.dest(PATHS.CMS_JS_DIST));
|
2016-01-26 05:28:36 +01:00
|
|
|
});
|
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
gulp.task('umd-cms', () => { // eslint-disable-line
|
2016-04-19 13:22:18 +02:00
|
|
|
return transformToUmd(glob.sync(`${PATHS.CMS_JS_SRC}/*.js`), PATHS.CMS_JS_DIST);
|
2016-01-26 05:28:36 +01:00
|
|
|
});
|
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
gulp.task('umd-watch', () => { // eslint-disable-line
|
|
|
|
if (isDev) {
|
2016-04-19 13:22:18 +02:00
|
|
|
gulp.watch(`${PATHS.CMS_JS_SRC}/*.js`, ['umd-cms']);
|
2016-04-08 05:17:18 +02:00
|
|
|
}
|
2016-01-26 05:28:36 +01:00
|
|
|
});
|
2016-02-14 23:42:37 +01:00
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
gulp.task('css', ['compile:css'], () => { // eslint-disable-line
|
2016-02-14 23:42:37 +01:00
|
|
|
if (isDev) {
|
2016-04-19 13:22:18 +02:00
|
|
|
gulp.watch(`${PATHS.CMS_CSS_SRC}/**/*.scss`, ['compile:css']);
|
|
|
|
gulp.watch(`${PATHS.CMS_JS_SRC}/**/*.scss`, ['compile:css']);
|
2016-02-14 23:42:37 +01:00
|
|
|
}
|
2016-04-08 05:17:18 +02:00
|
|
|
});
|
2016-02-14 23:42:37 +01:00
|
|
|
|
2016-04-08 05:17:18 +02:00
|
|
|
gulp.task('compile:css', () => { // eslint-disable-line
|
|
|
|
const outputStyle = isDev ? 'expanded' : 'compressed';
|
2016-02-14 23:42:37 +01:00
|
|
|
|
2016-04-19 13:22:18 +02:00
|
|
|
return gulp.src(`${PATHS.CMS_CSS_SRC}/**/*.scss`)
|
2016-04-08 05:17:18 +02:00
|
|
|
.pipe(sourcemaps.init())
|
|
|
|
.pipe(sass({ outputStyle })
|
|
|
|
.on('error', notify.onError({
|
|
|
|
message: 'Error: <%= error.message %>',
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
.pipe(sourcemaps.write())
|
2016-04-19 13:22:18 +02:00
|
|
|
.pipe(gulp.dest(PATHS.CMS_CSS_DIST));
|
2016-02-14 23:42:37 +01:00
|
|
|
});
|