Menu

How to use Shared Library Javascript with React

When we generate some NPM modules from generator-liferay-js , you should see
that the generated (.jar) module is a "bit heavy". In the case of the React
Liferay module it is not as much as the Angular Liferay module (posted
here
). However, let's say we want to work with a few modules
of this type and instance them on one page. This could cause some problems:

1. Loss of performance: JS files will be downloaded for each module
(those of the dependency) which implies a longer loading time.

2. Possible version conflicts: If you use different versions of React,
there might be conflicts.

  1. Longer build time: By having to package the module with all its dependencies, the build time is considerably high.

So, we ask ourselves, how can we solve these problems? One possible solution
is to use "Shared Bundle", that is, have a common javascript library in a
container as a provider and consume it from the angular modules. To learn more
keep reading the blog where we explain how to do it step by step.

Requirements : Node, Yeoman and NPM Installed

1. First, install generator-liferay-js by running this command (if not
preinstalled):

npm install -g generator-liferay-js


2. Run the generator inside your “liferay-workspace/modules” or any
project folder you are using

yo liferay-js


3. Choose the “Shared bundle” type and fill the rest of the information
needed:

![](https://liferay.dev/documents/portlet_file_entry/14/how-to-use-common-
library-react-liferay.png/c38046f3-edf5-bef1-811a-ec60be4a275a?imagePreview=1)
4. Open the “src/index.js” and add inside init() method

console.log('common-js-bundle is working as provider...');


5. Now create a React project, running the generator inside your
“liferay-workspace/modules” or any project folder you are using

yo liferay-js


6. Choose the “React Widget” type and fill in the rest of the information
necessary:

![](https://liferay.dev/documents/portlet_file_entry/14/how-to-use-common-
library-react-
liferay-6.png/58099905-27b3-4605-5b32-186b39e3776d?imagePreview=1)

7. Open the package.json and copy everything inside “dependencies”.

![](https://liferay.dev/documents/portlet_file_entry/14/how-to-use-common-
library-react-
liferay-7.png/b864453c-5f23-fdd8-8481-a285d5142894?imagePreview=1)

8. Paste as “dependencies” in your package.json file of your “Shared
Library project” previously created.

![](https://liferay.dev/documents/portlet_file_entry/14/how-to-use-common-
library-react-
liferay-8.png/d471f569-7303-8cbe-a747-e3c6b324bf34?imagePreview=1)

9. Inside the “Shared JS library” project, run:

npm install


10. Back to React Project, open the .npmbundlerrc file (here is the
setting)

  **a.**  [Excluding all dependencies](https://github.com/liferay/liferay-frontend-projects/blob/master/maintenance/projects/js-toolkit/docs/.npmbundlerrc-file-reference.md#exclude) declared inside our project, add the following

"exclude":{ "*":true },


  **b.** Importing all dependencies you need to pull from theShared JS library”. We can do throughconfig/imports+ the name of the provider. It should look like this:

"config":{ "imports":{ "common-js-bundle":{ "react": "16.8.6", "react-dom": "16.8.6" } }, }


**c.** Finally if you want to access the index.js from your provider, simply add the provider without namespace  inside theimports

"":{ "common-js-bundle" : "^1.0.0" }


  • The final code of .npmbundlerrc file should look like this:

![](https://liferay.dev/documents/portlet_file_entry/14/how-to-use-common-
library-react-
liferay-10.png/b7447e8f-8263-c803-c452-9b39884a819e?imagePreview=1)

11. Open the src/index.js and import your provider

**a.** This import, loads the main file \(index.js\) from provider

![](https://lh5.googleusercontent.com/JHXS3e672klSKZ0yghEAtFwn5zaPqgIOXYDe1aeF-
Xwq9JUeVvduwhgzrSuFAwsiVd_fQZN3wtkNG4y6fRiS154IxKsuSek8I4UrgOqPK2TQX8Q0vl40YvFKYDaTRppG88J1KzWW)

12. Back to your React Project, eliminate everything inside the build
folder.

13. Now we will deploy both projects: the consumer and the provider.To do
this, run inside each project:

npm run deploy


Note : If you are using the Hybrid React Widget (React plus Java
project) you can apply the same number 10 step to add the dependency and
exclude the others, and the number 11 step if you want to access the
index.js from the provider. The only difference is the “create-jar”
properties isn’t inside the .npmbundlerrc file because it is not needed for
hybrid widget.


Here my Git repo with some samples:
https://github.com/RoselaineMarquesMontero/liferay-workspace-shared- library/tree/main/modules

link

Posted by SourceForge Robot 2021-04-15

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.