【问题标题】:Yeoman Angular app, grunt watch .less filesYeoman Angular 应用程序,grunt watch .less 文件
【发布时间】:2014-05-11 05:53:08
【问题描述】:

我在玩 Yeoman、grunt、bower、bootstrap 和 yo Angular 应用程序。我试图让 grunt watch .less 文件,但我遇到了一些麻烦。 我当然已经用

安装了 grunt-contrib-less 模块
npm install grunt-contrib-less --save-dev

并将其保存到我的 package.json 中。

我已经能够使用代码在其他项目中完成这项工作:

  module.exports = function(grunt){
        grunt.initConfig({
            pkg:grunt.file.readJSON('package.json'),
            less:{
                development:{
                        options: {
                            paths: ['bower_components/bootstrap/less', 'bower_components/bootstrap/dist/css'],
                                   yuicompress: true
                        },
                    files:{
                        'bower_components/bootstrap/dist/css/bootstrap.css' : 'bower_components/bootstrap/less/bootstrap.less',
                    }
                }
            },
            // running 'grunt watch' will watch for changes
            watch: {
                files: ['bower_components/bootstrap/less/*.less', 'bower_components/bootstrap/less/*.less'],
                       tasks: ['less']
            }
        });

        grunt.loadNpmTasks('grunt-contrib-less');
        grunt.loadNpmTasks('grunt-contrib-watch');
        grunt.registerTask('default',['less']);
    };

和:

module.exports = function(grunt) {
      grunt.initConfig({
          less: {
              development: {
                  options: {
                      paths: ["bower_components/bootstrap/less"],
                      yuicompress: true
                  },
                  files: {
                      "bower_components/bootstrap/dist/css/bootstrap.css": "bower_components/bootstrap/less/bootstrap.less"
                  }
              }
          },
          watch: {
              files: "bower_components/bootstrap/less/*",
              tasks: ["less"]
          }
      });
      grunt.loadNpmTasks('grunt-contrib-less');
      grunt.loadNpmTasks('grunt-contrib-watch');
};

但我之前没有尝试过使用 Angular 应用程序。它也可能像我做错路由一样简单,但我现在已经尝试了很多次,我不确定是不是这样。 完整的东西也在github上https://github.com/Lumberfella/angularApp.git

我已经在 Gruntfile.js 中有一些监视代码

watch: {
      bower: {
        files: ['bower.json'],
        tasks: ['bowerInstall']
      },
      js: {
        files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],
        tasks: ['newer:jshint:all'],
        options: {
          livereload: true
        }
      },
      jsTest: {
        files: ['test/spec/{,*/}*.js'],
        tasks: ['newer:jshint:test', 'karma']
      },
      styles: {
        files: ['<%= yeoman.app %>/styles/{,*/}*.css'],
        tasks: ['newer:copy:styles', 'autoprefixer']
      },
      gruntfile: {
        files: ['Gruntfile.js']
      },
      livereload: {
        options: {
          livereload: '<%= connect.options.livereload %>'
        },
        files: [
          '<%= yeoman.app %>/{,*/}*.html',
          '.tmp/styles/{,*/}*.css',
          '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
        ]
      }
    },

并且想要类似的东西(我认为不是真正的功能代码)

less: {
  files: [
    '<%= yeoman.app %>/{,*/something.something}*.less'
  ]
  tasks: ['less:developer']
},

查看 .less 文件并尽可能将其制作成 css 文件。

