首先要安装node
命令行运行
npm init -y (y --》代表yes ,省去了默认选项点击)
然后命令行中一顿回车
完成后,生成一个package.json文件
npm install webpack webpack-cli --save-dev
①、在子文件夹public中新建index.html(此文件为入口文件模板,注意是模板)
②、在子文件夹src内新建main.js文件(此文件为项目入口主js文件)
③、webpack-config.js
const path = require('path');
module.exports = {
entry:{
app:'./src/main.js' //需要打包的文件
},
output: {
filename: 'bundle.js',//输入的文件名是什么,dist里面生成的文件名也是什么
path: path.resolve(__dirname, 'dist')//指定生成的文件目录
}
};
安装html-webpack-plugin是用于扩展webpack的功能
安装clean-webpack-plugin插件用于清除目录内容
也可以在打包之前使用这个插件尝试清除dist目录下的文件
npm install --save-dev webpack-dev-server
npm install --save-dev html-webpack-plugin
npm install --save-dev clean-webpack-plugin
package.json
{
"name": "amazing",
"version": "1.0.0",
"description": "项目搭建",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --open --hot"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
}
}
webpack.config.js
const path = require('path');
+const HtmlWebpackPlugin = require('html-webpack-plugin');
+const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
entry:{
app:'./src/main.js' //需要打包的文件
},
+ plugins: [
+ new HtmlWebpackPlugin({
+ template:'./public/index.html',//这个文件放到dist的文件一样
+ }),
+ new CleanWebpackPlugin(),
+ ],
+ devServer: {
+ contentBase: './dist',
+ port: 8080
+ },
output: {
filename: 'bundle.js',//输入的文件名是什么,dist里面生成的文件名也是什么
path: path.resolve(__dirname, 'dist')//指定生成的文件目录
}
}
npm install --save-dev style-loader css-loader
npm install --save-dev file-loader
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
entry:{
app:'./src/main.js' //需要打包的文件
},
plugins: [
new HtmlWebpackPlugin({
template:'./public/index.html',//这个文件放到dist的文件一样
}),
new CleanWebpackPlugin(),
],
devServer: {
contentBase: './dist',
port: 8080
},
+ module: {
+ rules: [
+ {
+ test: /\.css$/,
+ use: [
+ 'vue-style-loader',
+ 'style-loader',
+ 'css-loader'
+ ]
+ },
+ {
+ test: /\.(jpg|png|svg|gif)$/,
+ use: [
+ 'file-loader'
+ ]
+ },
+ {
+ test: /\.(woff|woff2|eot|ttf|otf)$/,
+ use: [
+ 'file-loader'
+ ]
+ }
+ ]
+ },
output: {
filename: 'bundle.js',//输入的文件名是什么,dist里面生成的文件名也是什么
path: path.resolve(__dirname, 'dist')//指定生成的文件目录
}
}
npm install --save-dev babel-loader
npm install --save-dev @babel/core @babel/cli @babel/preset-env
npm install --save-dev @babel/plugin-transform-arrow-functions
npm install --save-dev @babel/runtime @babel/plugin-transform-runtime
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
entry:{
app:'./src/main.js' //需要打包的文件
},
output: {
filename: 'bundle.js',//输入的文件名是什么,dist里面生成的文件名也是什么
path: path.resolve(__dirname, 'dist'),//指定生成的文件目录
},
plugins: [
new HtmlWebpackPlugin({
template:'./public/index.html',//这个文件放到dist的文件一样
}),
new CleanWebpackPlugin(),
],
devServer: {
contentBase: './dist',
port: 8080
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'style-loader',
'css-loader'
]
},
{
test: /\.(jpg|png|svg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
},
+ {
+ test: /\.js$/,
+ use: [
+ 'babel-loader'
+ ],
+ exclude: /node_modules/
+ },
]
}
}
babel.config.js
module.exports = function (api) {
api.cache(true);
const presets = [
'@babel/preset-env',
];
const plugins = [
'@babel/plugin-transform-arrow-functions',
'@babel/plugin-transform-runtime'
];
return {
presets,
plugins
};
}
npm install vue --save
npm install --save-dev vue-loader vue-template-compiler
安装sass(sass是一个CSS预处理器,还挺好用,可以根据个人爱好安装)
npm install --save-dev sass-loader
npm install --save-dev node-sass
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
+const VueLoaderPlugin = require('vue-loader/lib/plugin');
module.exports = {
entry:{
app:'./src/main.js' //需要打包的文件
},
plugins: [
new HtmlWebpackPlugin({
template:'./public/index.html',//这个文件放到dist的文件一样
}),
new CleanWebpackPlugin(),
+ new VueLoaderPlugin()
],
devServer: {
contentBase: './dist',
port: 8080
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'style-loader',
'css-loader'
]
},
{
test: /\.(jpg|png|svg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
},
{
test: /\.js$/,
use: [
'babel-loader'
],
exclude: /node_modules/
},
+ {
+ test: /\.vue$/,
+ use: [
+ 'vue-loader'
+ ],
+ },
+ {
+ test:/\.scss$/,
+ use:[
+ 'style-loader',
+ 'css-loader',
+ 'sass-loader'
+ ]
+ },
]
},
output: {
filename: 'bundle.js',//输入的文件名是什么,dist里面生成的文件名也是什么
path: path.resolve(__dirname, 'dist')//指定生成的文件目录
}
}
2、sass的使用如下:
①、引入外部样式使用
import '../css/test.scss'
②、在.vue中使用
<style lang="scss">
//sass语法样式
</style>
3、在子文件夹src下新建App.vue文件
main.js
import Vue from 'vue'
import App from './App.vue'
new Vue({
el:'#app',
render: h => h(App),
})
使用npm start启动项目