webpack+vue实现todolist-项目配置

2018-06-14  本文已影响0人  kayleeWei
npm init
npm i webpack vue vue-loader
npm i css-loder vue-template-compiler
const path = require('path')  // path 为nodejs自带的包

module.exports = {
    entry: path.join(__dirname, 'src/index.js'), // __dirname为项目根路径,通过join得到绝对路径
    output: {
        filename: 'bundle.js',
        path:path.join(__dirname, 'dist')
    }
}
import Vue from 'vue'
import App from './app.vue'

const root = document.createElement('div')
document.body.appendChild(root)

// 将vue文件变为html,挂载到body之中
new Vue({
    render: (h) => h(App)
}).$mount(root)
{
  "name": "webpack-practice",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config webpack.config.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "css-loader": "^0.28.11",
    "vue": "^2.5.16",
    "vue-loader": "^15.2.4",
    "vue-template-compiler": "^2.5.16",
    "webpack": "^4.12.0"
  }
}
# 会报错,需要为.vue类型声明一个loader
npm run build

因为webpack只支持js语法,且支持到es5,因此在package.config.js中加一些规则(module部分),如下

module.exports = {
    entry: path.join(__dirname, 'src/index.js'),
    output: {
        filename: 'bundle.js',
        path:path.join(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /.vue$/,
                loader: 'vue-loader'
            }
        ]
    }
}
上一篇 下一篇

猜你喜欢

热点阅读