【问题标题】:Can GruntJS remove the bower_components folder after copying files?GruntJS 可以在复制文件后删除 bower_components 文件夹吗?
【发布时间】:2014-02-24 17:14:39
【问题描述】:

我正在使用 Bower 和 GruntJS。我认为 grunt-bowercopy 会在完成后删除 bower_components 文件夹,但这并没有发生。

grunt-bowercopy 可以在将文件复制到我想要的位置后删除 bower_components 文件夹吗?

这是我的文件夹设置:

->Project
    ->Themes
        ->Theme
            ->assets
                ->scripts
                ->styles
    ->tools
        ->build
            ->bower_components
            ->bower.json
            ->gruntfile.js
            ->package.json

这是我在 gruntfile.js 中运行 grunt-bowercopy 的方式

module.exports = function (grunt) {

// Loads the necessary tasks for this Grunt file.
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    // Project configuration.
    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        bowercopy: {
          options: {
            clear: true
          },
          js: {
            options: {
                destPrefix: '../../Themes/Theme/assets/'
            },
            //The key is the destination file.  The value is the file from the bower_components folder
            //The files will be added to a scripts folder at the location: ../../themes/3MTheme/assets/scripts
            //The key is the name of the folder that is copied into the /assets folder.
            src : 'scripts'

          },
          css: {
            options: {
                destPrefix: '../../Themes/Theme/assets/'
            },
            src: 'styles'

          }
       }

      // Custom task, executed via the command "grunt bowercopy"
      grunt.registerTask('bower', ['bowercopy']);
};    

【问题讨论】:

  • 如果它在您构建项目后删除了 bower_components 文件夹,您将需要再次安装这些组件以进行下一次构建,对吗?为什么要删除该文件夹?它仅用于开发目的,不会进入 dist 文件夹。
  • @Sergey,我正在从共享服务器复制一些文件。这些文件可能偶尔会被其他人更新,我想确保我删除了那些最新的更新。

标签: javascript gruntjs bower


【解决方案1】:

grunt-bowercopy 似乎不会为您执行此操作,但我已经为在运行测试命令之前清除报告做了类似的事情。

只需创建您自己的自定义任务,称为 cleanbowercopy,它会在您正在清理的目录上执行 rmdir

然后更新您的注册任务以在 bowercopy 之后调用它:

grunt.registerTask('bower',['bowercopy','cleanbowercopy']);

这样它将完成您在同一命令中寻找的内容,如果您希望在不同的时间点清理目录,甚至可以重复使用。

【讨论】:

  • 谢谢!这真是个好主意。看起来 grunt-bowercopy 会删除 bower_components 文件夹,我只是在我在这里发布它之后才发现我的错误。
【解决方案2】:

我找到了答案。我在选项中使用了clear: true,而它应该是clean: true

这适用于我的 gruntfile.js

module.exports = function (grunt) {

// Loads the necessary tasks for this Grunt file.
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

// Project configuration.
grunt.initConfig({

    pkg: grunt.file.readJSON('package.json'),

    bowercopy: {
      options: {
        clean: true
      },
      js: {
        options: {
            destPrefix: '../../Themes/Theme/assets/'
        },
        //The key is the destination file.  The value is the file from the bower_components folder
        //The files will be added to a scripts folder at the location: ../../themes/3MTheme/assets/scripts
        //The key is the name of the folder that is copied into the /assets folder.
        src : 'scripts'

      },
      css: {
        options: {
            destPrefix: '../../Themes/Theme/assets/'
        },
        src: 'styles'

      }
   }

  // Custom task, executed via the command "grunt bowercopy"
  grunt.registerTask('bower', ['bowercopy']);
};    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2013-10-29
    • 1970-01-01
    • 2014-10-12
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多