How to split javascript code between maven modules

My maven project has the following structure:

  • "base" module - contains shared java files - should contain shared javascript files
  • module 1 - use shared java files as a dependency on maven - should i use shared javascript files?
  • module 2 - use shared java files as a dependency on maven - should I use shared javascript files using?

Currently webpack seems to be a new thing in javascript packaging, and npm seems to be the right package manager. So I tried the following: - the base modules create the npm package (with the npm package) using webpack - modules 1 and 2 install this package manually using the relative path to the target folder of the base module, where the npm package

Why haven't I used npm publish? - It is not possible to update published npm packages, so each assembly will have to create a new version number - To create

Internet connection required.

Other options? - I was thinking about using the maven resource plugin, but it seems to be a lot of manual work (file names, folders, etc.).

So I ask: are you sharing javascript code between maven modules within the same project? How do you achieve this? Should there be a better way to do this, or?

If you want to see my project, look here: https://github.com/stefanrinderle/softvis3d

Thanks in advance for your comments comments!

+5
source share
2 answers

Basically, to redistribute npm modules separately, you should use npm publish . However, during dev, npm has a nice npm link function that links your local folder as a global dependency and forces npm to use it instead of downloading from the repository. So, I just added npm link to your project and changed the node installation directory, so all submodules use the same instance of node. This way you can easily create your own modules and publish them when you are ready.

base / pom.xml:

 <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> ... <configuration> ... <installDirectory>../node</installDirectory> </configuration> <executions> <execution> <id>npm link</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>link</arguments> </configuration> </execution> </executions> </plugin> 

.. module /pom.xml

 <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <configuration> <installDirectory>../node</installDirectory> </configuration> <executions> <execution> <!-- link before install --> <id>npm link softvis3d-viewer</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>link softvis3d-viewer</arguments> </configuration> </execution> <execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> </execution> ... </executions> </plugin> 

.. module /package.json

  "devDependencies": { ... "softvis3d-viewer": "0.0.4-SNAPSHOT" }, 

See my commit for more details.

+4
source

NPM works if the Maven module supplies JavaScript that will undergo transpilation / minimization / concatenation. If sharing pre-compiled modules is required, I successfully use web-fragment.xml (defined in Servlet 3.0 specifications) and set my JavaScript, etc. under META-INF / resources. Depending on your front end technology, you will need to use some approach related to code splitting or web components.

In fact, the client side will have to download and execute its library code before starting everything that depends on it.

If you have a small (-ish) library with a specific API that you want to share between your Maven modules, and all this is part of the same ecosystem, npm publish and npm link is the way to go. For larger blocks of functionality, especially those that have specific usage profiles (for example, the user profile editor or configuration page - are not used every time), they can be dynamically loaded).

While there is nothing new in this, this area (POM, JARs, NPM and JS / ES6 modules) seems to be in search of the best approach, and it will take some time to plunge into a couple of reliable technicians. I don’t know clean, absolutely no means of support, a set of rules and settings to cope with this.

0
source

Source: https://habr.com/ru/post/1240225/


All Articles