webpack模块解析失败意外字符' @'

时间:2016-11-21 10:12:54

标签: webpack vue.js

在main.js导入css文件后,我正在使用vue-cli,

import Vue from 'vue'
import App from './App.vue'
import 'element-ui/lib/theme-default/index.css'
import ElementUI from 'element-ui'


Vue.use(ElementUI);

new Vue({
  el: '#app',
  render: h => h(App)
})

控制台记录了一个错误,无法呈现

  

VM818:1未捕获错误:模块解析失败:C:\ 02-folks \ new-version \ vueproject \ fademo01 \ node_modules.1.0.2@element-ui \ lib \ theme-default \ index.css意外字符&# 39; @' ...

我已经安装了样式加载器和css加载器 这是package.json文件

{
"name": "fademo01",
  "description": "A Vue.js project",
  "author": "",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --open --inline --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "dependencies": {
    "vue": "^2.0.1"
  },
  "devDependencies": {
    "babel-core": "^6.0.0",
    "babel-loader": "^6.0.0",
    "babel-preset-es2015": "^6.0.0",
    "cross-env": "^3.0.0",
    "css-loader": "^0.25.0",
    "element-ui": "^1.0.2",
    "file-loader": "^0.9.0",
    "style-loader": "^0.13.1",
    "vue-loader": "^9.7.0",
    "webpack": "^2.1.0-beta.25",
    "webpack-dev-server": "^2.1.0-beta.0"
  }
}

并在webpack.config.js中配置了css

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: /\.vue$/,
        loader: 'vue-loader',
        options: {
          // vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        loader: 'css-loader',
        exclude: /node_modules/
      },
       {
        test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
        loader: 'file-loader'
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue'
    }
  },
  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({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

3 个答案:

答案 0 :(得分:8)

我必须做一些事情:

npm install style-loader --save

我也错过了webpack.config.js中的一些条目

module:
...
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
        loader: 'file-loader'
      },

这些webpack.config.js条目记录在element-ui文档的Quick Start中。

答案 1 :(得分:1)

我刚刚解决了这个问题:  从webpack.config.js

中的module.rules的css配置元素中删除以下行
  {
    test: /\.css$/,
    loader: 'css-loader',
    exclude: /node_modules/   //remove this line
  },

答案 2 :(得分:1)

你需要重启webpack-dev-server !!我也在翻译软件中遇到了这个问题,给你回复。