mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
83 lines
2.4 KiB
JavaScript
83 lines
2.4 KiB
JavaScript
/*jshint onevar:false*/
|
|
|
|
var fs = require('fs'),
|
|
uglifyjs = require('uglify-js');
|
|
|
|
|
|
module.exports = function (grunt) {
|
|
|
|
var helpers = require('grunt-lib-legacyhelpers').init(grunt);
|
|
|
|
var START = [
|
|
"(function(){",
|
|
" function onload (moment) {",
|
|
""
|
|
].join('\n');
|
|
|
|
var END = [
|
|
"",
|
|
" }",
|
|
" if (typeof define === \"function\" && define.amd) {",
|
|
" define([\"moment\"], onload);",
|
|
" }",
|
|
" if (typeof window !== \"undefined\" && window.moment) {",
|
|
" onload(window.moment);",
|
|
" }",
|
|
"})()",
|
|
""
|
|
].join('\n');
|
|
|
|
// UglifyJS does not support keeping the first line comments unless using the CLI.
|
|
// This multi-task ensures that the first comments are kept.
|
|
grunt.registerMultiTask('minlang', 'Minify lang files with UglifyJS.', function () {
|
|
var files = grunt.file.expand(this.data.src),
|
|
min,
|
|
code,
|
|
comments,
|
|
tok,
|
|
result;
|
|
|
|
// Concat specified files. This should really be a single, pre-built (and
|
|
// linted) file, but it supports any number of files.
|
|
code = helpers.concat(files, {separator: this.data.separator});
|
|
|
|
// Add the first comments
|
|
//tok = uglifyjs.parse(code);
|
|
tok = uglifyjs.parse(code);
|
|
min = showCopyright(tok.start.comments_before);
|
|
|
|
// Add the minified source.
|
|
result = uglifyjs.minify(wrapFile(code), grunt.config('uglify.options'));
|
|
min += result.code;
|
|
grunt.file.write(this.data.dest, min);
|
|
|
|
// Fail task if errors were logged.
|
|
if (this.errorCount) { return false; }
|
|
|
|
// Otherwise, print a success message....
|
|
grunt.log.writeln('File "' + this.data.dest + '" created.');
|
|
|
|
// ...and report some size information.
|
|
helpers.min_max_info(min, code);
|
|
});
|
|
|
|
// Helper for the 'mincomment' multitask
|
|
function showCopyright(comments) {
|
|
var ret = "", i, c;
|
|
for (i = 0; i < comments.length; ++i) {
|
|
c = comments[i];
|
|
if (c.type === "comment1") {
|
|
ret += "//" + c.value + "\n";
|
|
} else {
|
|
ret += "/*" + c.value + "*/";
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
function wrapFile(code) {
|
|
code = code.replace(/require\([\'\"]\.\.\/moment[\'\"]\)/g, "moment");
|
|
return START + code + END;
|
|
}
|
|
};
|