1.新建 .gitignore 输入
distnode_modules
2.
npm init -ynpm i webpack webpack-cli -S
3.新建 webpack.config.js
4.修改package.json 文件
5.初次打包
- 新建src文件夹
- 新建文件index.js
- 输入console.log(“hello world”)
- 新建文件index.js
- 在webpack.config.js中输入
const path = require('path')const config = {entry:path.join(__dirname,'src/index.js'),output:{/* 打包的文件名 */filename:"[hash]-[name]-bundle.js",/* 打包文件的输出目录 */path:path.join(__dirname,'dist')},mode:"development"}module.exports = config;
- 输入 npm run build
6.配置plugins
- 安装 npm i html-webpack-plugin -S
导入HtmlWebpackPluginplugins:[new HtmlWebpackPlugin({title:"webpack打包"})]
输入 npm run build
安装 npm i clean-webpack-plugin -S
导入{CleanWebpackPlugin}plugins:[new HtmlWebpackPlugin({title:"webpack打包"}),new CleanWebpackPlugin()]
- 输入 npm run build
- webpack-server
- npm i webpack-dev-server -S
- npm i cross-env -S
- 配置package.json
"scripts": {"build": "cross-env NODE_ENV=production webpack --config webpack.config.js","serve":"cross-env NODE_ENV=development webpack-dev-server --config webpack.config.js"},
- 配置webpack.config.js
const isDev = process.env.NODE_ENV ==='development'if(isDev){config.devServer = {port:8080,host:'localhost',overlay:{errors:true},hot:true}}//npm run serve
8.loader
- 处理vue \jpg|png\ css
- npm i css-loader style-loader -S
- npm i vue-loader vue vue-template-compiler -S
- npm i file-loader -S
const {VueLoaderPlugin} = require('vue-loader');const config = {plugins:[new VueLoaderPlugin()],module:{rules:[{test:/\.css$/,use:['style-loader','css-loader']},{test:/\.vue$/,use:'vue-loader'}]},mode:"development"}
9.配置src/index.js
import Vue from 'vue';import '../assets/index.css';import App from './App.vue';const root = document.createElement("div");document.body.append(root)new Vue({render:h=>h(App)}).$mount(root)
配置src/App.vue
<template><div>hello world</div></template><script>export default {name:"App"}</script><style scoped></style>
配置assets/index.css
*{margin:0;padding:0}body{background: red;}
npm run serve
10.请求网络数据
index.js
//npm i axios -Simport Vue from 'vue';import '../assets/index.css';import App from './App.vue';import axios from 'axios';Vue.prototype.axios = axios;const root = document.createElement("div");document.body.append(root)new Vue({render:h=>h(App)}).$mount(root)
配置App.vue
<template><div><div v-for="item of musics" :key="item.id"><p>{{item.name}}</p><img :src="item.coverImgUrl" alt=""></div></div></template><script>export default {name:"App",data(){return{musics:[]}},mounted(){var url = "http://192.168.14.49:3000/top/playlist?cat=日语"this.axios(url).then(res=>{this.musics = res.data.playlists;})}}</script><style scoped></style>
