io.quarkiverse.web.bundler.deployment.QuteTemplateWebAssetsProcessor Maven / Gradle / Ivy
package io.quarkiverse.web.bundler.deployment;
import static io.quarkiverse.web.bundler.deployment.StaticWebAssetsProcessor.makeWebAssetPublic;
import static io.quarkiverse.web.bundler.deployment.util.PathUtils.prefixWithSlash;
import static java.util.concurrent.CompletableFuture.completedFuture;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.eclipse.microprofile.config.ConfigProvider;
import io.quarkiverse.web.bundler.deployment.items.GeneratedBundleBuildItem;
import io.quarkiverse.web.bundler.deployment.items.QuteTemplatesBuildItem;
import io.quarkiverse.web.bundler.deployment.items.WebAsset;
import io.quarkiverse.web.bundler.deployment.web.GeneratedWebResourceBuildItem;
import io.quarkiverse.web.bundler.deployment.web.GeneratedWebResourceBuildItem.SourceType;
import io.quarkiverse.web.bundler.runtime.Bundle;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.qute.*;
public class QuteTemplateWebAssetsProcessor {
@BuildStep
void processHtmlTemplateWebAssets(WebBundlerConfig config,
QuteTemplatesBuildItem htmlTemplates,
GeneratedBundleBuildItem generatedBundle,
BuildProducer staticResourceProducer,
LiveReloadBuildItem liveReload,
LaunchModeBuildItem launchMode) {
final Map bundle = generatedBundle != null ? generatedBundle.getBundle() : Map.of();
final Bundle.Mapping mapping = new Bundle.Mapping() {
@Override
public String get(String name) {
return bundle.get(name);
}
@Override
public Set names() {
return bundle.keySet();
}
};
final Engine engine = Engine.builder()
.addDefaults()
.addNamespaceResolver(NamespaceResolver.builder("inject")
.resolve((c) -> c.getName().equals("bundle") ? new Bundle(mapping) : null)
.build())
.addNamespaceResolver(NamespaceResolver.builder("build")
.resolve((c) -> c.getName().equals("launchMode") ? launchMode.getLaunchMode().toString() : null)
.build())
.addNamespaceResolver(NamespaceResolver.builder("config")
.resolveAsync(this::resolveConfig)
.build())
.addLocator(new WebBundlerTagsLocator())
.addSectionHelper(new UserTagSectionHelper.Factory("bundle", "web-bundler/bundle.html"))
.addValueResolver(new ReflectionValueResolver())
.addParserHook(new Qute.IndexedArgumentsParserHook())
.addResultMapper(new HtmlEscaper(ImmutableList.of("text/html", "text/xml")))
.build();
for (WebAsset webAsset : htmlTemplates.getWebAssets()) {
final byte[] bytes = webAsset.resource().contentOrReadFromFile();
final String content = engine.parse(new String(bytes, webAsset.charset())).render();
makeWebAssetPublic(staticResourceProducer, prefixWithSlash(webAsset.pathFromWebRoot(config.webRoot())),
HtmlPageWebAsset.of(webAsset, content), SourceType.BUILD_TIME_TEMPLATE);
}
}
private CompletionStage
© 2015 - 2025 Weber Informatics LLC | Privacy Policy