You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #Browsersync - Grunt, SASS & Autoprefixer
  2. ## Installation/Usage:
  3. To try this example, follow these 4 simple steps.
  4. **Step 1**: Clone this entire repo
  5. ```bash
  6. $ git clone https://github.com/Browsersync/recipes.git bs-recipes
  7. ```
  8. **Step 2**: Move into the directory containing this example
  9. ```bash
  10. $ cd bs-recipes/recipes/grunt.sass.autoprefixer
  11. ```
  12. **Step 3**: Install dependencies
  13. ```bash
  14. $ npm install
  15. ```
  16. **Step 4**: Run the example
  17. ```bash
  18. $ npm start
  19. ```
  20. ### Additional Info:
  21. This example shows how you can chain potentially slow-running tasks, but still achieve CSS
  22. Injection. The trick, as seen below, is to use the `bsReload` task that now comes
  23. bundled with `grunt-browser-sync` since `2.1.0`
  24. Don't forget the `spawn: false` option for the watch task - it's a requirement
  25. that allows Browsersync to work correctly
  26. ```js
  27. watch: {
  28. options: {
  29. spawn: false // Important, don't remove this!
  30. },
  31. files: 'app/**/*.scss',
  32. tasks: ['sass', 'autoprefixer', 'bsReload:css']
  33. },
  34. ```
  35. ### Preview of `Gruntfile.js`:
  36. ```js
  37. module.exports = function (grunt) {
  38. grunt.initConfig({
  39. dirs: {
  40. css: "app/css",
  41. scss: "app/scss"
  42. },
  43. watch: {
  44. options: {
  45. spawn: false
  46. },
  47. sass: {
  48. files: '<%= dirs.scss %>/**/*.scss',
  49. tasks: ['sass', 'autoprefixer', 'bsReload:css']
  50. },
  51. html: {
  52. files: 'app/*.html',
  53. tasks: ['bsReload:all']
  54. }
  55. },
  56. sass: {
  57. dev: {
  58. files: {
  59. '<%= dirs.css %>/main.css': '<%= dirs.scss %>/main.scss'
  60. }
  61. }
  62. },
  63. autoprefixer: {
  64. options: {
  65. browsers: ['last 5 versions', 'ie 8']
  66. },
  67. css: {
  68. src: '<%= dirs.css %>/main.css',
  69. dest: '<%= dirs.css %>/main.css'
  70. }
  71. },
  72. browserSync: {
  73. dev: {
  74. options: {
  75. server: "./app",
  76. background: true
  77. }
  78. }
  79. },
  80. bsReload: {
  81. css: {
  82. reload: "main.css"
  83. },
  84. all: {
  85. reload: true
  86. }
  87. }
  88. });
  89. // load npm tasks
  90. grunt.loadNpmTasks('grunt-contrib-sass');
  91. grunt.loadNpmTasks('grunt-autoprefixer');
  92. grunt.loadNpmTasks('grunt-browser-sync');
  93. grunt.loadNpmTasks('grunt-contrib-watch');
  94. // define default task
  95. grunt.registerTask('default', ['browserSync', 'watch']);
  96. };
  97. ```