代码之家  ›  专栏  ›  技术社区  ›  Brian M. Hunt

Typescript注入“require(…)`而不是导入文件

  •  0
  • Brian M. Hunt  · 技术社区  · 7 年前

    我正在尝试编译一个第三方模块 pdfassembler 包括…的来源 import 当前由引用的 require

    src/pdfassember.ts

    import { PDFDocument } from 'pdfjs-dist/lib/core/document';
    

    它不是被导入而是被转换成

    require('/Users/.../pdfassembler/node_modules/pdfjs-dist/lib/core/document.js')
    

    跑步 tsc --traceModules 例如。

    ==========模块名称“pdfjs dist/lib/core/document”已成功解析为“/Users/bmh/Repos/pdfassembler/node\u modules/pdfjs dist/lib/core/document.js”========

    on module resolution 没有迹象表明为什么会发生这种情况(在编写typescriptlang.org的时候)

    我想这是一种 "import vs require"

    导入PDF.js有一个 @types/pdfjs-dist

    理想的答案是如何编译pdfassembler而不使用任何 语句,但通常了解Typescript正在做什么/期望什么以及如何解决这类问题会很有用。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Matt McCutchen    7 年前

    如果您想将多个模块合并到一个文件中,则需要使用模块绑定器,如Webpack、Rollup或Browserify。模块绑定超出了TypeScript编译器的范围(TypeScript的模块解析功能的唯一目的是支持ide中的类型检查和代码导航。)

    推荐文章