Skip to content

Commit

Permalink
feat: add parameter to force prod build (#16757)
Browse files Browse the repository at this point in the history
* feat: add parameter to force prod build

Add a parameter that forces a
production bundle build even
when an applicable bundle exists.

Closes #16752

* Clean needs bundle file at end of tests

* dev bundle mojo is also a build mojo

* format

* fix task so it always runs check

write result file.
Use force production bundle in
test-prod-bundle module

* build instead of bundle

* remove extra space

* Trim string to not have [ ] or [] on different runs
  • Loading branch information
caalador committed May 10, 2023
1 parent 57cb4b8 commit f1a072f
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 9 deletions.
Expand Up @@ -210,6 +210,11 @@ public boolean ciBuild() {
return false; // ci build not applicable for dev mode
}

@Override
public boolean forceProductionBuild() {
return false; // not applicable for dev bundle generation.
}

/**
* Generates a List of ClasspathElements (Run and CompileTime) from a
* MavenProject.
Expand Down
Expand Up @@ -185,4 +185,6 @@ internal class GradlePluginAdapter(val project: Project, private val isBeforePro
override fun ciBuild(): Boolean = extension.ciBuild

override fun skipDevBundleBuild(): Boolean = extension.skipDevBundleBuild

override fun forceProductionBuild(): Boolean = extension.forceProductionBuild
}
Expand Up @@ -228,6 +228,16 @@ public open class VaadinFlowPluginExtension(project: Project) {
*/
public var skipDevBundleBuild: Boolean = false


/**
* Setting this to `true` will force a build of the production build
* even if there is a default production bundle that could be used.
*
* Created production bundle optimization is defined by
* [.optimizeBundle] parameter.
*/
public var forceProductionBuild: Boolean = false

public fun filterClasspath(@DelegatesTo(value = ClasspathFilter::class, strategy = Closure.DELEGATE_FIRST) block: Closure<*>? = null): ClasspathFilter {
if (block != null) {
block.delegate = classpathFilter
Expand Down
Expand Up @@ -108,6 +108,16 @@ public class BuildFrontendMojo extends FlowModeAbstractMojo
@Parameter(property = InitParameters.CI_BUILD, defaultValue = "false")
private boolean ciBuild;

/**
* Setting this to {@code true} will force a build of the production build
* even if there is a default production bundle that could be used.
*
* Created production bundle optimization is defined by
* {@link #optimizeBundle} parameter.
*/
@Parameter(property = InitParameters.FORCE_PRODUCTION_BUILD, defaultValue = "false")
private boolean forceProductionBuild;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
long start = System.nanoTime();
Expand Down Expand Up @@ -172,4 +182,9 @@ public boolean ciBuild() {
return ciBuild;
}

@Override
public boolean forceProductionBuild() {
return forceProductionBuild;
}

}
Expand Up @@ -179,6 +179,8 @@ public void setup() throws Exception {
ReflectionUtils.setVariableValueInObject(mojo, "generateBundle", false);
ReflectionUtils.setVariableValueInObject(mojo, "runNpmInstall", false);
ReflectionUtils.setVariableValueInObject(mojo, "optimizeBundle", true);
ReflectionUtils.setVariableValueInObject(mojo, "forceProductionBuild",
false);

ReflectionUtils.setVariableValueInObject(mojo, "openApiJsonFile",
openApiJsonFile);
Expand Down
Expand Up @@ -321,7 +321,8 @@ public static void runNodeUpdater(PluginAdapterBuild adapter)
.setNodeAutoUpdate(adapter.nodeAutoUpdate())
.setJavaResourceFolder(adapter.javaResourceFolder())
.withPostinstallPackages(adapter.postinstallPackages())
.withCiBuild(adapter.ciBuild());
.withCiBuild(adapter.ciBuild())
.withForceProductionBuild(adapter.forceProductionBuild());
new NodeTasks(options).execute();
} catch (ExecutionFailedException exception) {
throw exception;
Expand Down
Expand Up @@ -17,6 +17,8 @@

import java.io.File;

import com.vaadin.flow.server.InitParameters;

/**
* Gives access to plugin-specific implementations and configurations.
*
Expand Down Expand Up @@ -76,4 +78,13 @@ public interface PluginAdapterBuild extends PluginAdapterBase {
* @return true if ci build should be enabled
*/
boolean ciBuild();

/**
* Setting this to {@code true} will force a build of the production build
* even if there is a default production bundle that could be used.
*
* Created production bundle optimization is defined by
* {@link #optimizeBundle} parameter.
*/
boolean forceProductionBuild();
}
Expand Up @@ -221,4 +221,9 @@ public class InitParameters implements Serializable {
* Configuration name for disabling dev bundle rebuild.
*/
public static final String SKIP_DEV_BUNDLE_REBUILD = "skip.dev.bundle";

/**
* Configuration name for forcing optimized production bundle build.
*/
public static final String FORCE_PRODUCTION_BUILD = "force.production.build";
}
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -25,6 +26,7 @@
import com.vaadin.flow.component.WebComponentExporterFactory;
import com.vaadin.flow.internal.StringUtil;
import com.vaadin.flow.internal.UsageStatistics;
import com.vaadin.flow.internal.hilla.EndpointRequestUtil;
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.Mode;
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
Expand Down Expand Up @@ -69,8 +71,16 @@ public static boolean needsBuild(Options options,
try {
boolean needsBuild;
if (Mode.PRODUCTION == mode) {
needsBuild = needsBuildProdBundle(options, frontendDependencies,
finder);
if (options.isForceProductionBuild()
|| EndpointRequestUtil.isHillaAvailable()) {
getLogger().info("Frontend build requested.");
saveResultInFile(true, options);
return true;
} else {
needsBuild = needsBuildProdBundle(options,
frontendDependencies, finder);
saveResultInFile(needsBuild, options);
}
} else if (Mode.DEVELOPMENT_BUNDLE == mode) {
needsBuild = needsBuildDevBundle(options, frontendDependencies,
finder);
Expand All @@ -80,10 +90,6 @@ public static boolean needsBuild(Options options,
throw new IllegalArgumentException("Unexpected mode");
}

if (options.isProductionMode()) {
saveResultInFile(needsBuild, options);
}

if (needsBuild) {
getLogger().info("A {} mode bundle build is needed", mode);
} else {
Expand Down
Expand Up @@ -64,6 +64,8 @@ public class Options implements Serializable {

private boolean ciBuild;

private boolean forceProductionBuild;

private boolean useGlobalPnpm = false;

private File frontendGeneratedFolder;
Expand Down Expand Up @@ -408,6 +410,15 @@ public Options withCiBuild(boolean ciBuild) {
return this;
}

/**
* Setting this to {@code true} will force a build of the production build
* even if there is a default production bundle that could be used.
*/
public Options withForceProductionBuild(boolean forceProductionBuild) {
this.forceProductionBuild = forceProductionBuild;
return this;
}

/**
* Uses globally installed pnpm tool for frontend packages installation.
*
Expand Down Expand Up @@ -721,6 +732,10 @@ public boolean isCiBuild() {
return ciBuild;
}

public boolean isForceProductionBuild() {
return forceProductionBuild;
}

public boolean isUseGlobalPnpm() {
return useGlobalPnpm;
}
Expand Down
Expand Up @@ -111,6 +111,11 @@ public void teardown() {
frontendUtils.close();
devBundleUtils.close();
bundleUtils.close();
File needsBuildFile = new File(options.getResourceOutputDirectory(),
Constants.NEEDS_BUNDLE_BUILD_FILE);
if (needsBuildFile.exists()) {
needsBuildFile.delete();
}
}

private JsonObject getBasicStats() {
Expand Down Expand Up @@ -1604,6 +1609,19 @@ public void bundleMissesSomeEntries_devMode_skipBundleBuildSet_noBundleRebuild()
Assert.assertFalse("Rebuild should be skipped", needsBuild);
}

@Test
public void forceProductionBundle_bundleRequired() {
Assume.assumeTrue(mode == Mode.PRODUCTION);

options.withForceProductionBuild(true);

final boolean needsBuild = BundleValidationUtil.needsBuild(options,
Mockito.mock(FrontendDependenciesScanner.class), finder, mode);
Assert.assertTrue(
"Production bundle required due to force.production.bundle flag.",
needsBuild);
}

private void createPackageJsonStub(String content) throws IOException {
File packageJson = new File(temporaryFolder.getRoot(),
Constants.PACKAGE_JSON);
Expand Down
Expand Up @@ -34,7 +34,7 @@ public class CssLoadingIT extends ChromeBrowserTest {
private static final String BLUE_RGBA = "rgba(0, 0, 255, 1)";
private static final String GREEN_RGBA = "rgba(0, 255, 0, 1)";
private static final String YELLOW_RGBA = "rgba(255, 255, 0, 1)";
private static final String STYLESHEET_LUMO_FONT_SIZE_M = " 1.1rem";
private static final String STYLESHEET_LUMO_FONT_SIZE_M = "1.1rem";

@Test
public void CssImport_overrides_Lumo() {
Expand All @@ -45,7 +45,7 @@ public void CssImport_overrides_Lumo() {
STYLESHEET_LUMO_FONT_SIZE_M,
executeScript(
"return getComputedStyle(arguments[0]).getPropertyValue('--lumo-font-size-m')",
htmlElement));
htmlElement).toString().trim());
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions flow-tests/test-express-build/test-prod-bundle/pom.xml
Expand Up @@ -73,6 +73,9 @@
</goals>
</execution>
</executions>
<configuration>
<forceProductionBuild>true</forceProductionBuild>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
Expand Down

0 comments on commit f1a072f

Please sign in to comment.