I wrote a simple typescript library, which I use as a npm library. It looks like that:
index.ts
import * as peselManager from './pesel';
/**
* Returns information if PESEL number is valid.
*
* @param {string} pesel
* @returns {boolean}
*/
export const isValidPesel = (pesel: string): boolean => {
return peselManager.isValid(pesel);
};
Everything is fine, but i would like to use my library as js script too. For this I used webpack, with fallowing configuration:
var path = require('path');
var UglifyJSPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
entry: "./lib/index.ts",
output: {
filename: "peseljs.min.js",
path: path.resolve(__dirname, 'dist')
},
resolve: {
extensions: [".ts", ".js"]
},
module: {
loaders: [{ test: /\.ts$/, loader: "ts-loader" }]
},
plugins: [
new UglifyJSPlugin()
]
};
When I use webpack command I got minify js script. But when I add this script to example page like that:
<html lang="en">
<head>
<meta charset="utf-8">
<title>PeselJS Example</title>
<meta name="description" content="PeselJS Example Page">
<meta name="author" content="jaroslawkrol">
<script type="text/javascript" src="peseljs.min.js"></script>
</head>
<body>
<span id="some-span">ABC</span>
<script>
var isValid = isValidPesel("22032101355");
if(isValid) {
console.log("valid");
} else {
console.log("invalid");
}
</script>
</body>
</html>
I got error about Uncaught ReferenceError: isValidPesel is not defined. And my question: Is there a way to bundle my library to call a function like this? Is it a good practise? Maybe, I did it totally wrong, so I will be happy to hear your comments.