0

I'm trying to compile a third party module pdfassembler to include the source of imports that are currently referenced by require statements in the compiled result.

The src/pdfassember.ts file has e.g.

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

which instead of being imported is converted into

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

... instead of the source code.

Running tsc --traceModules gives e.g.

======== Module name 'pdfjs-dist/lib/core/document' was successfully resolved to '/Users/bmh/Repos/pdfassembler/node_modules/pdfjs-dist/lib/core/document.js'. ========

It's not clear how to have the imports included. The Typescript documentation on module resolution offers no indication of why this might be happening. (And at the time of writing typescriptlang.org is down)

I expect this is a species of the problem of "import vs require", but the answer there offers did not point to any apparent solutions.

The import PDF.js has an @types/pdfjs-dist, and it occurred to me that this may be usable, but it's not clear how.

An ideal answer would indicate how to compile pdfassembler without any require statements, but it'd be useful to know generally what Typescript is doing/expecting and how to work around this sort of problem.

1 Answer 1

2

If you want to combine multiple modules into one file, you'll need to use a module bundler such as Webpack, Rollup, or Browserify. Module bundling is beyond the scope of the TypeScript compiler. (The only purpose of TypeScript's module resolution functionality is to support type checking and code navigation in IDEs.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.