简体   繁体   English

Grunt,在构建时将html文件复制到脚本文件夹

[英]Grunt, copy html files to scripts folder on build

I use the angular-generator in yeoman. 我在自耕农中使用角度发生器。 In gruntfile.js, every html file in /app/views get copied to dist/views . 在gruntfile.js中, /app/views每个html文件都被复制到dist/views But I like to keep my directive templates in the same folder as the directive itself. 但我喜欢将指令模板保存在与指令本身相同的文件夹中。

Example: 例:

/app/scripts/widgets/mywidget.directive.js
/app/scripts/widgets/mywidget.tmpl.html

When I build the project, I want the html file to end up in the same folder structure as above. 当我构建项目时,我希望html文件最终处于与上面相同的文件夹结构中。

This should probably be done in the copy section in gruntfile.js. 这应该可以在gruntfile.js的copy部分中完成。

copy: {
  dist: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= yeoman.app %>',
      dest: '<%= yeoman.dist %>',
      src: [
        '*.{ico,png,txt}',
        '*.html',
        'images/{,*/}*.{webp}',
        'styles/fonts/{,*/}*.*'
      ]
    }...

I tried to add this in the src array: 我试着在src数组中添加它:

    '<%= yeoman.dist %>/scripts/{,*/}*.tmpl.html'

Did not work. 不工作。 Any ideas? 有任何想法吗?

You can move all .tmpl.html from app/scripts/* to dist/scripts/* using code modifications to the gruntfile like below. 您可以使用下面的gruntfile代码修改将所有.tmpl.html从app / scripts / *移动到dist / scripts / *。

files: [{
          expand: true,
          dot: true,
          cwd: '<%= yeoman.app %>',
          dest: '<%= yeoman.dist %>',
          src: [
            '*.{ico,png,txt}',
            '*.html',
            'views/{,*/}*.html'
          ]
        }, {
          // This block handles copying the .tmpl.html from app/scripts to dist/scripts
          expand: true,
          cwd: '<%= yeoman.app %>/scripts',
          dest: '<%= yeoman.dist %>/scripts',
          src: '{,*/}*.tmpl.html'
        }
       ...

You are going to want to add the new directory to the usemin block as well to ensure the filerev updates make it into your templates 您将要将新目录添加到usemin块,以确保filerev更新将其添加到您的模板中

usemin: {
      html: ['<%= yeoman.dist %>/{,*/}*.html', 
             '<%= yeoman.dist %>/scripts/{,*/}*.html'],
      ...

You also might want to add the directory to htmlmin to minify to html 您还可能希望将目录添加到htmlmin以缩小为html

htmlmin: {
  dist: {
    ...
    files: [
      {
        expand: true,
        cwd: '<%= yeoman.dist %>',
        src: ['*.html', 'views/{,*/}*.html', 'scripts/{,*/}*.html'],
        dest: '<%= yeoman.dist %>'
      }
    ]
  }

UPDATED These scripts now reflect moving any .tmpl.html from app/scripts/*/ to dist/scripts/*/ . 更新这些脚本现在反映将任何.tmpl.html从app/scripts/*/dist/scripts/*/ If your folder structure is more than one level deep inside scripts, change {,*/}*.html to **/*.html 如果您的文件夹结构在脚本内部有多个级别,请将{,*/}*.html更改为**/*.html

it's the normal behavior, that all files after build are copied to the dist-folder, because this is the standard build-output-folder. 这是正常的行为,构建后的所有文件都被复制到dist-folder,因为这是标准的build-output-folder。
what you can do, is to change config like this: 你可以做的是改变这样的配置:

 copy: {
        main: {
            files: [{
                src: ['img/**'],
                dest: 'dist/img/',
                filter: 'isFile',
                expand: true,
                flatten: true
            }, {
                src: ['pdf/**'],
                dest: 'dist/pdf/',
                filter: 'isFile',
                expand: true,
                flatten: true
            },{
                src: ['error/**'],
                dest: 'dist/error/',
                filter: 'isFile',
                expand: true,
                flatten: true
            }, {
                src: ['fonts/**'],
                dest: 'dist/fonts/',
                filter: 'isFile',
                expand: true,
                flatten: true
            }
        }
    }

this approach preserves the old structure within the dist folder. 这种方法保留了dist文件夹中的旧结构。 But I'm wondering, why you don't use htmlmin to minify and pack all your templates together on build... 但我想知道,为什么你不使用htmlmin缩小并将所有模板打包在一起构建...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM