I'm developing a Vanilla JS library. This library will have to expose an object, let's call it sdk: this object will contain all the library methods, so a potential user can do
<script src="thelibrary.js"></script>
<script>
sdk.myFunction();
</script>
This is my webpack configuration:
const path = require('path');
module.exports = {
entry:
['babel-polyfill', './src/index.js'],
output: {
path: path.join(__dirname, './dist'),
filename: 'my-sdk.js',
library: 'my-sdk-function',
libraryTarget: 'umd',
publicPath: '/dist/',
umdNamedDefine: true,
},
mode: 'production',
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env', 'react', 'es2015', 'stage-0'],
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.less$/,
use: ['style-loader', 'css-loader', {
loader: 'less-loader',
options: {
javascriptEnabled: true,
},
}],
},
],
},
};
This is my index.js
const myFunction = () => {
console.log('hello');
}
const sdk = { myFunction };
How can I achieve my objective?
I have an index.html file that I'm serving via a python SimpleHTTPServer:
<body>
<script src="http://localhost:5000/dist/my-sdk.js"></script>
</body>
But I'm not able to access the sdk object... what is my mistake?
Thanks a lot!