Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export function in webpack bundle

I am using the google Maps API which requires a callback. How do I export a callback from a webpack bundle to use by an external script such as Google Maps API?

HTML (X-d out key):

<div id="hello"></div>
<script src="/js/map.bundle.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXX&callback=initMap"></script>

map.js:

var $ = require("jquery");
function initMap() {
    $("#hello").text("hello world");
}

I build the above map.js file into a webpack bundle called map.bundle.js.

Browser console error:

Yc message : "initMap is not a function" name : "InvalidValueError" stack : "Error↵ at new Yc (https://maps.googleapis.com/ma...

I also tried adding

module.exports = { initMap: initMap }

to map.js but that didn't help.

EDIT: Same question, but for using javascript functions from webpack bundles in form events:

HTML:

<form onsubmit="sayHello(event)">
    <button type="submit">Say Hello</button>
</form>

JS:

function sayHello(e) {
    e.preventDefault();
    console.log("hello");
    return false;
}

When the JS is packaged into a bundle, the "hello" is no longer printed on the console

like image 395
Daniel Kats Avatar asked Feb 13 '17 20:02

Daniel Kats


People also ask

What is export in package json?

The exports field in the package. json of a package allows to declare which module should be used when using module requests like import "package" or import "package/sub/path" . It replaces the default implementation that returns main field resp.

How do I export multiple functions?

To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.

What is output in webpack config?

Configuring the output configuration options tells webpack how to write the compiled files to disk. Note that, while there can be multiple entry points, only one output configuration is specified.

What is the output file of webpack?

Output is a property that tells webpack where to emit / save the bundles it creates and how to name these bundled files. By default the main output file gets stored in ./dist/main. js and other generated files are stored in ./dist . module. exports = { output: { path: path.


1 Answers

Your webpack file (map.bundle.js) does not generally write any of your functions or variables into the global (in this case, window) namespace.

This causes problems for libraries that need to be on the global namespace like jQuery or the google maps api.

One way to get around this is to add initMap to the window object

var $ = require("jquery");
function initMap() {
    $("#hello").text("hello world");
}
window.initMap = initMap;
like image 168
redconservatory Avatar answered Sep 24 '22 08:09

redconservatory