gruntfile.js 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';
module.exports = function(grunt) {
21 22 23
    var buildTime = new Date().getTime(),
        distPath = './dist/',
        libPath = distPath + 'js/libs/',
24
        isDashboardDirectory = grunt.file.isDir('public'),
25
        nodeModulePath = './node_modules/',
26 27 28 29 30 31 32 33 34
        modulesPath = 'public/';
    if (!isDashboardDirectory) {
        modulesPath = '../public/'
    }

    grunt.initConfig({
        watch: {
            js: {
                files: ['public/**/*.js'],
35
                tasks: ['copy:dist']
36 37 38 39 40 41
            },
            html: {
                files: ['public/**/*.html'],
                tasks: ['copy:dist']
            },
            css: {
42
                files: ['public/**/*.scss', 'public/**/*.css'],
43
                tasks: ['copy:dist', 'sass']
44 45 46 47 48 49 50 51 52 53
            },
            image: {
                files: ['public/**/*.{ico,gif,png}'],
                tasks: ['copy:dist']
            }
        },
        connect: {
            server: {
                options: {
                    port: 9999,
54
                    base: distPath,
55 56
                    // change this to '0.0.0.0' to access the server from outside
                    hostname: '0.0.0.0',
57 58 59
                    middleware: function(connect, options, middlewares) {
                        middlewares.unshift(require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware());
                        return middlewares;
60 61 62 63
                    }
                },
                proxies: [{
                    context: '/api', // the context of the data service
64
                    host: '127.0.0.1',
65
                    port: 21000, // the port that the data service is running on
66
                    https: false
67 68 69 70 71 72
                }],
            },
        },
        npmcopy: {
            js: {
                options: {
73 74
                    destPrefix: libPath,
                    srcPrefix: nodeModulePath
75 76
                },
                files: {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                    // FileName : {"src":"dest"}
                    'jquery.min.js': { 'jquery/dist': 'jquery/js' },
                    'require.js': { 'requirejs': 'requirejs' },
                    'text.js': { 'requirejs-text': 'requirejs-text' },
                    'underscore-min.js': { 'underscore': 'underscore' },
                    'bootstrap.min.js': { 'bootstrap/dist/js': 'bootstrap/js' },
                    'backbone-min.js': { 'backbone': 'backbone' },
                    'backbone.babysitter.min.js': { 'backbone.babysitter/lib': 'backbone-babysitter' },
                    'backbone.marionette.min.js': { 'backbone.marionette/lib': 'backbone-marionette' },
                    'backbone.paginator.min.js': { 'backbone.paginator/lib': 'backbone-paginator' },
                    'backbone.wreqr.min.js': { 'backbone.wreqr/lib': 'backbone-wreqr' },
                    'backgrid.js': { 'backgrid/lib': 'backgrid/js' },
                    'backgrid-filter.min.js': { 'backgrid-filter': 'backgrid-filter/js' },
                    'backgrid-orderable-columns.js': { 'backgrid-orderable-columns': 'backgrid-orderable-columns/js' },
                    'backgrid-paginator.min.js': { 'backgrid-paginator': 'backgrid-paginator/js' },
                    'backgrid-sizeable-columns.js': { 'backgrid-sizeable-columns': 'backgrid-sizeable-columns/js' },
                    'Backgrid.ColumnManager.js': { 'backgrid-columnmanager/src': 'backgrid-columnmanager/js' },
                    'jquery-asBreadcrumbs.min.js': { 'jquery-asBreadcrumbs/dist': 'jquery-asBreadcrumbs/js' },
                    'd3.min.js': { 'd3': 'd3' },
                    'index.js': { 'd3-tip': 'd3/' },
                    'dagre-d3.min.js': { 'dagre-d3/dist': 'dagre-d3' },
                    'select2.full.min.js': { 'select2/dist/js': 'select2' },
                    'backgrid-select-all.min.js': { 'backgrid-select-all': 'backgrid-select-all' },
                    'moment.min.js': { 'moment/min': 'moment/js' },
                    'jquery.placeholder.js': { 'jquery-placeholder': 'jquery-placeholder/js' },
                    'platform.js': { 'platform': 'platform' },
                    'query-builder.standalone.min.js': { 'jQuery-QueryBuilder/dist/js': 'jQueryQueryBuilder/js' },
                    'daterangepicker.js': { 'bootstrap-daterangepicker': 'bootstrap-daterangepicker/js' },
                    'nv.d3.min.js': { 'nvd3/build': 'nvd3' },
106 107
                    'jquery.sparkline.min.js': { 'jquery-sparkline': 'sparkline' },
                    'table-dragger.js': { 'table-dragger/dist': 'table-dragger' }
108
                }
109

110 111 112
            },
            css: {
                options: {
113 114
                    destPrefix: libPath,
                    srcPrefix: nodeModulePath
115 116
                },
                files: {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                    'bootstrap.min.css': { 'bootstrap/dist/css': 'bootstrap/css' },
                    'glyphicons-halflings-regular.woff2': { 'bootstrap/fonts': 'bootstrap/fonts' },
                    'backgrid.css': { 'backgrid/lib': 'backgrid/css' },
                    'backgrid-filter.min.css': { 'backgrid-filter': 'backgrid-filter/css' },
                    'backgrid-orderable-columns.css': { 'backgrid-orderable-columns': 'backgrid-orderable-columns/css' },
                    'backgrid-paginator.css': { 'backgrid-paginator': 'backgrid-paginator/css' },
                    'backgrid-sizeable-columns.css': { 'backgrid-sizeable-columns': 'backgrid-sizeable-columns/css' },
                    'Backgrid.ColumnManager.css': { 'backgrid-columnmanager/lib': 'backgrid-columnmanager/css' },
                    'asBreadcrumbs.min.css': { 'jquery-asBreadcrumbs/dist/css': 'jquery-asBreadcrumbs/css' },
                    'select2.min.css': { 'select2/dist/css': 'select2/css' },
                    'backgrid-select-all.min.css': { 'backgrid-select-all': 'backgrid-select-all' },
                    'font-awesome.min.css': { 'font-awesome/css': 'font-awesome/css' },
                    '*': {
                        'expand': true,
                        'dot': true,
                        'cwd': nodeModulePath + 'font-awesome',
                        'src': ['fonts/*.*'],
                        'dest': libPath + 'font-awesome/'
                    },
                    'query-builder.default.min.css': { 'jQuery-QueryBuilder/dist/css': 'jQueryQueryBuilder/css' },
                    'daterangepicker.css': { 'bootstrap-daterangepicker': 'bootstrap-daterangepicker/css' },
                    'nv.d3.min.css': { 'nvd3/build': 'nvd3/css' }
139 140
                }

141 142 143
            },
            license: {
                options: {
144 145
                    destPrefix: libPath,
                    srcPrefix: nodeModulePath
146 147
                },
                files: {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
                    'LICENSE.txt': [
                        { 'jquery': 'jquery' },
                        { 'jquery-placeholder': 'jquery-placeholder' }
                    ],
                    'LICENSE': [{ 'requirejs-text': 'requirejs-text' },
                        { 'underscore': 'underscore' },
                        { 'bootstrap': 'bootstrap' },
                        { 'backgrid-columnmanager': 'backgrid-columnmanager' },
                        { 'jquery-asBreadcrumbs': 'jquery-asBreadcrumbs' },
                        { 'd3': 'd3' },
                        { 'd3-tip': 'd3/' },
                        { 'dagre-d3': 'dagre-d3' },
                        { 'platform': 'platform/' },
                        { 'jQuery-QueryBuilder': 'jQueryQueryBuilder/' }
                    ],
                    'LICENSE.md': [{ 'backbone.babysitter': 'backbone-babysitter' },
                        { 'backbone.wreqr': 'backbone-wreqr' },
                        { 'nvd3': 'nvd3/' }
                    ],
                    'license.txt': [{ 'backbone.marionette': 'backbone-marionette' }],
168
                    'license': [{ 'table-dragger': 'table-dragger' }],
169 170 171 172 173 174 175 176
                    'LICENSE-MIT': [{ 'backbone.paginator': 'backbone-paginator' },
                        { 'backgrid': 'backgrid' },
                        { 'backgrid-filter': 'backgrid-filter' },
                        { 'backgrid-orderable-columns': 'backgrid-orderable-columns' },
                        { 'backgrid-paginator': 'backgrid-paginator' },
                        { 'backgrid-sizeable-columns': 'backgrid-sizeable-columns' },
                        { 'backgrid-select-all': 'backgrid-select-all' }
                    ]
177
                }
178 179
            }
        },
180 181
        sass: {
            dist: {
182
                files: {
183 184
                    'dist/css/style.css': 'public/css/scss/style.scss',
                    'dist/css/login.css': 'public/css/scss/login.scss'
185 186 187
                }
            },
            build: {
188
                files: {
189 190
                    'dist/css/style.css': 'dist/css/scss/style.scss',
                    'dist/css/login.css': 'dist/css/scss/login.scss'
191 192 193
                }
            }
        },
194 195 196 197
        copy: {
            dist: {
                expand: true,
                cwd: modulesPath,
198 199 200 201 202 203 204
                src: ['**', '!**/scss/**'],
                dest: distPath
            },
            build: {
                expand: true,
                cwd: modulesPath,
                src: ['**'],
205 206 207 208 209 210 211 212
                dest: distPath
            }
        },
        clean: {
            build: [distPath, libPath],
            options: {
                force: true
            }
213 214
        },
        uglify: {
215 216 217 218 219 220 221 222 223 224 225 226 227 228
            buildlibs: {
                options: {
                    mangle: true,
                    compress: true,
                    beautify: false
                },
                files: [{
                    expand: true,
                    cwd: 'dist/js',
                    src: ['external_lib/**/*.js', 'libs/**/*.js'],
                    dest: 'dist/js'
                }]
            },
            buildjs: {
229
                options: {
230 231 232
                    mangle: false,
                    compress: true,
                    beautify: true
233
                },
234 235 236
                files: [{
                    expand: true,
                    cwd: 'dist/js',
237
                    src: ['**/*.js', '!libs/**', '!external_lib/**'],
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                    dest: 'dist/js'
                }]
            }
        },
        cssmin: {
            build: {
                files: [{
                    expand: true,
                    cwd: 'dist/css',
                    src: '*.css',
                    dest: 'dist/css'
                }]
            }
        },
        htmlmin: {
            build: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true
                },
                files: [{
                    expand: true,
                    cwd: 'dist/js/templates',
                    src: '**/*.html',
                    dest: 'dist/js/templates'
                }]
            }
265 266 267 268 269 270 271 272 273
        },
        template: {
            build: {
                options: {
                    data: {
                        'bust': buildTime
                    }
                },
                files: {
274
                    [distPath + 'index.html']: [modulesPath + 'index.html.tpl']
275 276
                }
            }
277 278 279
        }
    });

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    // Dynamically add copy-task using npmcopy
    var npmCopy = grunt.config.get('npmcopy'),
        libFiles = [],
        createPath = function(options) {
            var obj = options.obj,
                fileName = options.fileName,
                pathPrefix = options.pathPrefix;
            if (obj.length) {
                for (var i in obj) {
                    createPath({
                        'obj': obj[i],
                        'libFiles': options.libFiles,
                        'pathPrefix': pathPrefix,
                        'fileName': fileName
                    });
                }
            } else {
                key = Object.keys(obj);
                options.libFiles.push({ 'src': pathPrefix.srcPrefix + key + "/" + fileName, 'dest': pathPrefix.destPrefix + obj[key] + "/" + fileName });
            }
        };

    for (var key in npmCopy) {
        var options = npmCopy[key].options,
            files = npmCopy[key].files;
        for (var fileName in files) {
            if (fileName == "*") {
                libFiles.push(files[fileName]);
            } else {
                createPath({
                    'obj': files[fileName],
                    'libFiles': libFiles,
                    'pathPrefix': options,
                    'fileName': fileName
                });
            }
        }
    };
    grunt.config.set('copy.libs', { files: libFiles });

