25
loading...
This website collects cookies to deliver better user experience
messages.js
:
function sendMessage(message) {
console.log(message);
}
hello-world.js
:
function helloWorld() {
// this line assumes sendMessage was injected before & is available on the global scope
sendMessage("hello world!");
}
export
. So in our example we will have:// src/messages.js
export function sendMessage(message) {
console.log(message);
}
// src/hello-world.js
export function helloWorld() {
// this line assumes sendMessage was injected before & is available on the global scope
sendMessage("hello world!");
}
./src/
. Inside the files, the only difference is adding export
before the function declaration.// webpack.config.js
module.exports = {
entry: {
messages: "./src/messages",
"hello-world": "./src/hello-world",
},
output: {
library: {
type: "global",
},
filename: "[name].js",
},
};
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Webpack sdk</title>
<link rel="shortcut icon" href="#" />
<script src="./dist/messages.js"></script>
<script src="./dist/hello-world.js"></script>
</head>
<body>
<button onclick="helloWorld()">
hello world
</button>
</body>
</html>