2013-03-25 15 views
37

Provo ad usare yeoman ma non so come usare i miei file sass con esso.Che cos'è il flusso di lavoro in Yeoman per lavorare con i file Sass?

Con

file

grunt server

Sass sono guardati e compilati in

.tmp/styles/

Ma non v'è alcun riferimento al foglio di stile compilato, ad eccezione <link rel="stylesheet" href="styles/main.css">

Quindi, qual è il modo consigliato di usare i file sass compilati in ind ex.html durante lo sviluppo?

E.g. grunt server, se cambio il mio stile app/styles/my.sass in .tmp/styles/my.css, questo viene sovrascritto ed è esterno al server (localhost: 9000). Pertanto, è impossibile collegarlo in index.html.

Con grunt build è tutto all'interno main.css includono my.sass ma durante lo sviluppo non so come usare i miei file Sass in index.html.

Potete darmi un esempio semplice?

È l'installazione predefinita di yeoman. Ho fatto questo:

  1. yo angular test
  2. aggiungo app/styles/style.sass
  3. grunt server questa compilare style.sass in .tmp/styles/style.css
  4. ora non so come includere style.css in html

(mi dispiace può essere questa è una domanda stupida ma sono nuovo anche in yeoman e grugnito)

Questo è Gruntfile.js da Yeoman:

'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']); 
}; 
+0

Posso vedere il tuo gruntfile? –

+0

Ho aggiunto la risposta sotto – MarekLi

+0

Dovrebbe essere nella tua domanda ;-) Risposte è solo per le risposte alla domanda originale, non i commenti. –

risposta

14

Quello che state cercando è documentata su: https://github.com/yeoman/grunt-usemin

Basta avvolgere i vostri importazioni CSS in un blocco di commento in modo simile al modo in cui è fatto con i file 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 --> 

assicurarsi che il generatore è in su fino ad oggi e tutti i tuoi compiti sono stati impostati.Quello che fa la magia è 'useminPrepare'

+0

grazie questo è esattamente quello che stavo cercando – MarekLi

+0

Non capisco perché - useminPrepare di solito non viene mai chiamato nel task _server_. Nella produzione (compito di costruzione) sì, questo è importante da capire. – Andy

5

Basta includerlo come

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

Yeoman/grugnito si sa quando si esegue il server che deve prelevare il file sass dalla cartella Temp .

+0

Non di default. Ma puoi aggiungere un grunt-task come 'grunt-text-replace' che può essere usato per cercare/sostituire all'interno di file di testo. Ma perché vuoi diversi file SASS inclusi nel tuo index.html durante il tempo di sviluppo? – acme

+0

Non lo voglio. Ma in default 'grunt build' unisce tutti i file * css e * .sass da' app/style' a 'dist/styles/main.css', quindi dopo' grunt build' sono altri file css e sass inutili tranne 'main. css'. Forse non capivo come si intendesse il processo di creazione di yeoman, perché se "grunt-text-replace" era necessario, i creatori di IMHO di yeoman lo usano durante "build grunt". Quindi questa era la mia domanda sopra in altre parole: come yeoman intendeva usare i miei file css, sass? – MarekLi

6

La magia reale per lo sviluppo sta accadendo in

mountFolder(connect, '.tmp'), 

per la connect-livereload middleware per grunt-contrib-watch.

Questo rende il server locale servono anche il contenuto della cartella tmp, che è il motivo per cui è possibile fare riferimento stili/main.css e ottenere tmp/stili/main.css in cambio.

useminPrepare di solito è mai chiamato in compito server.

+1

Bello! Capisco il flusso di lavoro grugnito dalla tua risposta. '' ' livereload: { opzioni: { aperto: vero, di base: [ 'tmp', '<% = yeoman.app%>' ] }} , ' '' – rahmat

Problemi correlati