javascript 配置 grunt 复制任务以排除文件/文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15199092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:50:32  来源:igfitidea点击:

Configure grunt copy task to exclude files/folders

javascriptcopygruntjs

提问by mybecks

I have installed the grunt task grunt-contrib-copy. I embedd it in my Gruntfile.jsand load the task via grunt.loadNpmTasks('grunt-contrib-copy');.

我已经安装了 grunt 任务grunt-contrib-copy。我将它嵌入到我的Gruntfile.js并通过grunt.loadNpmTasks('grunt-contrib-copy');.

Currently I use following configuration to create a folder with a subset of my js files/folders.

目前我使用以下配置来创建一个包含我的 js 文件/文件夹子集的文件夹。

copy: {
            options: {
                processContent: [],
                processContentExclude: ['build/**', 'bin/**', '.*', '*.orig', '*.bak', '.*/**', '*.log', 'dist/**', 'test/**', 'dev/**', 'pyserver/**', 'node_modules/**', 'doc/**']
            },
            du: {
                files: [ 
                    {src: ['.conf1', '.conf2', './config.js'], dest: 'output/toolkit/', filter: 'isFile'},
                    {src: ['./css/**/*', './img/**/*', './js/**/*', './release/**/*', './lib/**/*', './locale/**/*'], dest: 'output/toolkit/'},
                    {expand: true, cwd: './', src: ['**'], dest: 'output/'}
                ]
            }
        }

This works fine, but everytime I run grunt copy it exits with the following error message:

这工作正常,但每次运行 grunt copy 时,它都会退出并显示以下错误消息:

Copying Gruntfile.js -> output/Gruntfile.js
Warning: Error while processing "Gruntfile.js" file. Use --force to continue.

I would like to exclude Gruntfile.jsand all *.lessfiles in js/**/*. Tried it with !(.less), !.less, !(*.less), !(./Grunfile.js), !(*Gruntfile.js)... But nothing works. Added it to the processContentExcludearray, but without success too.

我想排除Gruntfile.js和所有*.less的文件js/**/*。尝试过!(.less), !.less, !(*.less), !(./Grunfile.js), !(*Gruntfile.js)......但没有任何效果。将它添加到processContentExclude数组中,但也没有成功。

So how can I exclude the Gruntfile.jsand all less files in the folder structure js/**/*?

那么如何排除Gruntfile.js文件夹结构中的所有文件js/**/*

回答by mybecks

Found the solution:

找到了解决办法:

There is no need for these lines:

不需要这些行:

files: [ 
          {src: ['.conf1', '.conf2', './config.js'], dest: 'output/toolkit/', filter: 'isFile'},
          {src: ['./css/**/*', './img/**/*', './js/**/*', './release/**/*', './lib/**/*', './locale/**/*'], dest: 'output/toolkit/'},
          {expand: true, cwd: './', src: ['**'], dest: 'output/'}
       ]

because {expand: true, cwd: './', src: ['**'], dest: 'output/'}is a new copy step, copying all files from ./to output. Which is for me not needed, because the above lines are already copying the required files to output/toolkit.

因为 {expand: true, cwd: './', src: ['**'], dest: 'output/'}是新副本的步骤,从复制所有文件./output。这对我来说不需要,因为上面的行已经将所需的文件复制到output/toolkit.

So the following two lines does the job. No need for options or anything else. To keep out the *.lessfiles '!**/*.less'does the job.

所以下面的两行就完成了这项工作。不需要选项或其他任何东西。将*.less文件排除在外就'!**/*.less'可以了。

files: [ 
          {src: ['.conf1', '.conf2', 'config.js'], dest: 'output/toolkit/', filter: 'isFile'},
          {src: ['css/**', 'img/**', 'js/**', 'release/**', 'lib/**', 'locale/**', '!**/*.less'], dest: 'output/toolkit/'}
       ]

回答by StackHola

I wanted to exclude all .gz files in my copy and this options did the job for all folders

我想在我的副本中排除所有 .gz 文件,这个选项可以完成所有文件夹的工作

{
  expand: true,
  cwd:    './build/www/',
  src:    [ '**', '!**/*.gz' ],
  dest:   './mydDest'
}

So may be try !**/*.less, !gruntfile.js(no parens)

所以可以尝试!**/*.less, !gruntfile.js(没有括号)

回答by daxeh

Assuming that 'Gruntfile.js' is in root directory, the last line in your copy options is configuring it to copy everything in root to 'output/'.

假设 'Gruntfile.js' 在根目录中,复制选项中的最后一行是将其配置为将根目录中的所有内容复制到 'output/'。

If this is intended, add '!Gruntfile.js' to your array of src file paths.

如果是这样,请将 '!Gruntfile.js' 添加到您的 src 文件路径数组中。

{
  expand: true, 
  cwd: './', 
  src: ['**'],      // ex. src: ['**',  '!Gruntfile.js'],
  dest: 'output/'
}

Refer to these solutions from this threadand original thread

请参阅此线程原始线程中的这些解决方案

回答by Risadinha

If you have the liberty to change to a different Grunt task:

如果您可以自由更改为不同的 Grunt 任务:

There is also https://github.com/clavery/grunt-copy-towhich has an explicit "ignore" option to specify files and directories to omit. The README in Github shows an example configuration including ignore options.

还有https://github.com/clavery/grunt-copy-to有一个明确的“忽略”选项来指定要省略的文件和目录。Github 中的 README 显示了一个示例配置,包括忽略选项。

It works a bit different from the regular copy as it honours the modification times. But that might actually be welcome (it was in my case).

它的工作方式与常规副本略有不同,因为它遵守修改时间。但这实际上可能是受欢迎的(就我而言)。

From the README:

从自述文件:

Like grunt-contrib-copy but only copies files that are newer and maintains modified times for copied files. Useful for creating build directories that can be later synced using tools that rely on file modified times.

类似于 grunt-contrib-copy 但只复制较新的文件并维护复制文件的修改时间。用于创建构建目录,稍后可以使用依赖文件修改时间的工具进行同步。

(I'm only a user of that project (at least so far).)

(我只是该项目的用户(至少到目前为止)。)

回答by Travis G

I reckon this should do it. Not need for /**/*. /**covers all files in path and its subdirs

我认为应该这样做。不需要为/**/*. /**覆盖路径及其子目录中的所有文件

{src: ['./css/**', './img/**', './js/**', './release/**', './lib/**', './locale/**'], dest: 'output/toolkit/'},
{expand: true, cwd: './', src: ['**'], dest: 'output/'}