32
loading...
This website collects cookies to deliver better user experience
package.json
, besides library info we will have following fields:{
...
"main": "src/index.js",
"type": "module",
"scripts": {
....
"build": "webpack"
},
...
}
esmodules
, that's why there is "type": "module"
. The "main": "src/index.js"
informs clients' webpack what files use for imports - effectively ignoring anything build locally by us. This is as intended, because it's client's build that knows what parts of our library to leave, and what drops.src/index.js
is an entry point to our library:export { helloWorld } from "./hello-world.js";
src/hello-world.js
contains a simple demo method:export function helloWorld() {
console.log("hello world!");
}
webpack.config.js
that allow us to achieve that:export default {
output: {
library: {
type: "umd",
name: "sdk",
},
filename: "sdk.js",
},
};
output.library.type: "umd"
sets the export type to universal module definition - modules that are able to work on matter the environment, most importantly in our case - as drop-in js files. output.library.name
sets the content of our module to be published on window.sdk
- the same as you find jquery methods on $
.