背景
項目中有一個本地配置文件:
1
2
3
4
5
6
7
8
9
10
11
|
// src/image-position.js export default { label: '首頁' , value: 'home' , data: [ { label: '輪播' , value: 'carousel' } ] } |
如何引用一個本地文件大家都知道:
1
|
import ImagePosition from './image-position.js' |
現在需要把image-position.js文件丟到服務器上去,得到它的鏈接:
xxx.com/static/imag…
這個時候你直接引用文件地址自然是行不通的。
1
2
3
|
import ImagePosition from 'https://xxx.com/static/image-position.js' // ERROR This dependency was not found |
實現
首先對image-position.js做一點小改造,暴露一個全局對象ImagePosition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
// 改造后的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: '首頁' , value: 'home' , data: [ { label: '輪播' , value: 'carousel' } ] }; }))); |
在vue.config.js文件里添加externals。
1
2
3
4
5
6
7
|
module.exports = { configureWebpack: config => { config.externals = { 'image-position' : 'ImagePosition' } } } |
index.html 區分環境并引入js文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// 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 > |
結束上面的步驟后就可以愉快的引用image-position.js文件了。
1
2
3
|
import ImagePosition from 'image-position' console.log(ImagePosition) // {label: '首頁',value: 'home',data: [{label: '輪播', value: 'carousel'}]} |
補充vue-cli2.0下如何配置
1
2
3
4
5
6
7
|
// build/webpack.base.conf.js module.exports = { externals: { // 新增 'image-position' : 'ImagePosition' } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// 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 > |
總結
在Vue項目的打包體積優化中,cdn加速是常用的一種手段,上面其實就是cdn加速的實現內容,把第三方庫通過script標簽引入,大大減少打包的vendor.js文件大小。
當我們想把本地文件放到服務器遠程化時,關鍵在于實現步驟的第一步,其他的內容跟配置cdn加速的過程是一樣的。
以上就是Vue 如何import服務器上的js配置文件的詳細內容,更多關于Vue import js配置文件的資料請關注服務器之家其它相關文章!
原文鏈接:https://juejin.cn/post/6948312676413997093