1

vue-cli我很难在一个自举项目中找出一些功能。我不想使用,webpack-dev-server因为我想使用我自己的快速应用程序来提供index.html文件和build.js文件。所以我决定从头开始使用 Webpack 设置 Vue 2.0。

我在运行时遇到了错误webpack

ERROR in ./src/App.vue
Module parse failed: C:\node\aaaaa\customer\src\App.vue Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| <template lang="html">
|   <div id="app">
|     <h1>This is from App.vue</h1>
 @ ./src/main.js 2:0-27

我不断收到此错误,从外观上看,它vue-loader并没有考虑App.vuemain.js. 我遇到vue-html-loader了另一个 SO 答案,但我想那是针对旧版本的 Vue。

my-project
├──src
|  ├──App.vue
|  ├──main.js
├──node_modules
├──package.json
├──webpack.config.js

App.vue和从版本中保持main.js不变。vue-cli

这是我的package.json依赖项,它与原始 vue-cli 依赖项的不同之处在于不安装webpack-dev-server,cross-envfile-loader.

"dependencies": {
  "vue": "^2.1.0"
},
"devDependencies": {
  "babel-core": "^6.0.0",
  "babel-loader": "^6.0.0",
  "babel-preset-es2015": "^6.0.0",
  "css-loader": "^0.25.0",
  "vue-loader": "^10.0.0",
  "vue-template-compiler": "^2.1.0",
  "webpack": "^2.1.0-beta.25"
}

我的webpack.config.js不包含我认为不必要的 vue-cli 版本中的某些部分。

module.exports = {
  entry: './src/main.js',
  output: {
    path: __dirname + '/dist',
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        include: './src',
        options: {
          // vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: './src',
        exclude: /node_modules/,
        query: { //https://github.com/babel/babel-loader
          presets: ['es2015']
        }
      }
    ]
  }
};

该项目的webpack.config.js文件vue-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: /\.vue$/,
        loader: 'vue-loader',
        options: {
          // 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.common.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({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

任何帮助,将不胜感激。如果你能指导我到一个我可以学习使用vue-cli脚手架项目的地方,并且express没有webpack-dev-server.,那就太好了!

4

0 回答 0