Skip to content

Instantly share code, notes, and snippets.

@nhusher
Last active December 28, 2015 03:29
Show Gist options
  • Save nhusher/7435480 to your computer and use it in GitHub Desktop.
Save nhusher/7435480 to your computer and use it in GitHub Desktop.
Builds a custom rollup version of the YUI project that is tailored to the needs of your YUI3 application.
/*jslint node:true */
module.exports = function (grunt) {
var YUI = require('yui').YUI,
path = require('path'),
yuiVersion = require('../../package.json').dependencies.yui;
grunt.registerMultiTask('yui-rollup', 'Creates a YUI library rollup.', function () {
var Y = YUI(),
loader,
reqs,
jsRollup = [],
cssRollup = [],
data = this.data,
filter = this.data.filter || 'min',
outFileName = data.name + '-' + filter,
suffix = '/**/meta/*.json',
srcs = Array.isArray(data.src) ? data.src.map(function(src) {
return path.join(src, suffix);
}) : path.join(data.src, suffix),
metas = grunt.file.expand(srcs),
added = this.data.added || [],
dependencies = [];
if(Y.version !== yuiVersion) {
grunt.verbose.error("Expected YUI [" + yuiVersion + "], found [" + Y.version + "]");
grunt.fail.fatal("Cannot build YUI rollup. The YUI dependency is out-of-date!");
}
function parseDependencies(o, d) {
var property, submod, value;
for(property in o) {
value = o[property];
if(property === 'submodules') {
for(submod in value) {
parseDependencies(value[submod], d);
}
} else if(property === 'use' || property === 'requires') {
d.push.apply(d, value);
}
}
}
metas.forEach(function(meta) {
var metaFile = grunt.file.readJSON(meta),
key;
for(key in metaFile) {
parseDependencies(metaFile[key], dependencies);
}
});
// Force the environment to think it's IE9, Safari, Chrome, etc.
Y.UA = { ie: 9, chrome: 1, gecko: 1, safari: 1, webkit: 1 };
// We need to mock some BOM properties to make some of the feature checks pass.
Y.config.win = {
onhashchange: true,
getComputedStyle: true
};
Y.config.doc = {
documentMode: 9,
documentElement: {
style: {
opacity: 1
}
},
implementation: {
hasFeature: function() {
return true;
}
}
};
loader = new Y.Loader({
lang: 'en',
base: './node_modules/yui/',
ignoreRegistered: true,
filter: filter.toUpperCase(),
require: dependencies.concat(added),
modules:{
'querystring-stringify':{ supersedes:[ 'querystring-stringify-simple' ] }
},
ignore: []
});
reqs = loader.resolve(true);
// console.log(reqs);
grunt.verbose.writeln("YUI modules: " + (reqs.jsMods.map(function(it) { return it.name; }).join(', ')));
grunt.verbose.writeln("YUI CSS: " + (reqs.cssMods.map(function(it) { return it.name; }).join(', ')));
reqs.js.forEach(function (file) {
var data = grunt.file.read(file);
jsRollup.push(data);
});
reqs.css.forEach(function (file) {
var data = grunt.file.read(file);
cssRollup.push(data);
});
grunt.file.write(path.join(data.dest, outFileName + '.js' ), jsRollup.join('\n\n'));
grunt.file.write(path.join(data.dest, outFileName + '.css' ), cssRollup.join('\n\n'));
});
};
/*jslint node:true*/
module.exports = function (grunt) {
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadTasks('scripts/grunt');
// Project configuration.
grunt.initConfig({
// Use the YUI loader server-side to generate a rollup of the required modules
"yui-rollup": {
"min": {
"version": "CRM",
"added" : [ 'loader', 'autocomplete-list', 'io-form', 'yui-throttle' ],
"src" : "apps/ui/web-app/js/crm-src",
"dest" : "apps/ui/web-app/js/crm-build",
"name" : "yui-rollup"
},
"debug": {
// The name of the output file
"name" : "yui-rollup",
// (optional) filter - the filter param to pass to the YUI loader.
"filter" : "debug",
// (optional) added - a list of YUI modules to load in addition to the modules it decides to load
"added" : [ 'loader', 'autocomplete-list', 'io-form', 'yui-throttle' ],
// The source directory. Minimatch parameters accepted
"src" : "apps/ui/web-app/js/crm-src",
// The destination directory. Creates a file called "yui-rollup-{filter}.js"
"dest" : "apps/ui/web-app/js/crm-build"
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment