DukeAnn
做一个优雅的程序员

Vue引入elementUI报错 Unexpected character ‘@’ (1:0)

安装这个插件是按照官方文档给出的教程居然报错,OMG

import Element from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
Vue.use(Element)

运行时报错:Uncaught Error: Module parse failed: /Users/**/Desktop/vue2/node_modules/.1.0.0-rc.5@element-ui/lib/theme-default/index.css Unexpected character ‘@’ (1:0)

You may need an appropriate loader to handle this file type.

下面给出解决方案。

官网文档又有坑了,安装教程也不跟我们说这一步,当我们都是高手了…
报错是由于我们引入了index.css这个 CSS 文件,但是 webpack 打包的时候无法识别并转换成 js,所以就需要配置才能读取 css 和字体文件,运行命令安装下面三个东西(如果之前安装过就不需要了)

cnpm install style-loader –save-dev
cnpm install css-loader –save-dev
cnpm install file-loader –save-dev

webpack.config.js 中的 loaders 数组加入以下配置,记得该加逗号的地方加逗号!

{
    test: /\\.css$/,
    loader: "style!css"
},
{
    test: /\\.(eot|woff|woff2|ttf)([\\?]?.*)$/,
    loader: "file"
}

修改完的 webpack.config.js 如下

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'
  },
  resolveLoader: {
    root: path.join(__dirname, 'node_modules'),
  },
  module: {
    loaders: [
      {
        test: /\\.vue$/,
        loader: 'vue'
      },
      {
        test: /\\.js$/,
        loader: 'babel',
        exclude: /node_modules/
      },
      {
          test: /\\.css$/,
          loader: "style!css"
      },
      {
        test: /\\.(eot|woff|woff2|ttf)([\\?]?.*)$/,
        loader: "file"
      },
      {
        test: /\\.(png|jpg|gif|svg)$/,
        loader: 'file',
        query: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {vue: 'vue/dist/vue.js'}
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  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({
      compress: {
        warnings: false
      }
    })
  ])
}
DukeAnn的笔记本:DukeAnn的博客 » Vue引入elementUI报错 Unexpected character ‘@’ (1:0)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址