使用webpack搭建vue项目

182 阅读2分钟

基本安装

首先创建一个文件夹webpack-demo(名字自取),然后在本地安装webpack,接着安装webpack-cli

npm init -y
npm install webpack webpack-cli --save-dev

然后在主文件夹下新建一个src和public子文件夹和webpack.config.js配置文件

  • 在子文件夹public中新建index.html(此文件为入口文件模板,注意是模板)
  • 在子文件夹src内新建main.js文件(此文件为项目入口主js文件)
webpack-demo
|- /public
    |- index.html
|- /src
    |- main.js
|- package-lock.json
|- package.json
|- webpack.config.js

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>webpack-demo</title>
</head>
<body>
    <div id="app"></div>
</body>
</html>

webpack-config.js

const path = require('path');

module.exports = {
  entry: './src/main.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

安装webpack-dev-server,能够为你提供了一个简单的 web 服务器,并且能够实时重新加载等等 安装html-webpack-pluginclean-webpack-plugin插件

npm install --save-dev webpack-dev-server
npm install --save-dev html-webpack-plugin
npm install --save-dev clean-webpack-plugin

package.json

 {
    "name": "development",
    "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": {
      "html-webpack-plugin": "^2.29.0",
      "webpack": "^3.0.0",
      "webpack-cli": "^3.3.7",
      "clean-webpack-plugin": "^3.0.0",
    }
  }

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',
+     }),
+     new CleanWebpackPlugin(),
+   ],
+   devServer: {
+     contentBase: './dist',
+     port: 8000
+   },
    output: {
      filename: 'bundle.js',
      path: path.resolve(__dirname, 'dist')
    }
  };

为了从 JavaScript 模块中 import 一个 CSS 文件,你需要在 module 配置中 安装并添加 style-loader 和 css-loader 为了可以在项目中引入图片、字体文件,你需要在 module 配置中 安装并添加 file-loader

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: './src/main.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new HtmlWebpackPlugin({
            template:'./public/index.html',
        }),
        new CleanWebpackPlugin(),
    ],
    devServer: {
        contentBase: './dist',
        port: 8000
    },
+   module: {
+       rules: [
+           {
+               test: /\.css$/,
+               use: [
+                   'style-loader',
+                   'css-loader'
+               ]
+           },
+           {
+               test: /\.(jpg|png|svg|gif)$/,
+               use: [
+                   'file-loader'
+               ]
+           },
+           {
+               test: /\.(woff|woff2|eot|ttf|otf)$/,
+               use: [
+                   'file-loader'
+               ]
+           }
+       ]
+   }
}

由于好多好用的代码是ECMAScript 2015+ 版本的语法,浏览器无法识别,比如箭头函数等等,因此需要安装babel相关的模块和插件来转换为向后兼容的 JavaScript 语法(我只安装一下常用的,如果你使用其他的,需要自己安装和配置)

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

在主文件夹下面新建子文件夹babel.config.js

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
    entry: './src/main.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new HtmlWebpackPlugin({
            template:'./public/index.html',
        }),
        new CleanWebpackPlugin(),
    ],
    devServer: {
        contentBase: './dist',
        port: 8000
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    '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
    };
  }

Vue安装

安装vue相关的包

npm install vue --save
npm install --save-dev vue-loader vue-template-compiler

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: './src/main.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new HtmlWebpackPlugin({
            template:'./public/index.html',
        }),
        new CleanWebpackPlugin(),
+       new VueLoaderPlugin(),
    ],
    devServer: {
        contentBase: './dist',
        port: 8000
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    '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'
+               ],
+           },
        ]
    }
}

安装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: './src/main.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new HtmlWebpackPlugin({
            template:'./public/index.html',
        }),
        new CleanWebpackPlugin(),
        new VueLoaderPlugin(),
    ],
    devServer: {
        contentBase: './dist',
        port: 8000
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    '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'
+               ]
+           },
        ]
    }
}

sass的使用如下:

  • 引入外部样式使用
    import '../css/test.scss'
    
  • 在.vue中使用
    <style lang="scss">
        //sass语法样式
    </style>
    

在子文件夹src下新建App.vue文件

main.js

import Vue from 'vue'
import App from './App.vue'

new Vue({
    el: '#app',
    render: h => h(App),
});
webpack-demo
|- /public
    |- index.html
|- /src
    |- main.js
    |-App.vue
|- babel.config.js
|- package-lock.json
|- package.json
|- webpack.config.js

使用npm start启动项目