Yeoman使用Sass文件的工作流程是什么?

时间:2013-03-25 14:28:22

标签: sass compass-sass gruntjs yeoman

我尝试使用自耕农,但我不知道如何使用我自己的sass文件。

使用

grunt server

观看Sass文件并将其编译成

.tmp/styles/

但除了<link rel="stylesheet" href="styles/main.css">

之外,没有对已编译样式表的引用

那么,在开发过程中使用index.html中编译的sass文件的推荐方法是什么?

E.g。 grunt server,如果我将我的样式app/styles/my.sass更改为.tmp/styles/my.css,则会被覆盖并且它位于服务器之外(localhost:9000)。因此,无法在index.html中链接它。

使用grunt buildmain.css中的所有内容都包含my.sass,但在开发过程中,我不知道如何在index.html中使用我自己的sass文件。

你能给我一些简单的例子吗?

这是默认的yeoman安装。我这样做了:

  1. yo angular test
  2. 我添加app/styles/style.sass
  3. grunt serverstyle.sass编译为.tmp/styles/style.css
  4. 现在我不知道如何在html中加入style.css
  5. (对不起,这可能是一个愚蠢的问题,但我也是新人,也是自己的咕噜声)

    这是来自yeoman的Gruntfile.js:

    'use strict';
    var lrSnippet = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;
    var mountFolder = function (connect, dir) {
      return connect.static(require('path').resolve(dir));
    };
    
    module.exports = function (grunt) {
      // load all grunt tasks
      require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
    
      // configurable paths
      var yeomanConfig = {
        app: 'app',
        dist: 'dist'
      };
    
      try {
        yeomanConfig.app = require('./component.json').appPath || yeomanConfig.app;
      } catch (e) {}
    
      grunt.initConfig({
        yeoman: yeomanConfig,
        watch: {
          coffee: {
            files: ['<%= yeoman.app %>/scripts/{,*/}*.coffee'],
            tasks: ['coffee:dist']
          },
          coffeeTest: {
            files: ['test/spec/{,*/}*.coffee'],
            tasks: ['coffee:test']
          },
          compass: {
            files: ['<%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
            tasks: ['compass']
          },
          livereload: {
            files: [
              '<%= yeoman.app %>/{,*/}*.html',
              '{.tmp,<%= yeoman.app %>}/styles/{,*/}*.css',
              '{.tmp,<%= yeoman.app %>}/scripts/{,*/}*.js',
              '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp}'
            ],
            tasks: ['livereload']
          }
        },
        connect: {
          livereload: {
            options: {
              port: 9000,
              // Change this to '0.0.0.0' to access the server from outside.
              hostname: 'localhost',
              middleware: function (connect) {
                return [
                  lrSnippet,
                  mountFolder(connect, '.tmp'),
                  mountFolder(connect, yeomanConfig.app)
                ];
              }
            }
          },
          test: {
            options: {
              port: 9000,
              middleware: function (connect) {
                return [
                  mountFolder(connect, '.tmp'),
                  mountFolder(connect, 'test')
                ];
              }
            }
          }
        },
        open: {
          server: {
            url: 'http://localhost:<%= connect.livereload.options.port %>'
          }
        },
        clean: {
          dist: ['.tmp', '<%= yeoman.dist %>/*'],
          server: '.tmp'
        },
        jshint: {
          options: {
            jshintrc: '.jshintrc'
          },
          all: [
            'Gruntfile.js',
            '<%= yeoman.app %>/scripts/{,*/}*.js'
          ]
        },
        karma: {
          unit: {
            configFile: 'karma.conf.js',
            singleRun: true
          }
        },
        coffee: {
          dist: {
            files: {
              '.tmp/scripts/coffee.js': '<%= yeoman.app %>/scripts/*.coffee'
            }
          },
          test: {
            files: [{
              expand: true,
              cwd: '.tmp/spec',
              src: '*.coffee',
              dest: 'test/spec'
            }]
          }
        },
        compass: {
          options: {
            sassDir: '<%= yeoman.app %>/styles',
            cssDir: '.tmp/styles',
            imagesDir: '<%= yeoman.app %>/images',
            javascriptsDir: '<%= yeoman.app %>/scripts',
            fontsDir: '<%= yeoman.app %>/styles/fonts',
            importPath: '<%= yeoman.app %>/components',
            relativeAssets: true
          },
          dist: {},
          server: {
            options: {
              debugInfo: true
            }
          }
        },
        concat: {
          dist: {
            files: {
              '<%= yeoman.dist %>/scripts/scripts.js': [
                '.tmp/scripts/{,*/}*.js',
                '<%= yeoman.app %>/scripts/{,*/}*.js'
              ]
            }
          }
        },
        useminPrepare: {
          html: '<%= yeoman.app %>/index.html',
          options: {
            dest: '<%= yeoman.dist %>'
          }
        },
        usemin: {
          html: ['<%= yeoman.dist %>/{,*/}*.html'],
          css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
          options: {
            dirs: ['<%= yeoman.dist %>']
          }
        },
        imagemin: {
          dist: {
            files: [{
              expand: true,
              cwd: '<%= yeoman.app %>/images',
              src: '{,*/}*.{png,jpg,jpeg}',
              dest: '<%= yeoman.dist %>/images'
            }]
          }
        },
        cssmin: {
          dist: {
            files: {
              '<%= yeoman.dist %>/styles/main.css': [
                '.tmp/styles/{,*/}*.css',
                '<%= yeoman.app %>/styles/{,*/}*.css'
              ]
            }
          }
        },
        htmlmin: {
          dist: {
            options: {
              /*removeCommentsFromCDATA: true,
              // https://github.com/yeoman/grunt-usemin/issues/44
              //collapseWhitespace: true,
              collapseBooleanAttributes: true,
              removeAttributeQuotes: true,
              removeRedundantAttributes: true,
              useShortDoctype: true,
              removeEmptyAttributes: true,
              removeOptionalTags: true*/
            },
            files: [{
              expand: true,
              cwd: '<%= yeoman.app %>',
              src: ['*.html', 'views/*.html'],
              dest: '<%= yeoman.dist %>'
            }]
          }
        },
        cdnify: {
          dist: {
            html: ['<%= yeoman.dist %>/*.html']
          }
        },
        ngmin: {
          dist: {
            files: [{
              expand: true,
              cwd: '<%= yeoman.dist %>/scripts',
              src: '*.js',
              dest: '<%= yeoman.dist %>/scripts'
            }]
          }
        },
        uglify: {
          dist: {
            files: {
              '<%= yeoman.dist %>/scripts/scripts.js': [
                '<%= yeoman.dist %>/scripts/scripts.js'
              ],
            }
          }
        },
        copy: {
          dist: {
            files: [{
              expand: true,
              dot: true,
              cwd: '<%= yeoman.app %>',
              dest: '<%= yeoman.dist %>',
              src: [
                '*.{ico,txt}',
                '.htaccess',
                'components/**/*',
                'images/{,*/}*.{gif,webp}'
              ]
            }]
          }
        }
      });
    
      grunt.renameTask('regarde', 'watch');
      // remove when mincss task is renamed
      grunt.renameTask('mincss', 'cssmin');
    
      grunt.registerTask('server', [
        'clean:server',
        'coffee:dist',
        'compass:server',
        'livereload-start',
        'connect:livereload',
        'open',
        'watch'
      ]);
    
      grunt.registerTask('test', [
        'clean:server',
        'coffee',
        'compass',
        'connect:test',
        'karma'
      ]);
    
      grunt.registerTask('build', [
        'clean:dist',
        'jshint',
        'test',
        'coffee',
        'compass:dist',
        'useminPrepare',
        'imagemin',
        'cssmin',
        'htmlmin',
        'concat',
        'copy',
        'cdnify',
        'usemin',
        'ngmin',
        'uglify'
      ]);
    
      grunt.registerTask('default', ['build']);
    };
    

3 个答案:

答案 0 :(得分:14)

您正在寻找的内容记录在:https://github.com/yeoman/grunt-usemin

简单地将css导入包装在注释块中,类似于使用javascript文件完成的方式

<!-- build:css styles/main.css -->
<link rel="stylesheet" href="styles/base.css">
<link rel="stylesheet" href="styles/modules.css">
<link rel="stylesheet" href="styles/layout.css">
<!-- endbuild -->

确保您的生成器是最新的,并且您的grunt任务都已设置完毕。做魔术的人是'useminPrepare'

答案 1 :(得分:6)

发展的真正魔力发生在

mountFolder(connect, '.tmp'),

代表connect-livereload middlewaregrunt-contrib-watch

这使本地服务器也提供 .tmp 文件夹的内容,这就是为什么你可以引用 styles / main.css 并获取 .tmp的原因/styles/main.css 作为回报。

通常不会在服务器任务中调用

useminPrepare

答案 2 :(得分:5)

你只需要包含它

<link rel="stylesheet" href="styles/style.css">

Yeoman / grunt会在运行服务器时知道它应该从temp文件夹中获取sass文件。