webpack module parse failed Unexpected character &

2019-04-28 02:18发布

I am working with vue-cli, after import a css file in main.js,

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)
})

the console logged an error and failed to render

VM818:1Uncaught Error: Module parse failed: C:\02-folks\new-version\vueproject\fademo01\node_modules.1.0.2@element-ui\lib\theme-default\index.css Unexpected character '@'...

I have installed the style-loader and css loader Here is the package.json file

{
"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"
  }
}

and configed css in 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'
  },
  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条回答
Juvenile、少年°
2楼-- · 2019-04-28 02:54

I just worked it out: remove the following line from the element for css configuration of module.rules in webpack.config.js

  {
    test: /\.css$/,
    loader: 'css-loader',
    exclude: /node_modules/   //remove this line
  },
查看更多
老娘就宠你
3楼-- · 2019-04-28 02:57

you need restart webpack-dev-server!! I also encountered this problem with the translation software to give you a reply.

查看更多
干净又极端
4楼-- · 2019-04-28 02:58

I had to do a few things:

npm install style-loader --save

I also was missing a few entries in webpack.config.js

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

These webpack.config.js entries are documented in the Quick Start of the element-ui documentation.

查看更多
登录 后发表回答