I’m working on some Typescript project which the output (JS) will be used in a Javascript project (like a plugin). Like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="../dist/bundled.js"></script>
<script>
new TheClass()
</script>
</body>
</html>
TheClass
is defined in the Typescript file which is converted to Javascript (bundled.js) by Webpack. The target
in the tsconfig.js
is ES5
and the module
item is set to ES6
.
Now, it throws this error: Uncaught ReferenceError: TheClass is not defined.
So, I tried to add library options to the output section of webpack.config.js:
const path = require("path")
module.exports = {
mode: "development",
entry: "./src/main.ts",
devtool: "inline-source-map",
module: {
rules: [
{
test: /.tsx?$/,
include: path.resolve(__dirname, "src"),
use: "ts-loader",
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output: {
libraryTarget: 'umd', ///////////////////////////////// added this.
library: 'MyLibrary', ///////////////////////////////// added this.
filename: "bundled.js",
path: path.resolve(__dirname, "dist"),
},
}
Now I can access the class like this:
new MyLibrary.TheClass()
I also need to mention that the class is defined like this (it is exported):
export class TheClass {}
Ok, now there 3 questions:
- Is there another way for doing this?
- How can I access the class like:
TheClass()
(And notMyLibrary.TheClass()
) - How does
libraryTarget: 'umd'
help?
I also tried export default
:
export default class TheClass {}
So, I can use the class like:
new MyLibrary()
It didn’t work.
2
Answers
Found it! I changed this:
Into this:
I also used export default in my Typescript file:
Now, I can do what I wanted to:
Although, logically speaking, the name of our library has to be the same with the name of our class. So, if you change your class name, remember to change the library's name too.
If anyone knows how to prevent this conflict between the library and the class name, please comment on this answer or message me. Thanks.
Good question. Here are answers on each question based on what I know:
No, I think this is the only way to export your module code to an external library.
You can check out the type either
window
orglobal
as addressed here https://webpack.js.org/configuration/output/#type-window which is likely to work for your case:umd
is an proposed module which is designed to work for many environments such as browser and node so it should work on as module on both envs