首页 > 其他分享 >TS+Cesium配置

TS+Cesium配置

时间:2023-01-09 23:45:55浏览次数:39  
标签:node resolve plugin 配置 TS loader webpack Cesium path

记录一下 TS + Cesium 配置过程

首先 npm 安装 cesium 和 webpack 一众包,如下:

{
  // package.json   
  "dependencies": {
    "@babel/preset-env": "^7.20.2",
    "@open-wc/webpack-import-meta-loader": "^0.4.7",
    "babel-loader": "^9.1.2",
    "cesium": "^1.101.0",
    "clean-webpack-plugin": "^4.0.0",
    "copy-webpack-plugin": "^11.0.0",
    "css-loader": "^6.7.3",
    "html-webpack-plugin": "^5.5.0",
    "node-polyfill-webpack-plugin": "^2.0.1",
    "style-loader": "^3.3.1",
    "ts-loader": "^9.4.2",
    "webpack-cli": "^5.0.1",
    "webpack-dev-server": "^4.11.1"
  }
}

tsconfig

{
    "compilerOptions": {
      "module": "es2015",
      "target": "es2015",
      "strict": true,   // 严格模式
      "noEmitOnError": true,  // 错误禁止打包
      "moduleResolution": "node"  // 配置了才能找node_modules
    }
}

webpack.config.js

webpack 配置文件根据自己的需求添加loader即可,但 copy-webpack-plugin 非常重要,因为 cesium 库的结构有点怪,直接引入有点问题

注意,因为我的 node_modules 在根目录外层,并非根目录,所以在 path.resolve 中添加了 "../",常规来讲不需要这个,若出现了 unable to locate 'XXX' glob 这个错误,大概率就是 from 里的目录错了

const path = require("path")
const webpack = require("webpack")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")
// 重新编译自动清空build文件夹
const {CleanWebpackPlugin} = require("clean-webpack-plugin")

const cesiumSource = "./node_modules/cesium/Build/Cesium"
const cesiumWorkers = "Workers"

module.exports = {
    entry: "./index.ts",
    output: {
        path: path.resolve(__dirname, "build"),
        filename: "bundle.js",
        environment:{
            // 是否允许webpack使用箭头函数(为了兼容IE)
            arrowFunction: true,
        }
    },
    module:{
        rules:[
            {
                test:/\.ts$/,
                use: [
                    {
                        // 当配置项复杂时可用{}
                        // 指定加载器
                        loader:"babel-loader",
                        // 设置预定义的环境
                        options:{
                            // 设置预定义的环境
                            presets:[
                                [
                                    // 指定环境插件
                                    "@babel/preset-env",
                                    // 配置信息
                                    {
                                        // 要兼容的目标浏览器
                                        targets:{
                                            "chrome":"88",
                                            "ie":"11"
                                        },
                                        // 指定corejs版本
                                        "corejs":"3",
                                        // 使用corejs的方式为"usage",表示按需加载
                                        "useBuiltIns": "usage"
                                    }
                                ]
                            ]
                        }
                    },
                    "ts-loader"
                ],
                // 排除node-modules中的ts
                exclude:/node-modules/
            },
            {
                test: /\.js$/,
                use: {
                    loader: '@open-wc/webpack-import-meta-loader',
                },
            },
            {
                test:/\.css$/,
                use:[
                    "style-loader",
                    "css-loader"
                ]
            }
        ]
    },
    plugins:[
        new HtmlWebpackPlugin({
            template: "./index.html"
        }),
        new CleanWebpackPlugin(),
        new CopyWebpackPlugin({
            patterns:[
                {
                    from: path.resolve("../", cesiumSource, cesiumWorkers),
                    to: "Workers"
                },
                {
                    from: path.resolve("../", cesiumSource, "Assets"),
                    to: "Assets"
                },
                {
                    from: path.resolve("../", cesiumSource, "Widgets"),
                    to: "Widgets"
                },
                {
                    from: path.resolve("../", cesiumSource, "ThirdParty/Workers"),
                    to: "ThirdParty/Workers",
                }
            ]
        }),
        new webpack.DefinePlugin({
            CESIUM_BASE_URL: JSON.stringify("./"),
        })
    ],
    // 用来设置引用模块
    resolve:{
        // ts,js文件都可以设置为引用模块
        extensions:[".ts", ".js"],
    },

}

