I'm trying to create a simple TypeScript library and test it from within different TypeScript example projects, so that I can verify and show that the library is usable from TypeScript under different module formats, including AMD.
For the library, I'm compiling it to UMD code, outputting it together with declaration files into a "build" directory, which I later refer to from the package.json
file. Like so:
tsconfig.json
{
"compilerOptions": {
"module": "umd",
"outDir": "build",
"declaration": true,
...
},
...
}
package.json
{
"main": "build/index.js",
"types": "build/index.d.ts",
...
}
It's worth mentioning that the library is a compound of multiple files, and that index.js
is simply gluing together the different parts of it, exposing them through a single entry point.
Now, from one of the example projects, my goal is to compile to AMD code, and use RequireJS to understand the outcome. I'm able to write the TypeScript code, the compiler is happy with all the imports, and the editor (VS Code) provides auto-completion properly. However, the compiled code does not include the code of the library mentioned above, which is one of its dependencies. And when running the code in a browser, RequireJS is not able to load the library. Here is how this project is configured:
tsconfig.json
{
"compilerOptions": {
"module": "amd",
"outFile": "build/app.js"
},
...
}
index.html
...
<script src="node_modules/requirejs/require.js"></script>
<script src="build/app.js"></script>
<script>
require(['main'])
</script>
...
With this, RequireJS is trying to load the library from "/library-name.js", which of course does not exist.
I'm pretty clueless about how to proceed.