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.
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:
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>
import B from './moduleB';
$(document.body).append('<div>AAA</div>');
B();
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 ?
@danielo515
See this: https://github.com/kedzior-io/parcel-global-var-typescript-vue-test (ignore vue part)
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! :)