backgroundThere is a local configuration file in the project: // src/image-position.js export default { label: 'Homepage', value: 'home', data: [ { label: 'Carousel', value: 'carousel' } ] } Everyone knows how to reference a local file: import ImagePosition from './image-position.js' Now you need to drop the image-position.js file onto the server and get its link: xxx.com/static/imag… At this time, it is naturally not feasible for you to directly quote the file address. import ImagePosition from 'https://xxx.com/static/image-position.js' // ERROR This dependency was not found accomplishFirst, make a small modification to image-position.js to expose a global object ImagePosition // Modified image-position.js (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.ImagePosition = factory()); }(this, (function () { 'use strict'; return { label: 'Homepage', value: 'home', data: [ { label: 'Carousel', value: 'carousel' } ] }; }))); Add externals in vue.config.js file. module.exports = { configureWebpack: config => { config.externals = { 'image-position': 'ImagePosition' } } } index.html distinguishes the environment and imports js files. // public/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="renderer" content="webkit"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> <% if (NODE_ENV == 'production') { %> <script src="http://xxx.com/static/image-position.js"></script> <% } else { %> <script src="http://test.xxx.com/static/image-position.js"></script> <% } %> </body> </html> After completing the above steps, you can happily reference the image-position.js file. import ImagePosition from 'image-position' console.log(ImagePosition) // {label: 'Home', value: 'home', data: [{label: 'Carousel', value: 'carousel'}]} Supplement how to configure under vue-cli2.0// build/webpack.base.conf.js module.exports = { externals: { // Add 'image-position': 'ImagePosition' } } // index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="renderer" content="webkit"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> <% if (process.env == 'production') { %> <script src="http://xxx.com/static/image-position.js"></script> <% } else { %> <script src="http://test.xxx.com/static/image-position.js"></script> <% } %> </body> </html> SummarizeIn the packaging volume optimization of Vue projects, CDN acceleration is a commonly used method. The above is actually the implementation content of CDN acceleration. The third-party library is introduced through the script tag, which greatly reduces the size of the packaged vendor.js file. When we want to remotely place local files on a server, the key lies in the first step of the implementation process. The rest of the content is the same as the process of configuring CDN acceleration. The above is the details of how Vue imports the js configuration file on the server. For more information about Vue import js configuration file, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: View the number of files in each subfolder of a specified folder in Linux
>>: mysql5.7.19 winx64 decompressed version installation and configuration tutorial
Table of contents 1. Commonjs exports and require...
This article shares the specific code for JavaScr...
# Adjust VMware hard disk boot priority Step 1: E...
The first line of a Docker image starts with an i...
What is an index? Why create an index? Indexes ar...
Specific method: 1. Open Command Prompt 2. Enter ...
Table of contents A. Docker deployment of springb...
Table of contents 1. What is Promise 2. Basic usa...
ps: The environment is as the title Install possi...
Table of contents 1. ChildNodes attribute travers...
environment Hostname ip address Serve Jenkins 192...
Table of contents 1. Official Documentation 2. Cr...
This article mainly introduces the dynamic SQL st...
Table of contents 1. List interface and other mod...
1. Always close HTML tags In the source code of p...