一直使用cesium,但是都是使用script直接引入的,但是在将其放置在增加路由的子页面中中时会出现一个问题,刷新后提示cesium is undefined

看直接引入cesium.js

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<link rel="stylesheet" href="libs/Cesium/Widgets/widgets.css">
<link rel="stylesheet" href="//at.alicdn.com/t/font_1668594_l9pybqe35u.css">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="screen"></div>
<!-- built files will be auto injected -->
<script type="text/javascript" src="libs/Cesium/Cesium.js"></script>
</body>
</html>


刷新后提示错误


。。。。。。。。

后来决定根据cesium官网来改进配置

一、安装ceium

  npm install --save-dev cesium

二、配置vue.config.js

1、在vue.config.js增加cesium目录映射

// The path to the CesiumJS source code
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = '../Build/Cesium/Workers';

2、配置别名

    chainWebpack: config => {
config.resolve.alias
.set("cesium", resolve(cesiumSource))
},

3、使用WebpackPlugin拷贝cesium资源到dist

    configureWebpack: config => {
const plugins = [];
plugins.push(
new CopyWebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' } ])
);
plugins.push(
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets' } ])
);
plugins.push(
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' } ])
);
plugins.push(
new webpack.DefinePlugin({ CESIUM_BASE_URL: JSON.stringify('./') })
); config.plugins = [...config.plugins, ...plugins];
},

4、main.js引用

将Home.vue修改用于测试

最后vue.config.js配置是这样的

const path = require("path");
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin')
// The path to the CesiumJS source code
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = '../Build/Cesium/Workers';
function resolve(dir) {
return path.join(__dirname, dir)
} module.exports = { publicPath: process.env.NODE_ENV === 'production'
? './'
: '/',
devServer: {
port: 8099,
disableHostCheck: true,
},
chainWebpack: config => {
config.resolve.alias
.set("cesium", resolve(cesiumSource))
},
configureWebpack: config => {
const plugins = [];
plugins.push(
new CopyWebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' } ])
);
plugins.push(
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets' } ])
);
plugins.push(
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' } ])
);
plugins.push(
new webpack.DefinePlugin({ CESIUM_BASE_URL: JSON.stringify('./') })
); config.plugins = [...config.plugins, ...plugins];
},
}

5、 运行测试


npm run serve

提示了几个错误

 WARNING  Compiled with 3 warnings                                                                              09:14:40

 warning  in ./src/main.js

"export 'default' (imported as 'Cesium') was not found in 'cesium/Cesium'

 warning  in ./node_modules/cesium/Source/Core/buildModuleUrl.js

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

 warning  in ./node_modules/cesium/Source/Core/buildModuleUrl.js

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

看第一个错误相信熟悉过node 或者commonJS都知道其实,Cesium.js没有 export default默认导出

三、错误处理

那么就将

import  Cesium from 'cesium/Cesium'

修改为

//  const Cesium  = require('cesium/Cesium') ; // 也可以改成require引入
import * as Cesium from 'cesium/Cesium'

好了目前只有两个错误了

看地图也加载出来了



但是还是有两个【错误】…

看到有朋友说要增加unknownContextCritical配置,如:

于是我试着这样去修改配置

    chainWebpack: config => {
config.module
.unknownContextCritical(false)
.end()
config.resolve.alias
.set("cesium", resolve(cesiumSource))
},

但是运行是报错的


ERROR TypeError: config.module.unknownContextCritical is not a function

看了半天vue 配置文档,最后将回调函数换成对象就OK了

这里就不啰嗦了,直接看最终的vue.config.js配置吧

const path = require("path");
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin')
// The path to the CesiumJS source code
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = '../Build/Cesium/Workers';
function resolve(dir) {
return path.join(__dirname, dir)
} module.exports = { publicPath: process.env.NODE_ENV === 'production'
? './'
: '/',
devServer: {
port: 8099,
disableHostCheck: true,
},
configureWebpack: {
output: {
sourcePrefix: ' '
},
amd: {
toUrlUndefined: true
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve('src'),
'cesium': path.resolve(__dirname, cesiumSource)
}
},
plugins: [
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'}]),
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers'}]),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify('./')
})
],
module: {
unknownContextCritical: /^.\/.*$/,
unknownContextCritical: false }
}
}

四、deal with ‘JavaScript heap out of memory’ in vue build


