Skip to content

Instantly share code, notes, and snippets.

@FcoJavierSainz
Last active March 24, 2016 17:34
Show Gist options
  • Select an option

  • Save FcoJavierSainz/bdf8619c190cfe3e671a to your computer and use it in GitHub Desktop.

Select an option

Save FcoJavierSainz/bdf8619c190cfe3e671a to your computer and use it in GitHub Desktop.
Create Kendo UI build for RequireJS backward compatibility
/* jshint browser:false, node:true, esnext: true */
var gulp = require('gulp');
var debug = require('gulp-debug'); // jshint ignore:line
var logger = require('gulp-logger');
var sourcemaps = require('gulp-sourcemaps');
var runSequence = require('run-sequence');
var lazypipe = require('lazypipe');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var replace = require('gulp-replace');
// uglify
var compress = {
unsafe: true,
hoist_vars: true,
warnings: false,
pure_getters: true
};
var mangle = {
except: ['define']
};
//avoid to put jquery.min as a dependency of kendo.core, leave as jquery only
var jqueryProvided = true;
//the RequireJS kendo path
//require.config({
// path: {
// ...
// kendo: 'path/to/kendo/js'
// ...
// }
//});
var kendoRequireJsPath = 'kendo';
function renameModules(match) {
//define does not match with a AMD module definition, like kendo.data has a define function
if (!match.match(/define\((['"]([\w\.\/-]+)?['"]\s*,)?\[(['"]([\w\.\/-]+)?['"],?)+/)) {
return match;
}
//change module name, we must to put a complete name path+module name, because now there are multiples modules in one file, like kendo.grid.js
if (match.match(/define\((['"]([\w\.\/-]+)?['"]\s*,)\[/)) {
match = match.replace(/['"]([\w\.\/-]+)?['"]\s*/, '"' + kendoRequireJsPath + '/$1.min"');
}
//ignore jquery reference <- kendo.core
if (jqueryProvided && match.indexOf('jquery') > -1) {
return match;
}
//change dependencies path, we must to put a complete name path+module name, because now there are multiples modules in one file, like kendo.grid.js
var parts = match.split(/[\[\]]/);
var dependencies = parts[1];
dependencies = dependencies.split(/['"]([\w\.\/-]+)['"],?/).filter(function (el) {
return el.length != 0
});
dependencies.forEach(function (dependency, index, array) {
array[index] = '"' + kendoRequireJsPath + '/' + dependency + '.min"';
});
return parts[0] + '[' + dependencies.join(',') + ']' + parts[2];
}
uglify = lazypipe()
.pipe(logger, { after: 'uglify complete', extname: '.min.js', showChange: true })
.pipe(uglify, { compress: compress, mangle: mangle, preserveComments: 'license' })
.pipe(replace, /define\(.+?\]/g, renameModules)
.pipe(rename, { suffix: '.min' });
gulp.task('requirejs-backward-compatibility-modules', function () {
return gulp.src(['js/*.js', '!js/jquery.js'], { base: 'js' })
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(logger({ after: 'source map complete!', extname: '.map', showChange: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist/js'));
});
gulp.task('requirejs-backward-compatibility-messages-cultures', function () {
return gulp.src(['js/cultures/*.js', 'js/messages/*.js'], { base: 'js' })
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(logger({ after: 'source map complete!', extname: '.map', showChange: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist/js'));
});
gulp.task('copy-third-party-files', function () {
return gulp.src(['../js/kendo.angular.js',
'../js/angular.min.js',
'../js/jquery.min.js',
'../js/jquery.min.map',
'../js/jszip.min.js'])
.pipe(gulp.dest('dist/js'));
});
gulp.task('default', ['copy-third-party-files'], function (callback) {
runSequence(
['requirejs-backward-compatibility-modules'],
['requirejs-backward-compatibility-messages-cultures'],
callback
);
});
@FcoJavierSainz
Copy link
Author

Change kendoRequireJsPath variable with the value of kendo path on require.config, put it on src folder of kendo distribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment