Packaging, launching and optimizing the Vue project
Packaging of Vue project
Open the terminal and switch to the project root directory Enter the command: A dist folder will be generated in the root directory of the current project, which contains the packaged files Project hosting
express create server var express = require('express') const path = require('path') // 2. Create the server var app = express(); // Hosting static resources // You can also place all static resources in a specified directory, such as public, and then add the following configuration app.use(express.static('dist')) app.use('/', express.static(path.join(__dirname, 'dist'))) // 3. Start the server and listen to the port app.listen(3001, () => { console.log('http://127.0.0.1:3001') }) Start the server
Common optimization of projects
Generate project report files npm run build – --report Open the report page 1. In the report page, the larger the block, the larger the volume occupied by the template. CDN acceleration optimization
vue.config.js
Add package exclusions module.exports = { configureWebpack: { externals:{ 'vue': 'Vue', 'element-ui': 'ELEMENT', 'quill': 'Quill' } }, } As you can see, the size of the packaged project is significantly reduced, but the problem is not solved. Without these packages, the packaged project cannot be run. This is because there is no Vue package in the packaged project, so an error occurs. We now need to use CDN to provide these resources. Add user customization of CDN Add the following code to vue.config.js let cdn = { css: [ //element-ui css 'https://unpkg.com/element-ui/lib/theme-chalk/index.css', // Style sheet // Rich text box plug-in style 'https://cdn.bootcdn.net/ajax/libs/quill/2.0.0-dev.4/quill.bubble.css' ], js: [ // vue must be at first! 'https://unpkg.com/vue/dist/vue.js', // vuejs // element-ui js 'https://unpkg.com/element-ui/lib/index.js', // elementUI // Rich text box plug-in 'https://cdn.bootcdn.net/ajax/libs/quill/2.0.0-dev.4/quill.js' ] } Automatically add resources to the page through plugins Mount resources to plugins module.exports = { // Add packaging exclusions to indicate that the packages in the following configuration will not be packaged into the project in the future configureWebpack: { externals:{ 'vue': 'Vue', 'element-ui': 'ELEMENT', 'quill': 'Quill' } }, //Mount the CDN resources to the plugin chainWebpack (config) { config.plugin('html').tap(args => { args[0].cdn = cdn return args }) } } Use the plug-in to add the specified CDN resource in the page, and add the following code to the public index in the project (the index file before the project is packaged) Add css import (in head structure) <% for(var css of htmlWebpackPlugin.options.cdn.css) { %> <link rel="stylesheet" href="<%=css%>" /> <% } %> Add js import (in body structure) <% for(var js of htmlWebpackPlugin.options.cdn.js) { %> <script src="<%=js%>"></script> <% } %> Repack, OK Set to use CDN only in production stage
const isProd = process.env.NODE_ENV === 'production' // Is it a production environment? let externals = { 'vue': 'Vue', 'element-ui': 'ELEMENT', 'quill': 'Quill' } let cdn = { css: [ //element-ui css 'https://unpkg.com/element-ui/lib/theme-chalk/index.css', // Style sheet // Rich text box plug-in style 'https://cdn.bootcdn.net/ajax/libs/quill/2.0.0-dev.4/quill.bubble.css' ], js: [ // vue must be at first! 'https://unpkg.com/vue/dist/vue.js', // vuejs // element-ui js 'https://unpkg.com/element-ui/lib/index.js', // elementUI // Rich text box plug-in 'https://cdn.bootcdn.net/ajax/libs/quill/2.0.0-dev.4/quill.js' ] } cdn = isProd ? cdn : { css: [], js: [] } externals = isProd ? externals : {} module.exports = { // Add packaging exclusions to indicate that the packages in the following configuration will not be packaged into the project in the future configureWebpack: { externals }, // chainWebpack (config) { config.plugin('html').tap(args => { args[0].cdn = cdn return args }) } } This concludes this article on the implementation steps of vue project packaging and optimization. For more relevant vue project packaging and optimization content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Tutorial on installing and using virtualenv in Deepin
>>: Solving problems encountered when importing and exporting Mysql
Copy code The code is as follows: html { overflow...
Superset is a lightweight self-service BI framewo...
This article is welcome to be shared and aggregat...
1. Float: The main purpose is to achieve the effe...
What is the purpose of this sentence? Copy code Th...
nginx (engine x) is a high-performance HTTP and r...
1. Floating layout 1. Let the fixed width div flo...
Declare the parameter name, type and permission y...
Data Types and Operations Data Table 1.1 MySQL ty...
1. Select Edit → Virtual Network Editor in the me...
Click here to return to the 123WORDPRESS.COM HTML ...
Table of contents 1. Deconstruction Tips 2. Digit...
Without further ado, I will post the code for you...
Enctype : Specifies the type of encoding the brows...
AWS - Amazon's cloud computing service platfo...