[英]Typescript project - how to incorporate imported modules into final build?
I have created Typescript project in Visual Studio Code. 我已经在Visual Studio Code中创建了Typescript项目。 Added some basic configuration and have used npm to download some libs.
添加了一些基本配置,并使用npm下载了一些库。
I have simple main.ts file with code of below: 我有以下代码的简单main.ts文件:
import ApexCharts from 'apexcharts'
var chart = new ApexCharts(document.querySelector("#chart"), {...});
chart.render();
And tsconfig.json: 和tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"target": "es5",
}
}
Project compiles fine but output contains require() calls. 项目可以正常编译,但是输出包含require()调用。 Is there any way to configure project build to incorporate imported libs into javascript output?
有什么方法可以配置项目构建,以将导入的库合并到javascript输出中?
I'm opened to any suggestions. 我愿意接受任何建议。
To use typescript and output a module for usage in the browser you need another compiler that knows how to do this. 要使用打字稿并在浏览器中输出要使用的模块,您需要另一个知道如何执行此操作的编译器。 By following the instructions on the TypeScript website we can use Browserify to transpile into browser code.
按照TypeScript网站上的说明,我们可以使用Browserify转换为浏览器代码。
First we install local versions of tsify
and typescript
(A global version doesn't work): 首先,我们安装的本地版本
tsify
和typescript
(A全球版本不工作):
npm install tsify typescript --save-dev
Next we write our code: 接下来,我们编写代码:
import ApexCharts from 'apexcharts'
var chart = new ApexCharts(document.querySelector("#chart"), {});
chart.render();
Then finally we add the command to our scripts in the package.json : 最后,我们将命令添加到package.json中的脚本中:
Note: tsify
will automatically read the tsconfig.json
file. 注意:
tsify
将自动读取tsconfig.json
文件。 However, some options are ignored 但是, 某些选项被忽略
{
"scripts": {
"build": "browserify src/index.ts -p [ tsify ] > bundle.js"
}
}
Lastly we build the file: 最后,我们构建文件:
npm run build
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.