简体   繁体   English

Typescript项目-如何将导入的模块合并到最终版本中?

[英]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): 首先,我们安装的本地版本tsifytypescript (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.

相关问题 Node.js 和 Typescript,如何动态访问导入的模块 - Node.js and Typescript, how to dynamically access imported modules 如何将带有导入模块的 typescript 转换为浏览器友好的 js 文件 - How to transpile typescript with imported modules to a browser friendly js-file 如何使用导入数组构建 Typescript 字符串文字类型? - How can I build a Typescript string literal type with an imported array? TypeScript中的自定义内部版本和模块引用 - Customized build and modules references in TypeScript 如何使用 TypeScript 编译器 API 对使用 `require()` 导入的模块进行类型检查? - How to use the TypeScript Compiler API to type-check modules imported using `require()`? 在Javascript Webpack项目中过渡到Typescript(如何导出模块) - Transitioning to Typescript in a Javascript Webpack project (how to export modules) 如何用导入的模块加载Javascript? - How to load Javascript with imported modules? 如何在导出中包含导入的模块? - How to include imported modules in an export? Rollup + Typescript:最终输出中的 lib/index.js 连接模块 - Rollup + Typescript: lib/index.js concatenated modules in final output 仅在结果不是对象/数组的情况下,才能对导入的模块进行打字稿单元测试 - Typescript Unit Tests on imported modules only works if result is not object/array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM