Skip to content

Commit

Permalink
feat!: Make react-router default
Browse files Browse the repository at this point in the history
Enable react router as default with
property to switch back to vaadin router.
  • Loading branch information
caalador committed Jan 2, 2024
1 parent 47b6436 commit b7d5007
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public class BuildDevBundleMojo extends AbstractMojo
@Parameter(property = "npm.postinstallPackages", defaultValue = "")
private List<String> postinstallPackages;

@Parameter(property = InitParameters.REACT_ROUTER_ENABLED, defaultValue = "true")
private boolean reactRouterEnabled;

@Override
public void execute() throws MojoFailureException {
long start = System.nanoTime();
Expand Down Expand Up @@ -431,4 +434,9 @@ public boolean isPrepareFrontendCacheDisabled() {
return false;
}

@Override
public boolean isReactRouterEnabled() {
return reactRouterEnabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,5 @@ internal class GradlePluginAdapter(

override fun isPrepareFrontendCacheDisabled(): Boolean = config.alwaysExecutePrepareFrontend.get()

override fun isReactRouterEnabled(): Boolean = config.reactRouterEnabled.get()
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ internal class PrepareFrontendInputProperties(private val config: PluginEffectiv

@Input
@Optional
public fun getPnpmExecutablePath(): Provider<String> = config.pnpmEnable.map { pnpmEnable ->
public fun getPnpmExecutablePath(): Provider<String> = config.pnpmEnable.map { pnpmEnable: Boolean ->
if (!pnpmEnable) {
return@map ""
}
val pnpmExecutable = tools.get().pnpmExecutable ?: listOf()
pnpmExecutable.joinToString(" ")
}

private fun initialiseFrontendToolsSettings(): Provider<FrontendTools> = config.npmFolder.map { npmFolder ->
private fun initialiseFrontendToolsSettings(): Provider<FrontendTools> = config.npmFolder.map { npmFolder: File ->
val settings = FrontendToolsSettings(npmFolder.absolutePath) {
FrontendUtils.getVaadinHomeDirectory()
.absolutePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ public abstract class VaadinFlowPluginExtension {
*/
public abstract val alwaysExecutePrepareFrontend: Property<Boolean>

public abstract val reactRouterEnabled: Property<Boolean>

public fun filterClasspath(@DelegatesTo(value = ClasspathFilter::class, strategy = Closure.DELEGATE_FIRST) block: Closure<*>) {
block.delegate = classpathFilter
block.resolveStrategy = Closure.DELEGATE_FIRST
Expand Down Expand Up @@ -411,6 +413,10 @@ internal class PluginEffectiveConfiguration(
val alwaysExecutePrepareFrontend: Property<Boolean> = extension.alwaysExecutePrepareFrontend
.convention(false)

val reactRouterEnabled: Provider<Boolean> = extension.reactRouterEnabled
.convention(true)
.overrideWithSystemProperty(InitParameters.REACT_ROUTER_ENABLED)

/**
* Finds the value of a boolean property. It searches in gradle and system properties.
*
Expand Down Expand Up @@ -457,7 +463,8 @@ internal class PluginEffectiveConfiguration(
"processResourcesTaskName=${processResourcesTaskName.get()}, " +
"skipDevBundleBuild=${skipDevBundleBuild.get()}, " +
"alwaysExecutePrepareFrontend=${alwaysExecutePrepareFrontend.get()}, " +
"frontendHotdeploy=${frontendHotdeploy.get()}" +
"frontendHotdeploy=${frontendHotdeploy.get()}," +
"reactRouterEnabled=${reactRouterEnabled.get()}" +
")"
companion object {
internal fun get(project: Project): PluginEffectiveConfiguration =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ public abstract class FlowModeAbstractMojo extends AbstractMojo
@Parameter(property = InitParameters.SKIP_DEV_BUNDLE_REBUILD, defaultValue = "false")
private boolean skipDevBundleRebuild;

@Parameter(property = InitParameters.REACT_ROUTER_ENABLED, defaultValue = "true")
private boolean reactRouterEnabled;

/**
* Generates a List of ClasspathElements (Run and CompileTime) from a
* MavenProject.
Expand Down Expand Up @@ -456,4 +459,8 @@ public boolean isPrepareFrontendCacheDisabled() {
return false;
}

@Override
public boolean isReactRouterEnabled() {
return reactRouterEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static com.vaadin.flow.server.InitParameters.FRONTEND_HOTDEPLOY;
import static com.vaadin.flow.server.InitParameters.NODE_DOWNLOAD_ROOT;
import static com.vaadin.flow.server.InitParameters.NODE_VERSION;
import static com.vaadin.flow.server.InitParameters.REACT_ROUTER_ENABLED;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_INITIAL_UIDL;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE;
import static com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES;
Expand Down Expand Up @@ -255,6 +256,8 @@ public static File propagateBuildInfo(PluginAdapterBase adapter) {
buildInfo.put(DISABLE_PREPARE_FRONTEND_CACHE, true);
}

buildInfo.put(REACT_ROUTER_ENABLED, adapter.isReactRouterEnabled());

try {
FileUtils.forceMkdir(token.getParentFile());
FileUtils.write(token, JsonUtil.stringify(buildInfo, 2) + "\n",
Expand Down Expand Up @@ -328,7 +331,8 @@ public static void runNodeUpdater(PluginAdapterBuild adapter)
.setJavaResourceFolder(adapter.javaResourceFolder())
.withPostinstallPackages(adapter.postinstallPackages())
.withCiBuild(adapter.ciBuild())
.withForceProductionBuild(adapter.forceProductionBuild());
.withForceProductionBuild(adapter.forceProductionBuild())
.withReactRouter(adapter.isReactRouterEnabled());
new NodeTasks(options).execute();
} catch (ExecutionFailedException exception) {
throw exception;
Expand Down Expand Up @@ -392,7 +396,8 @@ public static void runDevBuildNodeUpdater(PluginAdapterBuild adapter)
.withPostinstallPackages(adapter.postinstallPackages())
.withBundleBuild(true)
.skipDevBundleBuild(adapter.skipDevBundleBuild())
.withCompressBundle(adapter.compressBundle());
.withCompressBundle(adapter.compressBundle())
.withReactRouter(adapter.isReactRouterEnabled());
new NodeTasks(options).execute();
} catch (ExecutionFailedException exception) {
throw exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,10 @@ default Lookup createLookup(ClassFinder classFinder) {
*/
boolean isPrepareFrontendCacheDisabled();

/**
* Set react router as enabled or disabled.
*
* @return {@code true} for react router and {@code false} for vaadin-router
*/
boolean isReactRouterEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public class FeatureFlags implements Serializable {
"https://github.com/vaadin/form-filler-addon", true,
"com.vaadin.flow.ai.formfiller.FormFiller");

public static final Feature REACT_ROUTER = new Feature(
"React router support", "reactRouter",
"https://vaadin.com/docs/latest/integrations/hilla", true, null);

private List<Feature> features = new ArrayList<>();

File propertiesFolder = null;
Expand All @@ -93,7 +89,6 @@ public FeatureFlags(Lookup lookup) {
features.add(new Feature(COLLABORATION_ENGINE_BACKEND));
features.add(new Feature(WEB_PUSH));
features.add(new Feature(FORM_FILLER_ADDON));
features.add(new Feature(REACT_ROUTER));
loadProperties();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ default boolean isEagerServerLoad() {
* Check if the React Router is enabled for the project instead of Vaadin
* router.
*
* @return {@code true} if React Router is used
* @return {@code true} if React Router is used, on by default
*/
default boolean isReactRouterEnabled() {
return getBooleanProperty(InitParameters.REACT_ROUTER_ENABLED, false);
return getBooleanProperty(InitParameters.REACT_ROUTER_ENABLED, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,6 @@ private void readBuildInfo(Properties initParameters,
}
}

if (!initParameters.containsKey(REACT_ROUTER_ENABLED)) {
// TODO: When not a feature flag remember to synchronize with
// NodeTasks!!
// This is set immediately, but a marking in the token file may
// override this.
// But mainly this is now dictated by the feature flag setting.
initParameters.put(REACT_ROUTER_ENABLED,
String.valueOf(
new FeatureFlags(context.getAttribute(Lookup.class))
.isEnabled(FeatureFlags.REACT_ROUTER)));
}
}

private static void readUiFromEnclosingClass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ private void addBootstrapTasks(Options options) {
if (options.isProductionMode() || options.isFrontendHotdeploy()
|| options.isBundleBuild()) {
commands.add(new TaskGenerateIndexTs(options));
if (options.getFeatureFlags()
.isEnabled(FeatureFlags.REACT_ROUTER)) {
if (options.isReactRouterEnabled()) {
commands.add(new TaskGenerateReactFiles(options));
}
if (!options.isProductionMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private static <T extends JsonValue> T computeIfAbsent(
Map<String, String> getDefaultDependencies() {
Map<String, String> dependencies = readDependencies("default",
"dependencies");
if (options.getFeatureFlags().isEnabled(FeatureFlags.REACT_ROUTER)) {
if (options.isReactRouterEnabled()) {
dependencies
.putAll(readDependencies("react-router", "dependencies"));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class Options implements Serializable {

private boolean frontendHotdeploy = false;

private boolean reactRouterEnabled = true;

/**
* Creates a new instance.
*
Expand Down Expand Up @@ -859,4 +861,13 @@ public Options withCompressBundle(boolean compressBundle) {
public boolean isCompressBundle() {
return compressBundle;
}

public boolean isReactRouterEnabled() {
return reactRouterEnabled;
}

public Options withReactRouter(boolean reactRouterEnabled) {
this.reactRouterEnabled = reactRouterEnabled;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected boolean shouldGenerate() {
protected String getFileContent() throws IOException {
String indexTemplate;
String indexFile = INDEX_TS;
if (options.getFeatureFlags().isEnabled(FeatureFlags.REACT_ROUTER)) {
if (options.isReactRouterEnabled()) {
indexFile = "index-react.ts";
}
try (InputStream indexTsStream = getClass()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,11 @@ public void handle_variousInputs_checkPushStateShouldBeCalledOrNot() {

// given a locked session
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
session.setConfiguration(new MockDeploymentConfiguration());
MockDeploymentConfiguration configuration = new MockDeploymentConfiguration();
// When using react router we have the sever do the update in all cases
// to control the correct timing for url updates
configuration.setReactRouterEnabled(false);
session.setConfiguration(configuration);

// given a NavigationStateRenderer mapping to RegularView
new NavigationStateBuilder(router).withTarget(RegularView.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,11 @@ public void defaultProdBundleExists_noCompressedProdBundleFile_noBuildRequired()
packageJson.createNewFile();

FileUtils.write(packageJson,
"{\"dependencies\": {" + "\"@vaadin/router\": \"^1.7.5\"}, "
"{\"dependencies\": {" + " \"react\": \"18.2.0\",\n"
+ " \"react-dom\": \"18.2.0\",\n"
+ " \"react-router-dom\": \"6.18.0\",\n"
+ " \"@types/react\": \"18.2.37\",\n"
+ " \"@types/react-dom\": \"18.2.15\"}, "
+ "\"vaadin\": { \"hash\": \"aHash\"} }",
StandardCharsets.UTF_8);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ public void getDefaultDependencies_includesAllDependencies() {
Set<String> expectedDependencies = new HashSet<>();
expectedDependencies.add("@polymer/polymer");
expectedDependencies.add("@vaadin/common-frontend");
expectedDependencies.add("@vaadin/router");
// expectedDependencies.add("@vaadin/router");
expectedDependencies.add("construct-style-sheets-polyfill");
expectedDependencies.add("lit");
expectedDependencies.add("react");
expectedDependencies.add("react-dom");
expectedDependencies.add("react-router-dom");
expectedDependencies.add("@types/react");
expectedDependencies.add("@types/react-dom");

Set<String> actualDependendencies = defaultDeps.keySet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MockDeploymentConfiguration
private boolean eagerServerLoad = false;
private boolean devModeLiveReloadEnabled = false;
private boolean devToolsEnabled = true;
private boolean isReactRouterEnabled = true;
private SessionLockCheckStrategy sessionLockCheckStrategy = SessionLockCheckStrategy.ASSERT;

private File projectFolder = null;
Expand Down Expand Up @@ -219,4 +220,13 @@ public void setLockCheckStrategy(
SessionLockCheckStrategy sessionLockCheckStrategy) {
this.sessionLockCheckStrategy = sessionLockCheckStrategy;
}

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

public void setReactRouterEnabled(boolean isReactRouterEnabled) {
this.isReactRouterEnabled = isReactRouterEnabled;
}
}

This file was deleted.

0 comments on commit b7d5007

Please sign in to comment.