【问题标题】:Continuous CSS concatenation in background with Yeoman使用 Yeoman 在后台连续连接 CSS
【发布时间】:2012-11-23 23:19:05
【问题描述】:

我正在使用Yeoman,但我找不到在文件更改时触发CSS task 的方法。

我有一个类似的项目结构

\ 应用程序 -----\模块1 -------- style.css -----\模块2 -------- style.css -----\模块3 -------- style.css -----\样式 -------- main.css

我想约曼来

  • 查看模块中的所有 CSS 文件;
  • 触发 CSS concat/minification 任务;
  • 覆盖app/styles/main.css

这是我的Gruntfile.js

module.exports = function( grunt ) {
  'use strict';
  //
  // Grunt configuration:
  //
  // https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
  //
  grunt.initConfig({

    // Project configuration
    // ---------------------

    // specify an alternate install location for Bower
    bower: {
      dir: 'app/components'
    },

    // Coffee to JS compilation
    coffee: {
      compile: {
        files: {
          // 'app/scripts/*.js': 'app/scripts/**/*.coffee',
          // 'test/spec/*.js': 'test/spec/**/*.coffee'
        }
      }
    },

    // compile .scss/.sass to .css using Compass
    compass: {
      dist: {}
    },

    // default watch configuration
    watch: {
      reload: {
        files: [
          'Gruntfile.js',
          'app/*.html',
          'app/styles/**/*.css',
          'app/scripts/**/*.js',
          'app/images/**/*',
          'app/app/**/*'
        ],
        tasks: 'css reload'
      }
    },

    // default lint configuration, change this to match your setup:
    // https://github.com/cowboy/grunt/blob/master/docs/task_lint.md#lint-built-in-task
    lint: {
      files: [
        'Gruntfile.js',
        'app/scripts/**/*.js',
        'spec/**/*.js'
      ]
    },

    // specifying JSHint options and globals
    // https://github.com/cowboy/grunt/blob/master/docs/task_lint.md#specifying-jshint-options-and-globals
    jshint: {
      options: {
        curly: true,
        eqeqeq: true,
        immed: true,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        boss: true,
        eqnull: true,
        browser: true
      },
      globals: {
        angular: true
      }
    },

    // Build configuration
    // -------------------

    // the staging directory used during the process
    staging: 'temp',

    // final build output
    output: 'dist',

    mkdirs: {
      staging: 'app/'
    },

    // Below, all paths are relative to the staging directory, which is a copy
    // of the app/ directory. Any .gitignore, .ignore and .buildignore file
    // that might appear in the app/ tree are used to ignore these values
    // during the copy process.

    // concat css/**/*.css files, inline @import, output a single minified css
    css: {
      'app/styles/main.css': ['app/styles/*.css', 'app/app/**/*.css']
    },

    // renames JS/CSS to prepend a hash of their contents for easier
    // versioning
    rev: {
      js: 'scripts/**/*.js',
      css: 'styles/**/*.css',
      img: 'images/**'
    },

    // usemin handler should point to the file containing
    // the usemin blocks to be parsed
    'usemin-handler': {
      html: 'index.html'
    },

    // update references in HTML/CSS to revved files
    usemin: {
      html: ['**/*.html'],
      css: ['**/*.css']
    },

    // HTML minification
    html: {
      files: ['**/*.html']
    },

    // Optimizes JPGs and PNGs (with jpegtran & optipng)
    img: {
      dist: '<config:rev.img>'
    },

    // rjs configuration. You don't necessarily need to specify the typical
    // `path` configuration, the rjs task will parse these values from your
    // main module, using http://requirejs.org/docs/optimization.html#mainConfigFile
    //
    // name / out / mainConfig file should be used. You can let it blank if
    // you're using usemin-handler to parse rjs config from markup (default
    // setup)
    rjs: {
      // no minification, is done by the min task
      optimize: 'none',
      baseUrl: './scripts',
      wrap: true
    }
  });

  // Alias the `test` task to run `testacular` instead
  grunt.registerTask('test', 'run the testacular test driver', function () {
    var done = this.async();
    require('child_process').exec('testacular start --single-run', function (err, stdout) {
      grunt.log.write(stdout);
      done(err);
    });
  });
};

【问题讨论】:

  • 您是否尝试过编写自己的手表配置?
  • 请记住,您只需要在构建时连接和缩小。开发时,您只希望 compass 生成 CSS 文件。默认生成器应该实现,净这个就好了。这是模板(如果您复制某些内容,请确保修复 yeoman 占位符 github.com/yeoman/generator-angular/blob/master/templates/…

标签: css build gruntjs yeoman


【解决方案1】:

我是 grunt 的新手,但我为编写自定义监视任务所做的就是这样。首先,我注册了一个新任务,其中仅包含我在开发时想要运行的东西。例如,我希望我的项目针对未缩小的 js 运行。所以我不要求执行 min 或 concat 任务。只需对它们进行 lint、测试并使用 grunt-targethtml 为开发模式重写 html。

    grunt.registerTask('devel', 'lint qunit targethtml');

然后我替换默认的watch配置运行devel任务

    watch: {
        files: '<config:lint.files>',
        tasks: 'devel'
    }

这是使用为 lint 任务定义的文件列表,但您应该能够将其替换为

       files: ['app/**/*.css']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2010-10-18
    • 1970-01-01
    • 2014-09-04
    • 2011-04-26
    相关资源
    最近更新 更多