> tf-pipe-gallery@0.1.0 build D:\YLKJPro\tf-pipe-gallery
> vue-cli-service build \ Building for production...
<--- Last few GCs ---> [10780:000001C5544A9790] 232452 ms: Mark-sweep 1383.4 (1420.6) -> 1383.0 (1420.6) MB, 1387.6 / 0.0 ms (average mu = 0.105, current mu = 0.017) allocation failure scavenge might not succeed
[10780:000001C5544A9790] 233690 ms: Mark-sweep 1383.7 (1420.6) -> 1383.3 (1421.1) MB, 1234.4 / 0.0 ms (average mu = 0.057, current mu = 0.003) allocation failure scavenge might not succeed <--- JS stacktrace ---> ==== JS stack trace ========================================= 0: ExitFrame [pc: 0000026F403D0461]
Security context: 0x01f699a9d971 <JSObject>
1: /* anonymous */(aka /* anonymous */) [000002EFA8A9A541] [D:\YLKJPro\tf-pipe-gallery\node_modules\webpack-sources\lib\applySourceMap.js:~58] [pc=0000026F4126674D](this=0x03d26f9825b1 <undefined>,0x02080a98c961 <String[6]: Array(>,0x01b0a61506b9 <Object map = 000000E391A70FC1>)
2: SourceNode_walk [0000036225A356F9] [D:\YLKJPro\tf-pipe-gallery\node... FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory Writing Node.js report to file: report.20200402.161224.10780.001.json
Node.js report completed
1: 00007FF6B7CCCC3A public: __cdecl v8::internal::GCIdleTimeHandler::GCIdleTimeHandler(void) __ptr64+4618
2: 00007FF6B7C798B6 uv_loop_fork+80934
3: 00007FF6B7C7A411 uv_loop_fork+83841
4: 00007FF6B8070F1E void __cdecl v8::internal::FatalProcessOutOfMemory(class v8::internal::Isolate * __ptr64,char const * __ptr64)+798
5: 00007FF6B8070E57 void __cdecl v8::internal::FatalProcessOutOfMemory(class v8::internal::Isolate * __ptr64,char const * __ptr64)+599
6: 00007FF6B8120E74 public: static bool __cdecl v8::internal::Heap::RootIsImmortalImmovable(int)+14900
7: 00007FF6B8116994 public: bool __cdecl v8::internal::Heap::CollectGarbage(enum v8::internal::AllocationSpace,enum v8::internal::GarbageCollectionReason,enum v8::GCCallbackFlags) __ptr64+7556
8: 00007FF6B8115068 public: bool __cdecl v8::internal::Heap::CollectGarbage(enum v8::internal::AllocationSpace,enum v8::internal::GarbageCollectionReason,enum v8::GCCallbackFlags) __ptr64+1112
9: 00007FF6B811EAB7 public: static bool __cdecl v8::internal::Heap::RootIsImmortalImmovable(int)+5751
10: 00007FF6B811EB36 public: static bool __cdecl v8::internal::Heap::RootIsImmortalImmovable(int)+5878
11: 00007FF6B82A78E1 public: class v8::internal::Handle<class v8::internal::HeapObject> __cdecl v8::internal::Factory::NewFillerObject(int,bool,enum v8::internal::AllocationSpace) __ptr64+49
12: 00007FF6B8360CEA public: static int __cdecl v8::internal::StoreBuffer::StoreBufferOverflow(class v8::internal::Isolate * __ptr64)+27082
13: 0000026F403D0461

看到这一片错误的确叫人头疼。。。

最后在vue 官网Issues终于找到了解决方法

直接修改package.json中的script

    "build": "node --max_old_space_size=4096 node_modules/@vue/cli-service/bin/vue-cli-service.js build --open"

再次npm run build 看见buil 成功了

ok到此总算完美解决了所有问题。

感谢阅读,希望对于刚入门的cesium 开发者一些帮助,谢谢

vue cli3 整合Cesium,处理build 时内存溢出问题的更多相关文章

  1. vue/cli3引入cesium

    vue/cli3引入cesium 一开始用了webpack结合vue引入vue:结果是各种bug,搞了半天.最后问了基友,发现vue脚手架这个·简单高效的方法,只需要几行代码就轻松地搞定啦! 方案一. ...

  2. 图片_ _Android有效解决加载大图片时内存溢出的问题 2

    Android有效解决加载大图片时内存溢出的问题 博客分类: Android Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或 setImageResource或 Bit ...

  3. Android开发中如何解决加载大图片时内存溢出的问题

    Android开发中如何解决加载大图片时内存溢出的问题    在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...

  4. 关于node的前端项目编译时内存溢出问题

    最近在做一个基于vue 的多页面项目  , 页面n++多,编译时发生node内存溢出问题,继而百度之,得到解答,故记录之. '如图' 只需在 package.json 里面   加上    --max ...

  5. nodejs 前端项目编译时内存溢出问题的原因及解决方案

    现象描述 昨天用webpack打包Vue的项目时,node内存溢出而停止build项目,即是项目构建过程中频繁报内存溢出:FATAL ERROR: CALL_AND_RETRY_LAST Alloca ...

  6. vue2打包时内存溢出解决方案

    vue项目完成时,若项目过大,就会出现内存溢出的问题,导致vue打包不成功 错误截图 解决方案 在依赖package.json中修改build为 "build":"nod ...

  7. 基于node的前端项目编译时内存溢出问题

    解决方法: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory JavaScript堆内存不足,这里说的 Jav ...

  8. vue 中的router 配置问题 导致的内存溢出~~~

    最近的项目用到 vue, 各种踩坑中. 其中一个就是router映射表写的稍有不慎,就会出现内存溢出的问题, 而且也不会具体告诉你哪里出错,所以很是头疼~~~ 出错多了,发现了一些router的一些规 ...

  9. hive中与hbase外部表join时内存溢出(hive处理mapjoin的优化器机制)

    与hbase外部表(wizad_mdm_main)进行join出现问题: CREATE TABLE wizad_mdm_dev_lmj_edition_result as select *  from ...

  10. Tomcat启动项目时内存溢出问题如何解决

    在Eclipse中,内存溢出(报不能创建JAVA虚拟机错时,也可能是这里配错了.) 1.双击Tomcat,点击Open launch configuration,Arguments, 2.在VM ar ...

随机推荐

  1. 一文带你了解 JS Module 的始末

    写在前面 模块化开发是我们日常工作潜移默化中用到的基本技能,发展至今非常地简洁方便,但开发者们(指我自己)却很少能清晰透彻地说出它的发展背景, 发展过程以及各个规范之间的区别.故笔者决定一探乾坤,深入 ...

  2. EF Code 如何应对高并发

    1.高并发的情况,时常会发生数据不稳定的情况 在看本节内容之前,请先看上一章SqlServer 高并发的情况下,如何利用锁保证数据的稳定性 本节内容,也是具体讨论如何在EF中实现这些操作 2.场景模拟 ...

  3. 你需要知道的 14 个常用的 JavaScript 函数

    1.确定任意对象的具体类型 众所周知,JavaScript 中有六种原始数据类型(Boolean.Number.String.Null.Undefined.Symbol)和一个对象数据类型.但是你知道 ...

  4. Kingpin Private Browser - 隐私保护浏览器,隐身模式、广告拦截做你的私人浏览器

    Kingpin Private Browser 是一个功能齐全的浏览器,隐身模式和广告拦截总是启用.它不会记住历史记录.密码或cookie.默认情况下,浏览器使用谷歌搜索(您可以在设置中将其更改为Du ...

  5. Win11右键菜单改回传统样式

    Win11右键菜单,比较不人性化,隐藏了一些常用选项,需要点"更多选项"才能显示,多次一举. 解决方法,一句话: reg.exe add "HKCU\Software\C ...

  6. 基于el-input实现数字区间输入框(已发布npm/github)

    项目地址:https://github.com/heyu3913/InputNumberRange  (求star) input-number-range tips:更多定制化需求请联系: 13102 ...

  7. 从0开始学杂项 第三期:隐写分析(2) PNG图片隐写

    Misc 学习(三) - 隐写分析:PNG 图片隐写 在上一期,我主要讲了讲自己对于隐写分析.信息搜集和直接附加的一些浅薄理解,这一期我们继续对隐写分析的学习,开始讲隐写分析最喜欢考的一项--图片隐写 ...

  8. yaml-cpp YAML格式处理库的介绍和使用(面向业务编程-文件格式处理)

    yaml-cpp YAML格式处理库的介绍和使用(面向业务编程-文件格式处理) YAML格式介绍 YAML的格式介绍,有关ini.json和xml或许很多人已经很了解了,但是关于YAML,还有许多人不 ...

  9. python入门教程之五数据结构

    变量 Python 变量类型 变量存储在内存中的值,这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同 ...

  10. sip消息拆包原理及组包流程

    操作系统 :CentOS 7.6_x64      freeswitch版本 :1.10.9 sofia-sip版本: sofia-sip-1.13.14   freeswitch使用sip协议进行通 ...