如何使用vue-cli webpack-simple模板使FontAwesome正常工作?

时间:2018-04-05 09:01:04

标签: npm webpack vue.js font-awesome

我使用vue-cli生成默认项目:

vue init webpack-simple grid-prototype

我通过npm安装FontAwesome:

npm install --save fontawesome

之后,我将其包含在main.js中:

import 'font-awesome/css/font-awesome.css'

我用:

执行应用程序
npm run dev

我收到了这个错误:

Failed to compile.

./node_modules/font-awesome/fonts/fontawesome-webfont.ttf?v=4.7.0
Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
 @ ./node_modules/css-loader!./node_modules/font-awesome/css/font-awesome.css 7:684-735
 @ ./node_modules/font-awesome/css/font-awesome.css
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8082 webpack/hot/dev-server ./src/main.js

这是我的webpack.config.js(这是cli创建的默认值):

var path = require("path");
var webpack = require("webpack");

module.exports = {
  entry: "./src/main.js",
  output: {
    path: path.resolve(__dirname, "./dist"),
    publicPath: "/dist/",
    filename: "build.js"
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["vue-style-loader", "css-loader"]
      },
      {
        test: /\.vue$/,
        loader: "vue-loader",
        options: {
          loaders: {}
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: "file-loader",
        options: {
          name: "[name].[ext]?[hash]"
        }
      }
    ]
  },
  resolve: {
    alias: {
      vue$: "vue/dist/vue.esm.js"
    },
    extensions: ["*", ".js", ".vue", ".json"]
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: "#eval-source-map"
};

if (process.env.NODE_ENV === "production") {
  module.exports.devtool = "#source-map";
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      "process.env": {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ]);
}

我做错了什么?

1 个答案:

答案 0 :(得分:2)

webpack-simple适用于快速原型设计,但没有高级规则。

解决方案#1 :使用webpack模板而不是webpack-simple来避免此问题及其他问题。

解决方案#2 :使用CDN中的FontAwesome,例如: https://www.bootstrapcdn.com/fontawesome/

我的意思是,只需将CDN css添加到index.html

即可

解决方案#3 :在您的webpack配置中再添加一条规则

module: {
  rules: [
   {
      test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
      loader: 'url-loader',
      options: {
        limit: 10000,
        name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
      }
   }
 ]

webpack模板中一样:

https://github.com/vuejs-templates/webpack/blob/develop/template/build/webpack.base.conf.js