接下来还可能有个错误,Module not found: Error: Can't resolve 'zlib' in 'XXX' 

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.

This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:

      - add a fallback 'resolve.fallback: { "zlib": require.resolve("browserify-zlib") }' - install 'browserify-zlib'

If you don't want to include a polyfill, you can use an empty module like this:

      resolve.fallback: { "zlib": false }

错误大概是上述的样子,报错原因是由于在 webpack5 中移除了 nodejs 核心模块的 polyfill 自动引入,所以需要手动引入:

所以只需,安装:

npm install node-polyfill-webpack-plugin -d

配置:

...
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
...

module.exports = {
    ...
    plugins:[
        ...
        // 引入 polyfill
        new NodePolyfillPlugin()    
        ...
    ],
    ...
}

此时 index.ts 中引入 cesium 就不会报错了

import * as Cesium from "cesium"
import "cesium/Build/Cesium/Widgets/widgets.css"

// 初始化地球
let viewer = new Cesium.Viewer('MapContainer', {
    ...
});
(viewer.cesiumWidget.creditContainer as HTMLElement).style.display = "none";

标签:node,resolve,plugin,配置,TS,loader,webpack,Cesium,path
From: https://www.cnblogs.com/xt112233/p/17038893.html

相关文章

  • openSUSE 安装与配置
    初创建于:2022-05-1711:29最近,有点想转向openSUSE了,究其原因,大概是因为毕竟arch是一个没有有实体组织支持的发行版,实际上稳定性确实是堪忧.这不仅是系统的......
  • XFCE 配置
    初创建于:2022-03-2716:20最近迁到了xfce4在尝试了kde,gnome,i3之后,感觉还是xfce比较好当然i3依旧在用,但是部分应用窗口在i3下的显示很奇怪,毕竟i3......
  • SpringBoot笔记--自动配置(高级内容)(上集)
    原理分析自动配置Condition--增加的条件判断功能来一个案例说明:具体实现:没有要求的话,就是这样的:Config.javaUser.javaSpringLearnApplication.java结果:加上......
  • Win10基础 AIDA64 查看电脑的配置报告
          OS:Windows10Version22H2      blog:师万物 typesetting:Markdown AIDA64查看电脑的配置报告喜欢研究电脑硬件的同学,可以使用相关......
  • VS2013+Qt5.9.0配置过程
    https://www.likecs.com/show-204435170.html#sc=4494 VS2013+Qt5.9.0配置过程准备工作下载VS2013与Qt5.9.0,下载vsaddin插件配置步骤要想在VS中使用Qt做界......
  • nginx + keepalived 高可用配置
    ip 192.168.153.14主 192.168.153.15备 1、安装nginxyum-yinstallgccpcre-develzlib-developensslopenssl-devel#安装nginxwgethttps://n......
  • linux基础:1、linux简介、虚拟化软件的安装与配置、Xshell的安装与配置
    Linux目录Linux一、linux简介二、linux发展史三、虚拟化技术1、简介2、虚拟化软件下载3、重要名词解释4、远程链接工具一、linux简介常见岗位1、自动化运维2、容器......
  • ABB 800XA学习笔记3:基本配置
    下面的内容我在新浪博客也发表过,地址是ABB80XA学习笔记03:基本配置_来自金沙江的小鱼_新浪博客(sina.com.cn)在这里记录一遍,以免丢失。这边笔记生成的时候,我刚刚开始学习......
  • Basic Concepts of STM32 (1)
           ......
  • 学习笔记——Mybatis核心配置文件概述及根标签
    2023-01-09一、Mybatis核心配置文件概述及根标签1、核心配置文件的概述(即“mybatis-config.xml”)MyBatis的配置文件包含了会深深影响MyBatis行为的设置和属性信息。2、......