【问题讨论】:

    标签: angularjs twitter-bootstrap less gruntjs yeoman


    【解决方案1】:

    这个问题的答案很简单,因为我有问题中的解决方案代码。对我来说唯一困难的事情是放在哪里以及如何放置。

    首先我安装了 grunt-contrib-less 模块,并在我的样式文件夹中而不是 bower_components 中创建了另一个 bootstrapless 文件夹(谢谢 Mingos!)。 然后我更新了 Gruntfile,这就是现在的样子:

    'use strict';
    
    // # Globbing
    // for performance reasons we're only matching one level down:
    // 'test/spec/{,*/}*.js'
    // use this if you want to recursively match all subfolders:
    // 'test/spec/**/*.js'
    
    module.exports = function (grunt) {
    
      // Load grunt tasks automatically
      require('load-grunt-tasks')(grunt);
    
      // Time how long tasks take. Can help when optimizing build times
      require('time-grunt')(grunt);
    
      // Define the configuration for all the tasks
      grunt.initConfig({
    
        // Project settings
        yeoman: {
          // configurable paths
          app: require('./bower.json').appPath || 'app',
          dist: 'dist'
        },
        less: {
            development: {
                options: {
                    compress: true,
                    yuicompress: true,
                    optimization: 2
                },
                files: {
                    // target.css file: source.less file
                    "app/styles/bootstrap.css": "app/styles/less/bootstrap.less"
                }
            }
        },
    
        // Watches files for changes and runs tasks based on the changed files
        watch: {
          bower: {
            files: ['bower.json'],
            tasks: ['bowerInstall']
          },
          js: {
            files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],
            tasks: ['newer:jshint:all'],
            options: {
              livereload: true
            }
          },
          jsTest: {
            files: ['test/spec/{,*/}*.js'],
            tasks: ['newer:jshint:test', 'karma']
          },
          styles: {
            files: ['<%= yeoman.app %>/styles/{,*/}*.css', '<%= yeoman.app %>/styles/less/{,*/}*.less'],
            tasks: ['newer:copy:styles', 'autoprefixer', 'less']
          },
          gruntfile: {
            files: ['Gruntfile.js']
          },
          livereload: {
            options: {
              livereload: '<%= connect.options.livereload %>'
            },
            files: [
              '<%= yeoman.app %>/{,*/}*.html',
              '.tmp/styles/{,*/}*.css',
              '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
            ]
          }
        },
    
        // The actual grunt server settings
        connect: {
          options: {
            port: 9000,
            // Change this to '0.0.0.0' to access the server from outside.
            hostname: 'localhost',
            livereload: 35729
          },
          livereload: {
            options: {
              open: true,
              base: [
                '.tmp',
                '<%= yeoman.app %>'
              ]
            }
          },
          test: {
            options: {
              port: 9001,
              base: [
                '.tmp',
                'test',
                '<%= yeoman.app %>'
              ]
            }
          },
          dist: {
            options: {
              base: '<%= yeoman.dist %>'
            }
          }
        },
    
        // Make sure code styles are up to par and there are no obvious mistakes
        jshint: {
          options: {
            jshintrc: '.jshintrc',
            reporter: require('jshint-stylish')
          },
          all: [
            'Gruntfile.js',
            '<%= yeoman.app %>/scripts/{,*/}*.js'
          ],
          test: {
            options: {
              jshintrc: 'test/.jshintrc'
            },
            src: ['test/spec/{,*/}*.js']
          }
        },
    
        // Empties folders to start fresh
        clean: {
          dist: {
            files: [{
              dot: true,
              src: [
                '.tmp',
                '<%= yeoman.dist %>/*',
                '!<%= yeoman.dist %>/.git*'
              ]
            }]
          },
          server: '.tmp'
        },
    
        // Add vendor prefixed styles
        autoprefixer: {
          options: {
            browsers: ['last 1 version']
          },
          dist: {
            files: [{
              expand: true,
              cwd: '.tmp/styles/',
              src: '{,*/}*.css',
              dest: '.tmp/styles/'
            }]
          }
        },
    
        // Automatically inject Bower components into the app
        bowerInstall: {
          app: {
            src: ['<%= yeoman.app %>/index.html'],
            ignorePath: '<%= yeoman.app %>/'
          }
        },
    
        // Renames files for browser caching purposes
        rev: {
          dist: {
            files: {
              src: [
                '<%= yeoman.dist %>/scripts/{,*/}*.js',
                '<%= yeoman.dist %>/styles/{,*/}*.css',
                '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
                '<%= yeoman.dist %>/styles/fonts/*'
              ]
            }
          }
        },
    
        // Reads HTML for usemin blocks to enable smart builds that automatically
        // concat, minify and revision files. Creates configurations in memory so
        // additional tasks can operate on them
        useminPrepare: {
          html: '<%= yeoman.app %>/index.html',
          options: {
            dest: '<%= yeoman.dist %>',
            flow: {
              html: {
                steps: {
                  js: ['concat', 'uglifyjs'],
                  css: ['cssmin']
                },
                post: {}
              }
            }
          }
        },
    
        // Performs rewrites based on rev and the useminPrepare configuration
        usemin: {
          html: ['<%= yeoman.dist %>/{,*/}*.html'],
          css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
          options: {
            assetsDirs: ['<%= yeoman.dist %>']
          }
        },
    
        // The following *-min tasks produce minified files in the dist folder
        cssmin: {
          options: {
            root: '<%= yeoman.app %>'
          }
        },
    
        imagemin: {
          dist: {
            files: [{
              expand: true,
              cwd: '<%= yeoman.app %>/images',
              src: '{,*/}*.{png,jpg,jpeg,gif}',
              dest: '<%= yeoman.dist %>/images'
            }]
          }
        },
    
        svgmin: {
          dist: {
            files: [{
              expand: true,
              cwd: '<%= yeoman.app %>/images',
              src: '{,*/}*.svg',
              dest: '<%= yeoman.dist %>/images'
            }]
          }
        },
    
        htmlmin: {
          dist: {
            options: {
              collapseWhitespace: true,
              collapseBooleanAttributes: true,
              removeCommentsFromCDATA: true,
              removeOptionalTags: true
            },
            files: [{
              expand: true,
              cwd: '<%= yeoman.dist %>',
              src: ['*.html', 'views/{,*/}*.html'],
              dest: '<%= yeoman.dist %>'
            }]
          }
        },
    
        // ngmin tries to make the code safe for minification automatically by
        // using the Angular long form for dependency injection. It doesn't work on
        // things like resolve or inject so those have to be done manually.
        ngmin: {
          dist: {
            files: [{
              expand: true,
              cwd: '.tmp/concat/scripts',
              src: '*.js',
              dest: '.tmp/concat/scripts'
            }]
          }
        },
    
        // Replace Google CDN references
        cdnify: {
          dist: {
            html: ['<%= yeoman.dist %>/*.html']
          }
        },
    
        // Copies remaining files to places other tasks can use
        copy: {
          dist: {
            files: [{
              expand: true,
              dot: true,
              cwd: '<%= yeoman.app %>',
              dest: '<%= yeoman.dist %>',
              src: [
                '*.{ico,png,txt}',
                '.htaccess',
                '*.html',
                'views/{,*/}*.html',
                'images/{,*/}*.{webp}',
                'fonts/*'
              ]
            }, {
              expand: true,
              cwd: '.tmp/images',
              dest: '<%= yeoman.dist %>/images',
              src: ['generated/*']
            }]
          },
          styles: {
            expand: true,
            cwd: '<%= yeoman.app %>/styles',
            dest: '.tmp/styles/',
            src: '{,*/}*.css'
          }
        },
    
        // Run some tasks in parallel to speed up the build process
        concurrent: {
          server: [
            'copy:styles'
          ],
          test: [
            'copy:styles'
          ],
          dist: [
            'copy:styles',
            'imagemin',
            'svgmin'
          ]
        },
    
        // By default, your `index.html`'s <!-- Usemin block --> will take care of
        // minification. These next options are pre-configured if you do not wish
        // to use the Usemin blocks.
        // cssmin: {
        //   dist: {
        //     files: {
        //       '<%= yeoman.dist %>/styles/main.css': [
        //         '.tmp/styles/{,*/}*.css',
        //         '<%= yeoman.app %>/styles/{,*/}*.css'
        //       ]
        //     }
        //   }
        // },
        // uglify: {
        //   dist: {
        //     files: {
        //       '<%= yeoman.dist %>/scripts/scripts.js': [
        //         '<%= yeoman.dist %>/scripts/scripts.js'
        //       ]
        //     }
        //   }
        // },
        // concat: {
        //   dist: {}
        // },
    
        // Test settings
        karma: {
          unit: {
            configFile: 'karma.conf.js',
            singleRun: true
          }
        }
      });
    
      grunt.loadNpmTasks('grunt-contrib-less');
      grunt.loadNpmTasks('grunt-contrib-watch');
    
      grunt.registerTask('serve', function (target) {
        if (target === 'dist') {
          return grunt.task.run(['build', 'connect:dist:keepalive']);
        }
    
        grunt.task.run([
          'clean:server',
          'bowerInstall',
          'concurrent:server',
          'autoprefixer',
          'connect:livereload',
          'watch'
        ]);
      });
    
      grunt.registerTask('server', function (target) {
        grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
        grunt.task.run(['serve:' + target]);
      });
    
      grunt.registerTask('test', [
        'clean:server',
        'concurrent:test',
        'autoprefixer',
        'connect:test',
        'karma'
      ]);
    
      grunt.registerTask('build', [
        'clean:dist',
        'bowerInstall',
        'useminPrepare',
        'concurrent:dist',
        'autoprefixer',
        'concat',
        'ngmin',
        'copy:dist',
        'cdnify',
        'cssmin',
        'uglify',
        'rev',
        'usemin',
        'htmlmin'
      ]);
    
      grunt.registerTask('default', [
        'newer:jshint',
        'test',
        'build'
      ]);
    };
    

    如您所见,这在我的样式文件夹中创建了一个新的 bootstrap.css,然后我从我的 index.html 链接到该文件夹​​:)

    【讨论】:

    • 这对我来说非常适合最新版本的 yeoman。对我这样的新手很有帮助。
    【解决方案2】:

    尝试将要观看的文件包装在子任务中:

    watch: {
        less: {
            files: "bower_components/bootstrap/less/*",
            tasks: ["less"]
        }
    }
    

    另外,请注意:您确定要监视通过 Bower 吸入的文件的更改吗?观看您编辑自己的代码而不是组件的目录不是更有意义吗?

    【讨论】:

    • 我不确定。这是否意味着我必须手动移动文件?从 bower_component 目录中观看它也没有问题。我可以从上面的代码中看到它只监视对较少代码所做的更改,但它不会更新我从我的 index.html 链接到的 bootstrap.css(确实如此)
    • 我的意思是编辑包含在 Bower 组件目录中的文件是个坏主意,因为在您运行 bower update 之后,您的更改很可能会被更新版本的组件覆盖。如果你想在你的自定义样式表中使用 Bootstrap 的文件,@import 他们。
    • 但是,如果我想编辑较少的引导程序文件并仍然将它们从凉亭组件@import 到自定义样式表,该怎么办。如果我更新,我在引导程序中编辑的文件不会被覆盖吗?我只是在寻找最简单的方法来做到这一点,一切都是自动的。 :)
    • 这就是我刚刚写的:在bower update 之后,您的更改可能会被覆盖。作为一般规则:永远不要修改 bower_components 目录中的任何内容。复制并编辑它,但不要触摸原始文件。 Bower 背后的整个想法是,无论何时出现新版本,或者当某个包需要给定版本的库作为依赖项时,Bower 都可以用来替换过时的代码。
    • 啊……好吧。我花了一段时间才得到它。谢谢!现在回到第一个问题。我怎样才能使上面的代码使更少的文件更新我从 index.html 链接到的 bootstrap.css?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2015-06-06
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多