2017-09-28 3 views
0

Je voudrais passer de gulp à webpack totalement mais je suis toujours à la recherche d'une bonne solution.de gulp iconfontCss à Webpack

Qu'est-ce que cette tâche engouffreur atteindre:

  • prend toutes svgs dans src/assets/icons/**/*
  • Créer des polices (TTF, EOT, woff ...)
  • Generate grâce à un cours src/assets/css/icons_template.scss la création du fichier icons.scss pour chaque icone

Mon Gulpfile

var gulp = require('gulp'); 
var iconfont = require('gulp-iconfont'); 
var iconfontCss = require('gulp-iconfont-css'); 

gulp.task('icons', function() { 
    return gulp.src('src/assets/icons/**/*') 
     .pipe(iconfontCss({ 
      fontName: 'myapp-icons', 
      path: 'src/assets/css/icons_template.scss', 
      fontPath: '../fonts/icons/', 
      targetPath: '../../css/icons.scss', 
      cssClass: 'mu-icon' 
     })) 
     .pipe(iconfont({ 
      fontName: 'myapp-icons', 
      formats: ['ttf', 'eot', 'woff', 'woff2', 'svg'], 
      normalize: true, 
      centerHorizontally: true 
     })) 
     .pipe(gulp.dest('src/assets/fonts/icons')) 
}); 

gulp.task('default', function() { 
    gulp.start('icons'); 
}); 

Le modèle i utiliser:

//src/assets/css/icons_template.scss 
@font-face { 
    font-family: "<%= fontName %>"; 
    src: url(<%= fontPath %><%= fontName %>.eot); 
    src: url(<%= fontPath %><%= fontName %>.eot?#iefix) format('eot'), 
    url(<%= fontPath %><%= fontName %>.woff2) format('woff2'), 
    url(<%= fontPath %><%= fontName %>.woff) format('woff'), 
    url(<%= fontPath %><%= fontName %>.ttf) format('truetype'), 
    url(<%= fontPath %><%= fontName %>.svg#<%= fontName %>) format('svg'); 
} 

@mixin <%= cssClass%>-styles { 
    font-family: "<%= fontName %>"; 
    -webkit-font-smoothing: antialiased; 
    -moz-osx-font-smoothing: grayscale; 
    font-style: normal; 
    font-variant: normal; 
    font-weight: normal; 
    // speak: none; // only necessary if not using the private unicode range (firstGlyph option) 
    text-decoration: none; 
    text-transform: none; 
} 

%<%= cssClass%> { 
    @include <%= cssClass%>-styles; 
} 

@function <%= cssClass%>-char($filename) { 
    $char: ""; 
<% _.each(glyphs, function(glyph) { %> 
    @if $filename == <%= glyph.fileName %> { 
    $char: "\<%= glyph.codePoint %>"; 
}<% }); %> 

@return $char; 
} 

@mixin <%= cssClass%>($filename, $insert: before, $extend: true) { 
&:#{$insert} { 
    @if $extend { 
    @extend %<%= cssClass%>; 
    } @else { 
    @include <%= cssClass%>-styles; 
    } 
    content: <%= cssClass%>-char($filename); 
} 
} 

<% _.each(glyphs, function(glyph) { %>.<%= cssClass%>-<%= glyph.fileName %> { 
    @include <%= cssClass%>(<%= glyph.fileName %>); 
} 
<% }); %> 

Répondre

0

Voici ce que je réussi à faire avec webpack:

Utilisez webfonts-loader

Ajouter un fichier myapp.font.js

module.exports = { 
    'files': [ 
     './icons/*.svg' 
    ], 
    'cssTemplate': './fonts/myapp_icons_template.css.tpl', 
    'fontName': 'myapp-icons', 
    'classPrefix': 'myapp-icon-', 
    'baseSelector': '.myapp-icon', 
    'types': ['eot', 'woff', 'woff2', 'ttf', 'svg'], 
    'fileName': 'myapp-icons.[ext]' 
}; 

Ajoutez ceci à mes chargeurs webpack après avoir importé le fichier précédent:

 { 
      test: /\.font\.js/, 
      loaders: [ 
       'style-loader', 
       'css-loader', 
       'webfonts-loader' 
      ] 
     }, 

Et puis le style est ton copié directement index.html