Overslaan naar hoofdinhoud

Minifierplug-in 25.11

Openen in ChatGPT

De webforJ Minifierplug-in verkleint en optimaliseert CSS- en JavaScript-assets automatisch tijdens het buildproces. De plug-in ontdekt assets die worden vermeld via webforJ-assetannotaties en verkleint ze in de buildoutput. Zo worden bestanden kleiner en laden ze sneller, zonder dat de oorspronkelijke bronbestanden worden gewijzigd.

Verkleinen is ingebouwd in de bundler

De frontend-bundler verkleint CSS en JavaScript tijdens de productiebuild. Een project dat de bundler gebruikt, heeft deze plug-in dus niet nodig. De plug-in blijft beschikbaar en wordt ondersteund voor projecten die assets via assetannotaties laden zonder de bundler. Bestaande configuraties blijven werken zoals voorheen; er is geen wijziging nodig.

Instellen​

Voor de configuratie zijn twee onderdelen nodig: een annotatieprocessor om assets tijdens het compileren te ontdekken en een plug-in om ze te verkleinen.

Voeg het volgende toe aan je pom.xml:

<build>
<plugins>
<!-- Annotation Processor Configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-foundation</artifactId>
<version>${webforj.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<!-- Minifier Plugin Configuration -->
<plugin>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-maven-plugin</artifactId>
<version>${webforj.version}</version>
<executions>
<execution>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
<dependencies>
<!-- CSS minification -->
<dependency>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-phcss-css</artifactId>
<version>${webforj.version}</version>
</dependency>
<!-- JavaScript minification -->
<dependency>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-closure-js</artifactId>
<version>${webforj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

De plug-in gebruiken​

Na de configuratie werkt de plug-in automatisch. Gebruik webforJ-assetannotaties in je code:

package com.example;

import com.webforj.annotation.StyleSheet;
import com.webforj.annotation.JavaScript;

@StyleSheet("ws://css/app.css")
@JavaScript("ws://js/app.js")
public class MyApp extends App {
// Your application code
}

Wanneer je je project bouwt, voert de plug-in automatisch de volgende stappen uit:

  1. Assets ontdekken die tijdens het compileren in annotaties worden vermeld
  2. De ontdekte CSS- en JavaScript-bestanden verkleinen
  3. De bestandsverkleining en verwerkingstijd rapporteren

URL-protocol oplossen​

De plug-in herkent webforJ-URL-protocollen en vertaalt deze naar bestandssysteempaden:

ProtocolWordt vertaald naarVoorbeeld
ws://src/main/resources/static/ws://css/app.css → static/css/app.css
context://src/main/resources/context://styles/app.css → styles/app.css

URL's zonder protocol worden niet ondersteund door de minifier en worden overgeslagen.

Ingebouwde minifiers​

webforJ bevat twee productierijpe minifiers voor CSS en JavaScript.

MinifierFunctiesWordt overgeslagen
CSSVerwijdert witruimte en opmerkingen en optimaliseert eigenschapswaarden.min.css
JavaScriptVariabelen hernoemen, dode code verwijderen en syntaxis optimaliseren.min.js, .min.mjs

Configuratieopties​

De plug-in biedt opties om verkleinen uit te schakelen, JavaScript-optimalisatie aan te passen en extra bestanden te verwerken.

Verkleinen uitschakelen​

Je kunt verkleinen uitschakelen tijdens de ontwikkeling of voor foutopsporing.

Via de opdrachtregel:

mvn package -Dwebforj.minify.skip=true

Via de plug-inconfiguratie:

<plugin>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

Opties voor de JavaScript-minifier​

De JavaScript-minifier biedt verschillende configuratieopties om het optimalisatiegedrag te bepalen.

Ondersteuning voor Gradle

Vanaf v25.12 ondersteunt de Gradle-plug-in het doorgeven van opties aan de JavaScript-minifier.

OptieStandaardBeschrijving
compilationLevelSIMPLE_OPTIMIZATIONS
  • WHITESPACE_ONLY - verwijdert alleen witruimte en opmerkingen
  • SIMPLE_OPTIMIZATIONS - hernoemt variabelen en verwijdert dode code
  • ADVANCED_OPTIMIZATIONS - agressieve optimalisatie met het hernoemen van functies en eigenschappen
languageInECMASCRIPT_NEXTInvoer-JavaScript-versie: ECMASCRIPT3, ECMASCRIPT5, ECMASCRIPT_2015 tot en met ECMASCRIPT_2021, ECMASCRIPT_NEXT
languageOutECMASCRIPT5Uitvoer-JavaScript-versie: hetzelfde als languageIn, plus NO_TRANSPILE
prettyPrintfalseStel in op true om de opmaak te behouden voor foutopsporing

Configureer deze opties in het configuratiegedeelte:

<plugin>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-maven-plugin</artifactId>
<version>${webforj.version}</version>
<configuration>
<minifierConfigurations>
<closureJs>
<compilationLevel>SIMPLE_OPTIMIZATIONS</compilationLevel>
<languageIn>ECMASCRIPT_2020</languageIn>
<languageOut>ECMASCRIPT5</languageOut>
<prettyPrint>false</prettyPrint>
</closureJs>
</minifierConfigurations>
</configuration>
<executions>
<execution>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-closure-js</artifactId>
<version>${webforj.version}</version>
</dependency>
</dependencies>
</plugin>

Extra bestanden verkleinen​

Als je bestanden wilt verkleinen die niet via annotaties zijn ontdekt, maak je een configuratiebestand met globpatronen:

src/main/resources/META-INF/webforj-minify.txt
# Include patterns
**/*.css
**/*.js

# Exclusion patterns (prefix with !)
!**/*.min.css
!**/*.min.js

Aangepaste minifiers​

De plug-in ondersteunt aangepaste minifiers via Java's Service Provider Interface (SPI), zodat je ondersteuning kunt toevoegen voor extra bestandstypen of alternatieve minificatiebibliotheken.

Een aangepaste minifier maken​

Implementeer de interface AssetMinifier om je eigen minifier te maken. Het volgende voorbeeld toont een JSON-minifier die Gson gebruikt om witruimte te verwijderen:

src/main/java/com/example/minifier/JsonMinifier.java
package com.example.minifier;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.webforj.minify.common.AssetMinifier;
import com.webforj.minify.common.MinificationException;
import java.nio.file.Path;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

public class JsonMinifier implements AssetMinifier {

private static final Logger logger = Logger.getLogger(JsonMinifier.class.getName());
private final Gson gson = new GsonBuilder().create();

@Override
public String minify(String content, Path sourceFile) throws MinificationException {
try {
JsonElement element = gson.fromJson(content, JsonElement.class);
return gson.toJson(element);
} catch (JsonSyntaxException e) {
logger.warning("Malformed JSON in " + sourceFile + ", skipping: " + e.getMessage());
return content;
} catch (Exception e) {
throw new MinificationException("Failed to minify JSON file: " + sourceFile, e);
}
}

@Override
public Set<String> getSupportedExtensions() {
return Set.of("json");
}

@Override
public boolean shouldMinify(Path filePath) {
String filename = filePath.getFileName().toString();
// Skip config files and already minified files
if (filename.equals("package.json") || filename.equals("tsconfig.json")) {
return false;
}
if (filename.endsWith("-lock.json") || filename.endsWith(".min.json")) {
return false;
}
return true;
}

@Override
public void configure(Map<String, Object> options) {
// Configuration options if needed
}
}

Je minifier registreren​

Maak een configuratiebestand voor de serviceprovider:

src/main/resources/META-INF/services/com.webforj.minify.common.AssetMinifier
com.example.minifier.JsonMinifier

Je aangepaste minifier gebruiken​

Verpak je minifier als een aparte JAR en voeg deze toe als pluginafhankelijkheid:

<plugin>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>json-minifier</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Standard minifiers (optional) -->
<dependency>
<groupId>com.webforj</groupId>
<artifactId>webforj-minify-phcss-css</artifactId>
<version>${webforj.version}</version>
</dependency>
</dependencies>
</plugin>

Veelvoorkomende problemen​

[WARN] No minifiers registered via SPI. Skipping minification.
[WARN] Ensure ph-css and/or closure-compiler are on the classpath.

Voeg afhankelijkheden van minifier-modules toe aan de plug-inconfiguratie. Voeg voor CSS webforj-minify-phcss-css toe. Voeg voor JavaScript webforj-minify-closure-js toe.

Als de plug-in Processed 0 files meldt, controleer dan of:

  1. De annotatieprocessor is geconfigureerd in maven-compiler-plugin, met webforj-minify-foundation in annotationProcessorPaths
  2. Er webforJ-assetannotaties in je broncode staan
  3. target/classes/META-INF/webforj-resources.json na het compileren bestaat
[WARN] File not found: /path/to/static/css/app.css (referenced as 'ws://css/app.css')

Controleer of het bestand zich op het juiste pad onder src/main/resources/static bevindt en of het URL-protocol overeenkomt met de mapstructuur.

[WARN] Error minifying file /path/to/app.css: parse error at line 42

De plug-in geeft een waarschuwing, maar gaat verder zonder de build te laten mislukken. Als het verkleinen mislukt, blijft de oorspronkelijke inhoud behouden. Controleer CSS of JavaScript met een linter om syntaxisfouten op te lossen.


Blijf leren