webpack manually build Vue project

webpack manually build Vue project


Initialize the project

Create a new directory, and then initialize the project

npm init
复制代码

The following were added in dependence of package.json

// dependencies是指在使用npm i -save [modules] 的时候安装的,执行 npm install --production 的时候会安装
"dependencies": {
    "vue": "^2.6.10"
  },
// devDependencies是指在使用npm i -save-dev [modules] 的时候安装的,执行 npm install --production 的时候不会安装
"devDependencies": {
// 安装babel相关的
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6",
"babel-polyfill": "^6.26.0",
// 插件:每次build的时候清空旧文件
"clean-webpack-plugin": "^3.0.0",
// css处理
"css-loader": "^2.1.1",
// 处理CSS兼容
"postcss-loader": "^3.0.0",
"autoprefixer": "^9.6.0",
// 添加sass支持
"node-sass": "^4.12.0",
"sass-loader": "^7.1.0",
// 插件
"html-webpack-plugin": "^3.2.0",
"style-loader": "^0.23.1",
// 添加webpack打包
"webpack": "^4.33.0",
"webpack-cli": "^3.3.3",
// 提供web容器,可在本地访问http://localhost:port
"webpack-dev-server": "^3.7.1",
// 提供配置文件的合并
"webpack-merge": "^4.2.1",
// 添加vue-loader
"vue-loader": "^15.7.0",
"vue-template-compiler": "^2.6.10",
}
复制代码

Install more dependent

npm install
复制代码

Configuration bable

Create a new file in the root directory .babelrc

{
    "presets": [
        "@babel/preset-env",
    ]
}
复制代码

Increase in the root directory of a postcss.config.js

module.exports = {  
    plugins: {  
      'autoprefixer': {browsers: 'last 5 version'}  
    }  
  } 
复制代码

In the root directory as a basis for a new webpack.base.config.js configuration webpack

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/index.js',
    output: {
        filename: 'bundle.[hash].js',
        path: path.join(__dirname, '/dist')
    },
    module: {
    	// 配置相应的规则
        rules: [
            {
                test: /\.(sa|sc|c)ss$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'postcss-loader',
                    'sass-loader',
                ],
            }, {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
        ]
    },
    // 配置相应的插件
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        new CleanWebpackPlugin()
    ]
};

复制代码

New webpack.dev.config.js in the root directory of the file as a configuration development environment

const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.config.js');

module.exports = merge(baseConfig, {
	// 设置为开发模式
    mode: 'development',
    devtool: 'inline-source-map',
    // 配置服务端目录和端口
    devServer: {
        contentBase: './dist',
        port: 3000
    }
});

复制代码

New webpack.product.config.js in the root directory of the file as the configuration of the production environment

const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.config.js');

module.exports = merge(baseConfig, {
	// 设置为生产模式
    mode: 'production'
});

复制代码

Configuration script command

  "scripts": {
    "start": "webpack-dev-server --open --config webpack.dev.config.js",
    "build": "webpack --config webpack.product.config.js"
  },
复制代码

Src directory in the root directory of the new, and 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 & Vue</title>
</head>
<body>
    <div id="root"></div>
</body>
</html>

复制代码

New entry in the file src index.js and App.vue

import Vue from 'vue';
import App from './App.vue';
import './index.scss';

new Vue({
  el: '#root',
  render: h => h(App),
});

复制代码
<template>
  <div id="app">
    <h1 class="hello">hello webpack & Vue</h1>
  </div>
</template>

<script>
export default {
  name: "app"
};
</script>

<style scoped>
</style>

复制代码

New index.scss files, test files in the src directory scss

$blog-color-red: #ff0000;

.hello {
    color: $blog-color-red;
}

复制代码

Run the program

npm run start
复制代码

Not surprisingly, then, it will jump after the execution of the browser interface, and display red ** Hello Vue & Webpack! ** words

Packed file

npm run build
复制代码

Not surprisingly, then generates a file in the root directory dist, you can open the file in a folder between, display red ** Hello Vue & Webpack! ** words

Epilogue

Before the frame is generally used scaffolding comes tool, taking advantage of the Dragon Boat Festival have time, take the initiative to configure the look webpack & Vue, of course, more than just some simple configuration, more complex configurations also need to modify webpack configuration, there is time to add. Also a configuration & React WebPACK .

GitHub project Address: github.com/kavience/we...

(Finish)

Reproduced in: https: //juejin.im/post/5cfb8b82f265da1baf7cde26

Guess you like

Origin blog.csdn.net/weixin_34101784/article/details/91447204