在Grunt中复制dist文件的更有效方法?

时间:2016-01-15 10:56:00

标签: javascript gruntjs grunt-contrib-copy

gruntjs的新手,目前正在使用它将一些npm发行版移动到public / js文件夹。

以下是代码:

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    copy: {
        bootstrapCss: {
            src: "./node_modules/bootstrap/dist/css/bootstrap.css",
            dest: "./public/css/bootstrap.css"
        },
        bootstrapTheme: {
            src: "./node_modules/bootstrap/dist/css/bootstrap-theme.css",
            dest: "./public/css/bootstrap-theme.css"
        },
        bootstrap: {
            src: "./node_modules/bootstrap/dist/js/bootstrap.js",
            dest: "./public/js/libs/bootstrap.js"
        },
        backbone: {
            src: "./node_modules/backbone/backbone.js",
            dest: "./public/js/libs/backbone.js"
        },
        backboneLocalstorage: {
            src: "./node_modules/backbone.localstorage/backbone.localStorage.js",
            dest: "./public/js/libs/backbone.localStorage.js"
        },
        requireJs: {
            src: "./node_modules/requirejs/require.js",
            dest: "./public/js/libs/requirejs.js"
        },
        underscore: {
            src: "./node_modules/underscore/underscore.js",
            dest: "./public/js/libs/underscore.js"
        },
        jquery: {
            src: "./node_modules/jquery/dist/jquery.js",
            dest: "./public/js/libs/jquery.js"
        },
        requireJsText: {
            src: "./node_modules/requirejs-text/text.js",
            dest: "./public/js/libs/requirejs-text.js"
        }
    }



});

// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-copy');

// Default task(s).
grunt.registerTask('default', ['copy']);

};

有没有办法让这些代码更小,而不是有很多单独的复制命令?

由于

1 个答案:

答案 0 :(得分:9)

我会在我的copy配置中创建两个目标,一个用于JS,一个用于CSS。我会使用Grunt提供的一些features来动态构建files对象,以免我输入和维护每个目标路径。这样,当我想添加文件时,我只需要将它们的路径添加到相应的src数组。

grunt.initConfig({
    copy: {
        js: {
            expand: true,
            cwd: './node_modules',
            dest: './public/js/libs/',
            flatten: true,
            filter: 'isFile',
            src: [
                './bootstrap/dist/js/bootstrap.js',
                './backbone/backbone.js',
                './backbone.localstorage/backbone.localStorage.js',
                './requirejs/require.js',
                './underscore/underscore.js',
                './jquery/dist/jquery.js',
                './requirejs-text/text.js'
            ]
        },
        css: {
            expand: true,
            cwd: './node_modules',
            dest: './public/css/',
            flatten: true,
            filter: 'isFile',
            src: [
                './bootstrap/dist/css/bootstrap.css',
                './bootstrap/dist/css/bootstrap-theme.css'
            ]
        }
    }
});