【问题标题】:Yeoman angular: Sass compilation extremly slowYeoman angular:Sass 编译速度极慢
【发布时间】:2014-08-07 13:46:42
【问题描述】:

我正在使用最新的 Yeoman 角度生成器来启动一个项目。我想使用 Yeoman 默认提供的 SASS 和 Compass。

Compass 的最大缺点是速度非常慢。我有 Twitter Bootstrap(SASS 版本),我的样式文件夹中有三个 .scss 文件,编译大约需要 10-12 秒。使用直播时会稍微快一些(大约一秒)。

但是每次更改样式文件时等待 10-12 秒将是疯狂的。我环顾四周,发现了一些帖子(其中大部分是一岁左右),但它们对我的帮助不大。我还发现了这个项目https://github.com/sindresorhus/grunt-sass,其中原来的https://github.com/gruntjs/grunt-contrib-sass 被替换为C++ 版本,而不是Ruby 编译。但缺点是,它不支持 Compass。

我希望有人可以帮助我,加快速度。

这是我的设置:

我正在使用 grunt serve 启动我的 grunt 服务器,实时观看 *.js, *.scss 文件,并在文件更改时触发重新加载。这是我的Gruntfile.js

// Generated on 2014-06-16 using generator-angular 0.9.0-1
'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);

  // Configurable paths for the application
  var appConfig = {
    app: require('./bower.json').appPath || 'app',
    dist: 'dist'
  };

  // Define the configuration for all the tasks
  grunt.initConfig({

    // Project settings
    yeoman: appConfig,

    // Watches files for changes and runs tasks based on the changed files
    watch: {
      bower: {
        files: ['bower.json'],
        tasks: ['wiredep']
      },
      js: {
        files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],
        tasks: ['newer:jshint:all'],
        options: {
          livereload: '<%= connect.options.livereload %>'
        }
      },
      jsTest: {
        files: ['test/spec/{,*/}*.js'],
        tasks: ['newer:jshint:test', 'karma']
      },
      compass: {
        files: ['<%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
        tasks: ['compass:server', 'autoprefixer'],
        options: {
            spawn: false,
        }
      },
      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: false,
          middleware: function (connect) {
            return [
              connect.static('.tmp'),
              connect().use(
                '/bower_components',
                connect.static('./bower_components')
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      test: {
        options: {
          port: 9001,
          middleware: function (connect) {
            return [
              connect.static('.tmp'),
              connect.static('test'),
              connect().use(
                '/bower_components',
                connect.static('./bower_components')
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      dist: {
        options: {
          open: false,
          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: {
        src: [
          '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
    wiredep: {
      app: {
        src: ['<%= yeoman.app %>/index.html'],
        ignorePath: new RegExp('^<%= yeoman.app %>/|../')
      },
      sass: {
        src: ['<%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
        ignorePath: /(\.\.\/){1,2}bower_components\//
      }
    },

    // Compiles Sass to CSS and generates necessary files if requested
    compass: {
      options: {
        sassDir: '<%= yeoman.app %>/styles',
        cssDir: '.tmp/styles',
        generatedImagesDir: '.tmp/images/generated',
        imagesDir: '<%= yeoman.app %>/images',
        javascriptsDir: '<%= yeoman.app %>/scripts',
        fontsDir: '<%= yeoman.app %>/styles/fonts',
        importPath: './bower_components',
        httpImagesPath: '/images',
        httpGeneratedImagesPath: '/images/generated',
        httpFontsPath: '/styles/fonts',
        relativeAssets: false,
        assetCacheBuster: false,
        raw: 'Sass::Script::Number.precision = 10\n'
      },
      dist: {
        options: {
          generatedImagesDir: '<%= yeoman.dist %>/images/generated'
        }
      },
      server: {
        options: {
          debugInfo: true
        }
      }
    },

    // Renames files for browser caching purposes
    filerev: {
      dist: {
        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 filerev and the useminPrepare configuration
    usemin: {
      html: ['<%= yeoman.dist %>/{,*/}*.html'],
      css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
      options: {
        assetsDirs: ['<%= yeoman.dist %>','<%= yeoman.dist %>/images']
      }
    },

    // The following *-min tasks will produce minified files in the dist folder
    // 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'
    //       ]
    //     }
    //   }
    // },
    // uglify: {
    //   dist: {
    //     files: {
    //       '<%= yeoman.dist %>/scripts/scripts.js': [
    //         '<%= yeoman.dist %>/scripts/scripts.js'
    //       ]
    //     }
    //   }
    // },
    // concat: {
    //   dist: {}
    // },

    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,
          conservativeCollapse: 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/*']
        }, {
          expand: true,
          cwd: '.',
          src: 'bower_components/bootstrap-sass-official/vendor/assets/fonts/bootstrap/*',
          dest: '<%= yeoman.dist %>'
        }]
      },
      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: [
        'compass:server'
      ],
      test: [
        'compass'
      ],
      dist: [
        'compass:dist',
        'imagemin',
        'svgmin'
      ]
    },

    // Test settings
    karma: {
      unit: {
        configFile: 'test/karma.conf.js',
        singleRun: false
      }
    }
  });


  grunt.loadNpmTasks('grunt-sass');

  grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }
    grunt.task.run([
      'clean:server',
      'wiredep',
      'concurrent:server',
      'autoprefixer',
      'connect:livereload',
      'watch'
    ]);
  });

  grunt.registerTask('server', 'DEPRECATED TASK. Use the "serve" task instead', 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',
    'autoprefixer',
    'connect:test',
    'karma'
  ]);

  grunt.registerTask('build', [
    'clean:dist',
    'wiredep',
    'useminPrepare',
    'concurrent:dist',
    'autoprefixer',
    'concat',
    'ngmin',
    'copy:dist',
    'cdnify',
    'cssmin',
    'uglify',
    'filerev',
    'usemin',
    'htmlmin'
  ]);

  grunt.registerTask('default', [
    'newer:jshint',
    'test',
    'build'
  ]);
};

当启动我的 grunt 服务器时,我得到这个输出:

Running "serve" task

Running "clean:server" (clean) task
Cleaning .tmp...OK

Running "wiredep:app" (wiredep) task

Running "wiredep:sass" (wiredep) task

Running "concurrent:server" (concurrent) task

    Running "compass:server" (compass) task
    directory .tmp/styles/ 
       create .tmp/styles/colors.css (0.173s)
       create .tmp/styles/main.css (5.485s)
       create .tmp/styles/styles.css (5.725s)
    Compilation took 11.39s

    Done, without errors.


    Execution Time (2014-06-17 13:43:30 UTC)
    compass:server  12.9s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 100%
    Total 12.9s

Running "autoprefixer:dist" (autoprefixer) task
File .tmp/styles/colors.css created.
File .tmp/styles/main.css created.
File .tmp/styles/styles.css created.

Running "connect:livereload" (connect) task
Started connect web server on http://localhost:9000

Running "watch" task
Waiting...

文件更改显示此日志:

>> File "app/styles/colors.scss" changed.

Running "compass:server" (compass) task
identical .tmp/styles/colors.css (0.033s)
overwrite .tmp/styles/main.css (4.074s)
overwrite .tmp/styles/styles.css (4.548s)
Compilation took 9.216s

Running "autoprefixer:dist" (autoprefixer) task
File .tmp/styles/colors.css created.
File .tmp/styles/main.css created.
File .tmp/styles/styles.css created.

Running "watch" task
Completed in 12.515s at Tue Jun 17 2014 15:48:47 GMT+0200 (CEST) - Waiting...

如您所见,初始启动大约需要 13 秒,文件更改大约需要 9 秒。

任何想法,如何加快速度?


更新

我玩了一下,这可能会有所帮助。它本身并没有解决编译缓慢的问题,但它加快了速度:

  • 如果您不需要 Bootstrap SASS 版本,请跳过它并切换到普通 CSS 版本。您的编译时间将显着减少
  • 如果您需要 SASS 版本,请查看 bootstrap.scss 文件(在您的 bower_components/bootstrap-sass-official 文件夹中,或者如果您手动下载了它,则放置它)。也许有些组件你不需要,所以你可以跳过一些文件。在您的主 SCSS 文件中,您将包含如下引导程序:@import "bootstrap-sass-official/vendor/assets/stylesheets/bootstrap.scss";。查看此文件,仅将您需要的内容复制到主 SCSS 文件中(并确保更改路径)。
  • 确保正确使用@import 语句。默认情况下,文件名前面没有_ 的所有内容都会被 Grunt 编译和包含。通常你会为你的整个应用程序创建一个像styles.scss 这样的文件,或者如果你想将它分成几个page1.scss, page2.scss... 文件。如果您包含其他文件,例如 @import 'colors.scss';,请确保文件名是 _colors.scss。否则,colors.scss 也会被 Grunt 编译,然后由于 @import 语句而再次编译。这会减慢您的编译过程。

但同样,这只是变通方法,并不能加速编译本身。所以,仍在寻找解决方案。

【问题讨论】:

  • 找一样的,我们组投票给了foundationcss和compass,所以不得不使用grunt-contrib-sass(Ruby)。希望以后能说服大家使用bourbon + grunt-sass(libsass)。
  • @Vincent:我更新了我的答案并添加了一些提示以降低 SASS 编译过程的复杂性。也许这对你也有帮助(如果你找到了任何超高速 sass 编译库,请告诉我;)
  • 对于foundationcss,我实际上可以使用ruby Foundation gem 来生成带有grunt 文件的-libsass 版本。我可以将它与我的角度生成器 grunt 文件合并并摆脱指南针。 (还没做)
  • @23tux 您是在使用某种虚拟化(Virtualbox、VMware 还是不知道它们但使用 Vagrant?)还是这个问题出现在您的主机(台式机)上?

标签: sass gruntjs yeoman compass-sass


【解决方案1】:

您可以将 compass 替换为与 libsass 完全兼容的 compass-mixins,并使用 compass-importer 将所有原始 compass @import 更新为新的。

当我将一些 compass 项目迁移到 libsass 时,我将编译时间从大约 30 秒减少到了大约 350 毫秒。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2015-02-07
    • 2011-03-30
    相关资源
    最近更新 更多