我正在尝试在一个Typescript项目中使用第三方库(具体来说是three.js)。作为一个概念验证,我试图将所有客户端代码解析为模块(而不需要转换为ES5或打包)。
我的项目设置如下:
我希望让TypeScript同时解析
我的项目设置如下:
cgi/app.js (compiled typescript file)
node_modules/@types
node_modules/three/build/three.module.js
src/app.ts
index.html
tsconfig.json
package.json
而在我的index.html
文件中
<head>
<script type="module" src="node_modules/three/build/three.module.js"></script>
<script type="module" src="cgi/app.js"></script>
</head>
我希望让TypeScript同时解析
three.module.js
文件并使用@types/three
中的类型声明。通常,您可以使用以下代码导入库:import { Scene } from 'three'
,这会为我提供类型支持,但编译后的模块没有正确的ES6语法。需要使用import { Scene } from './path/to/three.js
。
不幸的是,TypeScript目前不支持自动执行此操作。我可以直接导入ES6模块(而不使用@types),但那样就失去了类型支持。
在TypeScript编译后,是否可能将模块解析从node语法转换为ES6语法?(例如,import { Scene } from 'three'
转换为 import { Scene } from './three.js'
?
具体来说,是否可以利用Rollup来实现这一点?