From 83941d2e837f68a5a0202d49924f59758ed099e4 Mon Sep 17 00:00:00 2001 From: Andy Damevin Date: Mon, 27 May 2024 10:52:01 +0200 Subject: [PATCH] Introduce Bundling envs --- .../bundler/deployment/WebBundlerConfig.java | 21 +++++++++++++++++++ .../PrepareForBundlingProcessor.java | 3 +++ .../src/test/resources/application.properties | 1 + deployment/src/test/resources/web/app/app.js | 2 +- .../pages/includes/quarkus-web-bundler.adoc | 17 +++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/common-deployment/src/main/java/io/quarkiverse/web/bundler/deployment/WebBundlerConfig.java b/common-deployment/src/main/java/io/quarkiverse/web/bundler/deployment/WebBundlerConfig.java index 9e55d67..ad18b13 100644 --- a/common-deployment/src/main/java/io/quarkiverse/web/bundler/deployment/WebBundlerConfig.java +++ b/common-deployment/src/main/java/io/quarkiverse/web/bundler/deployment/WebBundlerConfig.java @@ -6,6 +6,7 @@ import java.nio.charset.Charset; import java.util.*; +import java.util.stream.Collectors; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; @@ -134,6 +135,25 @@ interface BundlingConfig { @WithDefault("linked") String sourceMap(); + /** + * List of environments for the bundle + */ + Map envs(); + + default Map safeEnvs() { + if (envs().isEmpty()) { + return Map.of(); + } + return envs() + .entrySet() + .stream() + .collect(Collectors.toMap(e -> safe(e.getKey()), e -> "'" + safe(e.getValue()) + "'")); + } + + static String safe(String v) { + return v.replaceAll("^[^a-zA-Z_$]|[^0-9a-zA-Z_$]", "_"); + } + default boolean sourceMapEnabled() { return "linked".equalsIgnoreCase(sourceMap()) || "true".equalsIgnoreCase(sourceMap()) @@ -152,6 +172,7 @@ static boolean isEqual(BundlingConfig c1, BundlingConfig c2) { return Objects.equals(c1.splitting(), c2.splitting()) && LoadersConfig.isEqual(c1.loaders(), c2.loaders()) && Objects.equals(c1.external(), c2.external()) + && Objects.equals(c1.envs(), c2.envs()) && Objects.equals(c1.sourceMap(), c2.sourceMap()); } diff --git a/deployment/src/main/java/io/quarkiverse/web/bundler/deployment/PrepareForBundlingProcessor.java b/deployment/src/main/java/io/quarkiverse/web/bundler/deployment/PrepareForBundlingProcessor.java index 3e9a0ca..bf6a5bb 100644 --- a/deployment/src/main/java/io/quarkiverse/web/bundler/deployment/PrepareForBundlingProcessor.java +++ b/deployment/src/main/java/io/quarkiverse/web/bundler/deployment/PrepareForBundlingProcessor.java @@ -160,6 +160,9 @@ ReadyForBundlingBuildItem prepareForBundling(WebBundlerConfig config, + "'") .fixedEntryNames(); } + if (!config.bundling().envs().isEmpty()) { + esBuildConfigBuilder.define(config.bundling().safeEnvs()); + } if (config.bundling().external().isPresent()) { for (String e : config.bundling().external().get()) { esBuildConfigBuilder.addExternal(e); diff --git a/deployment/src/test/resources/application.properties b/deployment/src/test/resources/application.properties index 5d1e6a9..f3a223d 100644 --- a/deployment/src/test/resources/application.properties +++ b/deployment/src/test/resources/application.properties @@ -1,5 +1,6 @@ quarkus.web-bundler.dependencies.compile-only=false quarkus.web-bundler.bundle-redirect=true +quarkus.web-bundler.bundling.envs.HELLO_ENV=world quarkus.log.category."io.quarkiverse.web.bundler".level=DEBUG quarkus.log.category."io.mvnpm.esbuild".level=DEBUG quarkus.http.root-path=/foo/bar \ No newline at end of file diff --git a/deployment/src/test/resources/web/app/app.js b/deployment/src/test/resources/web/app/app.js index a092ea7..5b08554 100644 --- a/deployment/src/test/resources/web/app/app.js +++ b/deployment/src/test/resources/web/app/app.js @@ -1,5 +1,5 @@ import $ from 'jquery'; $(document).ready(function() { - console.log("hello world"); + console.log("hello " + HELLO_ENV); }); diff --git a/docs/modules/ROOT/pages/includes/quarkus-web-bundler.adoc b/docs/modules/ROOT/pages/includes/quarkus-web-bundler.adoc index 7fc99c4..1574d3e 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-web-bundler.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-web-bundler.adoc @@ -538,4 +538,21 @@ endif::add-copy-button-to-env-var[] --|boolean |`false` + +a|icon:lock[title=Fixed at build time] [[quarkus-web-bundler_quarkus-web-bundler-bundling-envs-envs]]`link:#quarkus-web-bundler_quarkus-web-bundler-bundling-envs-envs[quarkus.web-bundler.bundling.envs]` + + +[.description] +-- +List of environments for the bundle + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++QUARKUS_WEB_BUNDLER_BUNDLING_ENVS+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++QUARKUS_WEB_BUNDLER_BUNDLING_ENVS+++` +endif::add-copy-button-to-env-var[] +--|`Map` +| + |=== \ No newline at end of file