git clone https://github.com/wthrajat/npm-in-webjar.git && cd npm-in-webjar && mvn clean install
The directory src/main/resources
is now an NPM project - like the one you probaby have worked on usually.
- Edit
src/main/resources/package.json
for the deps update cd src/main/resources
and runnpm install
- This will update
src/main/resources/package-lock.json
NOTE: Without this step, the updated version is not taken into account during the maven build.
export default function helloWorld(name : string) {
console.log("Hello " + name + "!");
}
export default helloWorld;
require.config({
paths: {
'output': '$services.webjars.url('domain.org.name:npm-in-webjar', 'output.js')'
}
});
require(['output'], function (output) {
console.log('Your output bundle has been loaded successfully!');
name = "Taylor Swift";
bundle.helloWorld(name);
// Prints: Hello Taylor Swift!
});
- At first, maven copy
src/main/resources
totarget/webjar
- Then
npm ci
is run insidetarget/webjar
installing the dependencies by followingpackage-lock.json
- Then
npm run build
is run insidetarget/webjar
, producing the artifacts oftarget/webjar/dist
- At last, the content of
target/webjar/dist
is copied to the relevant directory to be included in the WebJar packaging.
This project is inspired from Manuel Leducs's webpack demo.