前幾天接到了用vue重構某個業務模組的需求,常規開發不做討論,但是在打包時需要排除業務中使用的某個第三方依賴包。 程式碼如下:
//main.js
import Vue from 'vue';
import Cesium from 'cesium/Cesium'; //必須全域性引入,某個元件中引入忽略無效
import Widgets from 'cesium/Widgets/widgets.css'
Vue.prototype.Cesium = Cesium;
複製程式碼
//vue.config.js(如果沒有就在根目錄下新建)
const cesiumSource = "node_modules/cesium/Source";
const cesiumWorkers = '../Build/Cesium/Workers';
module.exports = {
chainWebpack: config => {
//定義別名
.resolve.alias.set('cesium', path.resolve(__dirname, cesiumSource)).end().end()
},
configureWebpack: config => {
let settings = {};
if (process.env.NODE_ENV === 'production') { // 使用環境變數,區分生產和開發模式
settings = {
plugins: [
new webpack.DefinePlugin({
'CESIUM_BASE_URL': JSON.stringify('static')
}),
new CopyWebpackPlugin([{
from: path.join(cesiumSource, cesiumWorkers),
to: 'static/Workers'
}]),
new CopyWebpackPlugin([{
from: path.join(cesiumSource, 'Assets'),
to: 'static/Assets'
}]),
new CopyWebpackPlugin([{
from: path.join(cesiumSource, 'Widgets'),
to: 'static/Widgets'
}])
],
externals: {
/**
*key: main.js中全域性引入的路徑
*value: 全域性暴露出來的物件名
*/
"cesium/Cesium": "Cesium" //忽略js
"cesium/Widgets/widgets.css": "Widgets" //忽略css
}
}
} else {
settings = {
plugins: [
new webpack.DefinePlugin({
'CESIUM_BASE_URL': JSON.stringify('')
}),
new CopyWebpackPlugin([{
from: path.join(cesiumSource, cesiumWorkers),
to: 'Workers'
}]),
new CopyWebpackPlugin([{
from: path.join(cesiumSource, 'Assets'),
to: 'Assets'
}]),
new CopyWebpackPlugin([{
from: path.join(cesiumSource, 'Widgets'),
to: 'Widgets'
}]),
]
}
}
return settings
}
複製程式碼
that's all