Parcel: how to introduce a module as globals variable to all other sub modules

Created on 18 Jan 2019  路  9Comments  路  Source: parcel-bundler/parcel

i am using parcel to bundle my js, before with webpack it provides a plugin using that we can introduce some variable which will be available in all other module as a global.
Example: Like i need jquery for other modules and for that i have to import jquery in all of them instead if there is a way the jquery variable would be available as a global to all my modules.

Question

All 9 comments

Like i need jquery for other modules

By module, do you mean bundle or an actual module?
If you have only one bundle, then you need to import jQuery in every module
```js
const jQuery = require("jquery");
````

i meant module but for further clarification please open and see the link below
https://stackoverflow.com/questions/37656592/define-global-variable-with-webpack

See the --global command line option: https://parceljs.org/cli.html#expose-modules-as-umd

@danmarshall how do i import jquery and pass that as UMD.

@Mayank694 If you use the global $ of JQuery for example, you might just have:

index.html

Loading Jquery from CDN and injecting a global variable $

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
</head>
<body>
    <script src="moduleA.js"></script>
</body>
</html>

moduleA.js

import B from './moduleB';
$(document.body).append('<div>AAA</div>');
B();

moduleB.js

export default function B() {
    $(document.body).append('<div>BBB</div>');
}

Both moduleA and moduleB just use the global variable named $. Or whatever global variable you are trying to use.

thanks @danmarshall for the help

That do not works for me. My global variable is named global. How can I access it ?

Loading Jquery from CDN and injecting a global variable $

Is it possible to load jQuery not from CDN? I need to use it from local file ./assets/js/jquery.min.js
Thanks in advance! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

medhatdawoud picture medhatdawoud  路  3Comments

davidnagli picture davidnagli  路  3Comments

dsky1990 picture dsky1990  路  3Comments

devongovett picture devongovett  路  3Comments

will-stone picture will-stone  路  3Comments