我很难通过引导的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
的{{1}}并未考虑App.vue
所要求的main.js
中的要求。我在另一个SO答案中遇到vue-html-loader
,但我猜这是旧版本的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-env
和file-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
的地方,那就太棒了!