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 Preface 1. Introduction to one-...
<br />For an article on a content page, if t...
Special symbols Named Entities Decimal encoding S...
Today I was dealing with the issue of migrating a...
1. Abnormal performance of Docker startup: 1. The...
This article example shares the specific code of ...
Table of contents 1 View the current database con...
Original source: www.bamagazine.com There are nar...
1. Record several methods of centering the box: 1...
Table of contents Problem Description Historical ...
<br />Looking at this title, you may find it...
What is a sticky footer layout? Our common web pa...
Preface Recently, when working on a high-availabi...
What is MIME TYPE? 1. First, we need to understan...
Table of contents The node version does not corre...