Skip to content

Commit

Permalink
Add react configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
DamnClin committed Sep 21, 2024
1 parent 3b512f7 commit bc757f3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package tech.jhipster.lite.generator.client.react.core.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.COMMON;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.REACT;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.*;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.*;

import java.util.function.Consumer;
import tech.jhipster.lite.module.domain.Indentation;
Expand Down Expand Up @@ -51,7 +50,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.addDevDependency(packageName("vite"), COMMON)
.addDependency(packageName("react"), REACT)
.addDependency(packageName("react-dom"), REACT)
.addScript(scriptKey("dev"), scriptCommand("npm-run-all dev:*"))
.addScript(scriptKey("dev"), scriptCommand("npm-run-all --parallel dev:*"))
.addScript(scriptKey("dev:vite"), scriptCommand("vite"))
.addScript(scriptKey("build"), scriptCommand("npm-run-all build:*"))
.addScript(scriptKey("build:tsc"), scriptCommand("tsc"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.*;

import java.util.regex.Pattern;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.replacement.RegexNeedleAfterReplacer;
import tech.jhipster.lite.module.domain.replacement.ReplacementCondition;

public class TikuiModuleFactory {

Expand Down Expand Up @@ -114,7 +117,8 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.and()
.optionalReplacements()
.in(path("vite.config.ts"))
.add(lineAfterRegex("port:\\s*9000,"), vueStyleProxy(properties))
.add(existingProxyReplacer(), proxyForStyle(properties))
.add(newProxyReplacer(), newProxyForStyle(properties))
.and()
.in(path("src/main/webapp/index.html"))
.add(lineBeforeText("</head>"), tikuiLink(properties))
Expand All @@ -127,19 +131,40 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
//@formatter:on
}

private static RegexNeedleAfterReplacer newProxyReplacer() {
return new RegexNeedleAfterReplacer(ReplacementCondition.notContaining("proxy:"), Pattern.compile("port:\\s*9000,", Pattern.MULTILINE));
}

private static RegexNeedleAfterReplacer existingProxyReplacer() {
return new RegexNeedleAfterReplacer(
(contentBeforeReplacement, replacement) -> contentBeforeReplacement.contains("proxy:"),
Pattern.compile("proxy:\\s*\\{", Pattern.MULTILINE)
);
}

private String tikuiLink(JHipsterModuleProperties properties) {
return properties.indentation().times(2) + "<link rel=\"stylesheet\" href=\"/style/tikui.css\" />";
}

private static String vueStyleProxy(JHipsterModuleProperties properties) {
private static String newProxyForStyle(JHipsterModuleProperties properties) {
return new StringBuilder()
.append(properties.indentation().times(2))
.append("proxy: {")
.append(LINE_BREAK)
.append(proxyForStyle(properties))
.append(LINE_BREAK)
.append(properties.indentation().times(2))
.append("},")
.toString();
}

private static String proxyForStyle(JHipsterModuleProperties properties) {
return """
{S}{S}proxy: {
{S}{S}{S}'/style': {
{S}{S}{S}{S}ws: true,
{S}{S}{S}{S}changeOrigin: true,
{S}{S}{S}{S}rewrite: path => path.replace('/style', ''),
{S}{S}{S}{S}target: 'http://localhost:9005',
{S}{S}{S}},
{S}{S}},""".replace("{S}", properties.indentation().times(1));
{S}{S}{S}},""".replace("{S}", properties.indentation().times(1));
}
}
1 change: 0 additions & 1 deletion src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ application:
jhlite-hidden-resources:
slugs:
- svelte-core
- tikui

jhlite-preset-file:
name: preset.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void shouldBuildModuleWithStyle() {
.containing(nodeDependency("vite"))
.containing(nodeDependency("react"))
.containing(nodeDependency("react-dom"))
.containing(nodeScript("dev", "npm-run-all dev:*"))
.containing(nodeScript("dev", "npm-run-all --parallel dev:*"))
.containing(nodeScript("dev:vite", "vite"))
.containing(nodeScript("build", "npm-run-all build:*"))
.containing(nodeScript("build:tsc", "tsc"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ private static ModuleFile indexFile() {
}
}

@Nested
class ReactTest {

@Test
void shouldBuildModuleOnReactProject() {
assertThatTikuiModule(viteConfigFile(), indexFile()).hasFile("vite.config.ts").containing(styleProxy());
}

private static String styleProxy() {
return """
proxy: {
'/style': {
ws: true,
changeOrigin: true,
rewrite: path => path.replace('/style', ''),
target: 'http://localhost:9005',
},
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
},
},\
""";
}

public static ModuleFile viteConfigFile() {
return file("src/main/resources/generator/client/react/core/vite.config.ts.mustache", "vite.config.ts");
}

private static ModuleFile indexFile() {
return file("src/main/resources/generator/client/react/core/src/main/webapp/index.html.mustache", "src/main/webapp/index.html");
}
}

private static JHipsterModuleAsserter assertThatTikuiModule(ModuleFile proxyFile, ModuleFile indexFile) {
JHipsterModuleProperties properties = propertiesBuilder(TestFileUtils.tmpDirForTest()).build();

Expand Down

0 comments on commit bc757f3

Please sign in to comment.