320
    grunt.loadNpmTasks('grunt-contrib-connect');
321
    grunt.loadNpmTasks('grunt-middleware-proxy');
322
    grunt.loadNpmTasks('grunt-contrib-watch');
323 324 325
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');
326
    grunt.loadNpmTasks('grunt-template');
327 328 329 330 331

    require('load-grunt-tasks')(grunt);

    grunt.registerTask('dev', [
        'clean',
332
        'copy:libs',
333
        'copy:dist',
334
        'sass:dist',
335
        'template',
336
        'setupProxies:server',
337
        'connect:server',
338
        'watch'
339 340 341
    ]);

    grunt.registerTask('build', [
342
        'clean',
343
        'copy:libs',
344
        'copy:build',
345 346
        'sass:build',
        'template'
347 348 349 350
    ]);

    grunt.registerTask('dev-minify', [
        'clean',
351
        'copy:libs',
352
        'copy:dist',
353
        'sass:dist',
354 355
        'uglify',
        'cssmin',
356
        'template',
357
        'setupProxies:server',
358 359
        'connect:server',
        'watch'
360 361
    ]);

362 363
    grunt.registerTask('build-minify', [
        'clean',
364
        'copy:libs',
365 366
        'copy:build',
        'sass:build',
367 368
        'uglify',
        'cssmin',
369
        'template'
370
    ]);
371
};