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
Table of contents Project Background start Create...
Table of contents npm download step (1) Import (2...
Copy code The code is as follows: <style type=...
Currently, layui officials have not provided the ...
By default, Nginx supports only one SSL certifica...
introduction You must have encountered this in an...
Preface For cost considerations, most webmasters ...
Table of contents Defining the HTML structure Inp...
This article uses examples to illustrate the usag...
MySQL is a very powerful relational database. How...
Preface Recently, I accidentally discovered MySQL...
How to solve VMware workstation virtual machine c...
Install Oracle_11g with Docker 1. Pull the oracle...
<br />Original text: http://andymao.com/andy...
1. Unzip the downloaded file as shown below . 2. ...