Skip to content

Commit f294505

Browse files
authored
XCOMMONS-3470: Easier importmap declaration for webjars (#4737)
1 parent 49e1b2f commit f294505

File tree

20 files changed

+984
-45
lines changed

20 files changed

+984
-45
lines changed

xwiki-platform-core/pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
org.xwiki.contrib:authservice-backport-api,
4747
org.xwiki.contrib:authservice-backport-default
4848
</xwiki.platform.oldcore.extension.features>
49+
<xwiki.enforcer.valid-importmap.skip>${xwiki.enforcer.skip}</xwiki.enforcer.valid-importmap.skip>
4950
</properties>
5051
<build>
5152
<pluginManagement>
@@ -187,6 +188,36 @@
187188
</analysisConfiguration>
188189
</configuration>
189190
</plugin>
191+
<plugin>
192+
<groupId>org.apache.maven.plugins</groupId>
193+
<artifactId>maven-enforcer-plugin</artifactId>
194+
<dependencies>
195+
<dependency>
196+
<groupId>org.xwiki.platform</groupId>
197+
<artifactId>xwiki-platform-tool-enforcer-importmap</artifactId>
198+
<version>${project.version}</version>
199+
</dependency>
200+
</dependencies>
201+
<executions>
202+
<!-- Make sure declared importmap are valid. -->
203+
<execution>
204+
<id>enforce-valid-javascript-importmap</id>
205+
<!-- Make sure the enforcer is performed after the artifacts for the current project are produced to be
206+
able to check for the validity of path the the module itself.
207+
-->
208+
<phase>verify</phase>
209+
<goals>
210+
<goal>enforce</goal>
211+
</goals>
212+
<configuration>
213+
<skip>${xwiki.enforcer.valid-importmap.skip}</skip>
214+
<rules>
215+
<javascriptImportMapCheck/>
216+
</rules>
217+
</configuration>
218+
</execution>
219+
</executions>
220+
</plugin>
190221
</plugins>
191222
</build>
192223
<modules>
@@ -229,6 +260,7 @@
229260
<module>xwiki-platform-index</module>
230261
<module>xwiki-platform-instance</module>
231262
<module>xwiki-platform-invitation</module>
263+
<module>xwiki-platform-javascript</module>
232264
<module>xwiki-platform-job</module>
233265
<module>xwiki-platform-jodatime</module>
234266
<module>xwiki-platform-lesscss</module>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
* See the NOTICE file distributed with this work for additional
4+
* information regarding copyright ownership.
5+
*
6+
* This is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU Lesser General Public License as
8+
* published by the Free Software Foundation; either version 2.1 of
9+
* the License, or (at your option) any later version.
10+
*
11+
* This software is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this software; if not, write to the Free
18+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20+
-->
21+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>org.xwiki.platform</groupId>
25+
<artifactId>xwiki-platform-core</artifactId>
26+
<version>18.0.0-SNAPSHOT</version>
27+
</parent>
28+
<artifactId>xwiki-platform-javascript</artifactId>
29+
<name>XWiki Platform - Javascript</name>
30+
<packaging>pom</packaging>
31+
<description>Modules related to the integration of Javascript concepts.</description>
32+
<modules>
33+
<module>xwiki-platform-javascript-importmap</module>
34+
</modules>
35+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
* See the NOTICE file distributed with this work for additional
4+
* information regarding copyright ownership.
5+
*
6+
* This is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU Lesser General Public License as
8+
* published by the Free Software Foundation; either version 2.1 of
9+
* the License, or (at your option) any later version.
10+
*
11+
* This software is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this software; if not, write to the Free
18+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20+
-->
21+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>org.xwiki.platform</groupId>
25+
<artifactId>xwiki-platform-javascript</artifactId>
26+
<version>18.0.0-SNAPSHOT</version>
27+
</parent>
28+
<artifactId>xwiki-platform-javascript-importmap</artifactId>
29+
<name>XWiki Platform - Javascript - Importmap</name>
30+
<packaging>jar</packaging>
31+
<description>Scan extensions for importmap definition and inject them in the page html header.</description>
32+
<properties>
33+
<xwiki.jacoco.instructionRatio>0.42</xwiki.jacoco.instructionRatio>
34+
</properties>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.xwiki.commons</groupId>
38+
<artifactId>xwiki-commons-component-api</artifactId>
39+
<version>${commons.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.xwiki.platform</groupId>
43+
<artifactId>xwiki-platform-uiextension-api</artifactId>
44+
<version>${project.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.fasterxml.jackson.core</groupId>
48+
<artifactId>jackson-databind</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.xwiki.commons</groupId>
52+
<artifactId>xwiki-commons-extension-api</artifactId>
53+
<version>${commons.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.xwiki.platform</groupId>
57+
<artifactId>xwiki-platform-webjars-api</artifactId>
58+
<version>${project.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.xwiki.commons</groupId>
62+
<artifactId>xwiki-commons-tool-test-component</artifactId>
63+
<version>${commons.version}</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.xwiki.commons</groupId>
68+
<artifactId>xwiki-commons-javascript</artifactId>
69+
<version>${commons.version}</version>
70+
</dependency>
71+
</dependencies>
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.javascript.importmap.internal;
21+
22+
import java.util.List;
23+
import java.util.Objects;
24+
25+
import jakarta.inject.Inject;
26+
import jakarta.inject.Named;
27+
import jakarta.inject.Singleton;
28+
29+
import org.xwiki.component.annotation.Component;
30+
import org.xwiki.extension.event.AbstractExtensionEvent;
31+
import org.xwiki.extension.event.ExtensionInstalledEvent;
32+
import org.xwiki.extension.event.ExtensionUninstalledEvent;
33+
import org.xwiki.extension.event.ExtensionUpgradedEvent;
34+
import org.xwiki.model.namespace.WikiNamespace;
35+
import org.xwiki.observation.EventListener;
36+
import org.xwiki.observation.event.Event;
37+
import org.xwiki.wiki.descriptor.WikiDescriptorManager;
38+
39+
/**
40+
* Listen for extensions related events (installed, uninstalled, upgraded) and clear {@link JavascriptImportmapResolver}
41+
* cache when the events are received.
42+
*
43+
* @version $Id$
44+
* @since 18.0.0RC1
45+
*/
46+
@Component
47+
@Named(JavascriptImportmapEventListener.HINT)
48+
@Singleton
49+
public class JavascriptImportmapEventListener implements EventListener
50+
{
51+
/**
52+
* Component hint.
53+
*/
54+
public static final String HINT = "JavascriptImportmapEventListener";
55+
56+
@Inject
57+
private JavascriptImportmapResolver javascriptImportmapResolver;
58+
59+
@Inject
60+
private WikiDescriptorManager wikiDescriptorManager;
61+
62+
@Override
63+
public String getName()
64+
{
65+
return HINT;
66+
}
67+
68+
@Override
69+
public List<Event> getEvents()
70+
{
71+
return List.of(new ExtensionInstalledEvent(), new ExtensionUninstalledEvent(), new ExtensionUpgradedEvent());
72+
}
73+
74+
@Override
75+
public void onEvent(Event event, Object source, Object data)
76+
{
77+
if (event instanceof AbstractExtensionEvent extensionInstalledEvent) {
78+
if (!extensionInstalledEvent.hasNamespace() || Objects.equals(extensionInstalledEvent.getNamespace(),
79+
new WikiNamespace(this.wikiDescriptorManager.getCurrentWikiId()).serialize()))
80+
{
81+
// Only clear the cache for events related to the current wiki (i.e., local to the wiki or global to
82+
// the whole farm).
83+
this.javascriptImportmapResolver.clearCache();
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)