-
Notifications
You must be signed in to change notification settings - Fork 12.8k
What is the workflow to distribute a library ? #4545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In a lot of cases this depends on your target environment and there are a lot of considerations outside of just compiling to TypeScript. There are several open issues about this as there is not clear and definitive approach. I think the TypeScript team recognise that it has its limits current and there is conversation going on here about proposing to simplify this: #4433 - Proposal: Bundling TS module type definitions (which links to a lot of the other tickets/solutions). |
use namespace (aka internal modules) if you are not using modules/module loaders. |
And in the case of internal module, why do I get library.d.ts is not a module ? The file is full of declare module library {}. Just adding export = library at the end of the file fixes it, but how can I automate it ? What's the correct way of having a usable d.ts out of the box ? So basically I should drop the module library{} statements and generate one javascript file with its associated d.ts file ? Then the consumer would need to do something like : import {A} from 'library/dist/A'; import {B} from 'library/dist/B'; right ? So it's not the same module anymore. How do people working on big projects with TS do to organize shared libraries ? It seems almost easier for me to distribute TS files at the moment. |
you can either distribute your library as an external module, i.e. your users need a loader to consume it, e.g. require, commonjs, system... or as a standalone script file. i think both are fairly common practices in the js world. TypeScript compiler is distributed as a file, (e.g. include typescriptservices.js into your html page to get compiler support), and we do use namespaces all over the place. |
Hi everyone,
I spent two days trying to figure out this module-internal-external-namespace-import-export thing, and I have to say, it's counter intuitive, and not very easy to use.
So, let's pretend that we have a little module that should be consumed by different projects. The module is composed of two classes A and B in the two files A.ts and B.ts. We want to bundle it in a file called library.js along with a type definitions in library.d.ts. I tried two main things so far :
Then I would use the --out option, which should be called --outFile but that my compiler as of version 1.5.3 doesn't know of. I end up with a .js file, which doesn't contain a module.exports = library. Which makes it fail to load in node. And the library.d.ts generated doesn't contain export = library, which leads to the compile error TS2306 library.d.ts is not a module. Of course, I played around, tried to create a file library.ts that would import or export things trying to compile it and hope it would generate what was needed.
I also tried to play with external modules, but this can't be the way to go as it seems impossible to generate a single file from it.
So, is there anywhere, a guide or manual that clearly explains how to bundle code ? A sort of step by step process. Looking at references online it seems a lot of people are after that, and a lot of people struggle with it. It can't be that hard can it ? People get confused with internal modules, external modules, namespaces, are internal modules actually gone ?
So what is "the way" of packaging a library that can be consumed by a browser and node ?
The text was updated successfully, but these errors were encountered: