Fontawesome is not working when project is built with grunt

前端 未结 15 1130
余生分开走
余生分开走 2020-12-02 07:50

I\'m using the font library font awesome. It works when the project is not built/uglified with grunt.

But when I\'m building the project with grunt it\'s not workin

相关标签:
15条回答
  • 2020-12-02 08:00

    I was having the very same problem. I took a look a the bower.json file for font-awesome and found this:

    {
      "name": "font-awesome",
      "description": "Font Awesome",
      "keywords": [],
      "homepage": "http://fontawesome.io",
      "dependencies": {},
      "devDependencies": {},
      "license": ["OFL-1.1", "MIT", "CC-BY-3.0"],
      "main": [
        "less/font-awesome.less",
        "scss/font-awesome.scss"
      ],
      "ignore": [
        "*/.*",
        "*.json",
        "src",
        "*.yml",
        "Gemfile",
        "Gemfile.lock",
        "*.md"
      ]
    }
    

    There was no reference to the "font-awesome.css" in the "main" array. Perhaps, like me, you're not using SASS or LESS for styling. So no styles are being added for font-awesome. I modified the json file as follows:

    {
      "name": "font-awesome",
      "description": "Font Awesome",
      "keywords": [],
      "homepage": "http://fontawesome.io",
      "dependencies": {},
      "devDependencies": {},
      "license": ["OFL-1.1", "MIT", "CC-BY-3.0"],
      "main": [
        "less/font-awesome.less",
        "scss/font-awesome.scss",
        "css/font-awesome.css",
        "fonts/fontawesome-webfont.tff",
        "fonts/fontawesome-webfont.woff",
        "fonts/fontawesome-webfont.woff2"
      ],
      "ignore": [
        "*/.*",
        "*.json",
        "src",
        "*.yml",
        "Gemfile",
        "Gemfile.lock",
        "*.md"
      ]
    }
    

    I saved and ran grunt serve, and now my font-awesome icons show up.

    Hope this helps.

    0 讨论(0)
  • 2020-12-02 08:03

    Hi the main issue is that the font files required by font-awesome css don't get copied after you run the grunt task and you may get 404 not found error the same can be verified if you open your chrome developer mode and look in the consoe or networks tab. The same issue may occur for bootstrap aswell.

    Hence we need to modify the grunt task so that the all the font files get copied.

    Add seperate copy task for font files .

    copy: {
      dist: { .....
    
      },
       fonts: {
        expand: true,
        flatten: true,
        cwd: '.',
        src: ['bower_components/bootstrap/fonts/*', 'bower_components/font-awesome/fonts/*'],
        dest: '<%= yeoman.dist %>/fonts',
        filter: 'isFile'
      },
      styles: { .......
      }
    } 
    

    Register the 'copy:fonts' task in grunt.registerTask so that it gets executed at the build time.

    0 讨论(0)
  • 2020-12-02 08:04

    I was able to fix the problem by adding the following to copy.dist.files:

    {
       expand: true,
       flatten: true,
       src: 'bower_components/components-font-awesome/fonts/*',
       dest: 'dist/fonts' 
    }
    
    0 讨论(0)
  • 2020-12-02 08:05

    Here is the solution: https://stackoverflow.com/a/32128931/3172813

    { 
      expand: true,
      cwd: '<%= yeoman.app %>/bower_components/font-awesome', 
      src: 'fonts/*', 
      dest: '<%= yeoman.dist %>' 
    }
    
    0 讨论(0)
  • 2020-12-02 08:09

    As answered above this one worked very well for me too

     // 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',
                'scripts/components/{,*/}*.html',
                'images/{,*/}*.{webp,png,jpg,jpeg,gif}',
                'fonts/*',
                'styles/fonts/{,*/}*.*',
                'services/{,*/}*.json',
                'services/mocks/{,*/}*.json'
              ]
            }, {
              expand: true,
              cwd: '.tmp/images',
              dest: '<%= yeoman.dist %>/images',
              src: ['generated/*']
            }, {
              expand: true,
              cwd: '.tmp/concat/scripts',
              dest: '<%= yeoman.dist %>/scripts',
              src: '{,*/}*.js'
            }, {
              expand: true,
              cwd: '.tmp/concat/styles',
              dest: '<%= yeoman.dist %>/styles',
              src: '{,*/}*.css'
            }, {
              expand: true,
              cwd: '<%= yeoman.app %>',
              src: 'styles/Bootstrap/fonts/bootstrap/*',
              dest: '<%= yeoman.dist %>'
            }, {
              expand: true,
              cwd: 'bower_components/font-awesome/fonts/',
              src: ['*.*'],
              dest: '<%= yeoman.dist %>/fonts'
            }]
          }
    
    0 讨论(0)
  • 2020-12-02 08:13

    I had the same problem. The following code solved my problem.

    copy: {
        dist: {
            files: [{
                expand: true,
                dot: true,
                cwd: '<%= config.app %>',
                dest: '<%= config.dist %>',
                src: [
                    '*.{ico,png,txt}',
                    '.htaccess',
                    'images/{,*/}*.webp',
                    '{,*/}*.html',
                    'styles/fonts/{,*/}*.*'
                ]
            },{
                expand: true,
                dot: true,
                cwd: 'bower_components/bootstrap/dist', // change this for font-awesome
                src: ['fonts/*.*'],
                dest: '<%= config.dist %>'
            }]
        }
    }
    
    0 讨论(0)
提交回复
热议问题