Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Move default frontend to src/main #18803

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ vaadin-dev-server/src/main/resources/META-INF/frontend
vaadin-dev-server/src/main/resources/META-INF/metadata

/flow-tests/**/frontend/routes.tsx
flow-tests/**/src/main/bundles/*
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public class BuildDevBundleMojo extends AbstractMojo
@Parameter(property = InitParameters.REACT_ENABLE, defaultValue = "true")
private boolean reactEnable;

/**
* A directory with project's frontend source files.
*/
@Parameter(defaultValue = "${project.basedir}/src/main/" + FRONTEND)
private File frontendDirectory;

@Override
public void execute() throws MojoFailureException {
long start = System.nanoTime();
Expand Down Expand Up @@ -262,12 +268,12 @@ public boolean eagerServerLoad() {

@Override
public File frontendDirectory() {
return new File(projectBasedir, FRONTEND);
return frontendDirectory;
}

@Override
public File generatedTsFolder() {
return new File(projectBasedir, FRONTEND + "/generated");
return new File(frontendDirectory(), "generated");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ class VaadinSmokeTest : AbstractGradleTest() {
*/
@Test
fun vaadinPrepareFrontendDeletesFrontendGeneratedFolder() {
val generatedFolder = testProject.newFolder("frontend/generated")
val generatedFile = testProject.newFile("frontend/generated/index.ts")
val generatedFlowFolder = testProject.newFolder("frontend/generated/flow")
val generatedOldFlowFile = testProject.newFolder("frontend/generated/flow/extra.js")
val generatedFolder = testProject.newFolder(FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR)
val generatedFile = testProject.newFile(FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "index.ts")
val generatedFlowFolder = testProject.newFolder(FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "flow")
val generatedOldFlowFile = testProject.newFolder(FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "/flow/extra.js")
testProject.build("vaadinPrepareFrontend")
expect(true) { generatedFolder.exists() }
expect(false) { generatedFile.exists() }
Expand Down Expand Up @@ -166,8 +166,8 @@ class VaadinSmokeTest : AbstractGradleTest() {
*/
@Test
fun vaadinCleanDeletesGeneratedFolder() {
val generatedFolder = testProject.newFolder("frontend/generated")
val generatedFile = testProject.newFile("frontend/generated/index.ts")
val generatedFolder = testProject.newFolder(FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR)
val generatedFile = testProject.newFile(FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "index.ts")
testProject.build("vaadinClean")
expect(false) { generatedFile.exists() }
expect(false) { generatedFolder.exists() }
Expand Down Expand Up @@ -317,11 +317,11 @@ class VaadinSmokeTest : AbstractGradleTest() {
result.expectTaskSucceded("vaadinBuildFrontend")

expect(false) {
File(testProject.dir, "frontend/generated/index.ts").exists()
File(testProject.dir, FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "index.ts").exists()
}
expect(true) {
// Only generated for executing project or building bundle
File(testProject.dir, "src/main/frontend/generated/index.tsx").exists()
File(testProject.dir, FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "index.tsx").exists()
}
}

Expand Down Expand Up @@ -365,7 +365,7 @@ class VaadinSmokeTest : AbstractGradleTest() {
result.expectTaskSucceded("vaadinPrepareFrontend")
result.expectTaskSucceded("vaadinBuildFrontend")

val cssFile = File(testProject.dir, "frontend/generated/jar-resources/mystyle.css")
val cssFile = File(testProject.dir, FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "jar-resources/mystyle.css")
expect(true, cssFile.toString()) { cssFile.exists() }

}
Expand Down Expand Up @@ -403,7 +403,7 @@ class VaadinSmokeTest : AbstractGradleTest() {
}

private fun enableHilla() {
testProject.newFolder("frontend")
testProject.newFile("frontend/index.ts")
testProject.newFolder(FrontendUtils.DEFAULT_FRONTEND_DIR)
testProject.newFile(FrontendUtils.DEFAULT_FRONTEND_DIR + "index.ts")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public class PluginEffectiveConfiguration(
.convention(project.projectDir)

public val frontendDirectory: Provider<File> = extension.frontendDirectory
.convention(File(project.projectDir, "frontend"))
.convention(File(project.projectDir, FrontendUtils.DEFAULT_FRONTEND_DIR))

public val generateBundle: Provider<Boolean> = extension.generateBundle
.convention(true)
Expand Down Expand Up @@ -362,7 +362,7 @@ public class PluginEffectiveConfiguration(
.convention(File(project.projectDir, "src/main/resources"))

public val generatedTsFolder: Property<File> = extension.generatedTsFolder
.convention(File(project.projectDir, "frontend/generated"))
.convention(File(project.projectDir, FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR))

public val nodeVersion: Property<String> = extension.nodeVersion
.convention(FrontendTools.DEFAULT_NODE_VERSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ public abstract class FlowModeAbstractMojo extends AbstractMojo
/**
* A directory with project's frontend source files.
*/
@Parameter(defaultValue = "${project.basedir}/" + FRONTEND)
@Parameter(defaultValue = "${project.basedir}/src/main/" + FRONTEND)
private File frontendDirectory;

/**
* The folder where flow will put TS API files for client projects.
*/
@Parameter(defaultValue = "${project.basedir}/" + FRONTEND + "/generated")
@Parameter(defaultValue = "${null}")
private File generatedTsFolder;

/**
Expand Down Expand Up @@ -304,8 +304,10 @@ public File frontendDirectory() {

@Override
public File generatedTsFolder() {

return generatedTsFolder;
if (generatedTsFolder != null) {
return generatedTsFolder;
}
return new File(frontendDirectory(), "/generated");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void setup() throws Exception {
defaultJavaSource = new File(".", "src/test/java");
openApiJsonFile = new File(npmFolder,
"target/classes/com/vaadin/hilla/openapi.json");
generatedTsFolder = new File(npmFolder, "frontend/generated");
generatedTsFolder = new File(npmFolder, "src/main/frontend/generated");

Assert.assertTrue("Failed to create a test project resources",
projectFrontendResourcesDirectory.mkdirs());
Expand Down Expand Up @@ -469,7 +469,7 @@ public void existingTokenFile_parametersShouldBeRemoved()
JsonObject initialBuildInfo = Json.createObject();
initialBuildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE, false);
initialBuildInfo.put(Constants.NPM_TOKEN, "npm");
initialBuildInfo.put(Constants.FRONTEND_TOKEN, "frontend");
initialBuildInfo.put(Constants.FRONTEND_TOKEN, "src/main/frontend");

initialBuildInfo.put(InitParameters.SERVLET_PARAMETER_ENABLE_PNPM,
true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void setup() throws Exception {
Mockito.when(project.getBasedir()).thenReturn(projectBase);

packageJson = new File(projectBase, PACKAGE_JSON).getAbsolutePath();
frontendGenerated = new File(projectBase, "frontend/generated");
frontendGenerated = new File(projectBase,
"src/main/frontend/generated");

ReflectionUtils.setVariableValueInObject(mojo, Constants.NPM_TOKEN,
projectBase);
Expand All @@ -75,7 +76,7 @@ public void setup() throws Exception {
"resourceOutputDirectory",
new File(projectBase, VAADIN_SERVLET_RESOURCES));
ReflectionUtils.setVariableValueInObject(mojo, "frontendDirectory",
new File(projectBase, "frontend"));
new File(projectBase, "src/main/frontend"));

ReflectionUtils.setVariableValueInObject(mojo, "openApiJsonFile",
new File(projectBase,
Expand Down Expand Up @@ -301,8 +302,9 @@ private void enableHilla() throws IOException {
.resolve("test-classes/com/vaadin/hilla"));
Files.createFile(Paths.get(projectBase.toString(), "target").resolve(
"test-classes/com/vaadin/hilla/EndpointController.class"));
Files.createDirectories(Paths.get(projectBase.toString(), "frontend"));
Files.createFile(Paths.get(projectBase.toString(), "frontend")
Files.createDirectories(
Paths.get(projectBase.toString(), "src/main/frontend"));
Files.createFile(Paths.get(projectBase.toString(), "src/main/frontend")
.resolve("index.ts"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public void setup() throws Exception {
VAADIN_SERVLET_RESOURCES);
defaultJavaSource = new File(".", "src/test/java");
defaultJavaResource = new File(".", "src/test/resources");
generatedTsFolder = new File(projectBase, "frontend/generated");
generatedTsFolder = new File(projectBase,
"src/main/frontend/generated");

ReflectionUtils.setVariableValueInObject(mojo, Constants.NPM_TOKEN,
projectBase);
Expand All @@ -111,7 +112,7 @@ public void setup() throws Exception {
ReflectionUtils.setVariableValueInObject(mojo,
"resourceOutputDirectory", resourceOutputDirectory);
ReflectionUtils.setVariableValueInObject(mojo, "frontendDirectory",
new File(projectBase, "frontend"));
new File(projectBase, "src/main/frontend"));

ReflectionUtils.setVariableValueInObject(mojo, "openApiJsonFile",
new File(projectBase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setup() throws IOException {
adapter = Mockito.mock(PluginAdapterBuild.class);
Mockito.when(adapter.npmFolder()).thenReturn(baseDir);
Mockito.when(adapter.generatedTsFolder())
.thenReturn(new File(baseDir, "frontend/generated"));
.thenReturn(new File(baseDir, "src/main/frontend/generated"));
Mockito.when(adapter.projectBaseDirectory())
.thenReturn(tmpDir.getRoot().toPath());
ClassFinder classFinder = Mockito.mock(ClassFinder.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import com.vaadin.flow.internal.hilla.EndpointRequestUtil;
import com.vaadin.flow.server.frontend.BundleUtils;
import com.vaadin.flow.server.frontend.FileIOUtils;
import com.vaadin.flow.server.frontend.FrontendUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ private static URL findAssetInFrontendThemesOrDevBundle(
.getDeploymentConfiguration();
// First, look for the theme assets in the {project.root}/frontend/
// themes/my-theme folder
File frontendFolder = new File(
deploymentConfiguration.getProjectFolder(),
FrontendUtils.FRONTEND);
File frontendFolder = FrontendUtils
.getProjectFrontendDir(deploymentConfiguration);
File assetInFrontendThemes = new File(frontendFolder, assetPath);
if (assetInFrontendThemes.exists()) {
return assetInFrontendThemes.toURI().toURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public class FrontendUtils {
* Path of the folder containing application frontend source files, it needs
* to be relative to the {@link FrontendUtils#DEFAULT_NODE_DIR}
*
* By default it is <code>/frontend</code> in the project folder.
* By default it is <code>/src/main/frontend</code> in the project folder.
*/
public static final String DEFAULT_FRONTEND_DIR = DEFAULT_NODE_DIR
+ FRONTEND;
+ "src/main/" + FRONTEND;

/**
* The name of the vite configuration file.
Expand Down Expand Up @@ -505,8 +505,7 @@ private static Optional<DevModeHandler> activeDevModeHandler(

private static InputStream getFileFromFrontendDir(
AbstractConfiguration config, String path) {
File file = new File(new File(config.getProjectFolder(), "frontend"),
path);
File file = new File(getProjectFrontendDir(config), path);
if (file.exists()) {
try {
return Files.newInputStream(file.toPath());
Expand Down Expand Up @@ -556,7 +555,8 @@ public static InputStream getFrontendFileFromDevModeHandler(
if (devModeHandler.isPresent()) {
try {
File frontendFile = resolveFrontendPath(
devModeHandler.get().getProjectRoot(), path);
devModeHandler.get().getProjectRoot(),
service.getDeploymentConfiguration(), path);
return frontendFile == null ? null
: new FileInputStream(frontendFile);
} catch (IOException e) {
Expand All @@ -568,19 +568,33 @@ public static InputStream getFrontendFileFromDevModeHandler(

/**
* Looks up the frontend resource at the given path. If the path starts with
* {@code ./}, first look in {@code frontend}, then in
* {@value FrontendUtils#JAR_RESOURCES_FOLDER}. If the path does not start
* with {@code ./}, look in {@code node_modules} instead.
* {@code ./}, first look in {@value FrontendUtils#DEFAULT_FRONTEND_DIR},
* then in {@value FrontendUtils#JAR_RESOURCES_FOLDER}. If the path does not
* start with {@code ./}, look in {@code node_modules} instead.
*
* @param projectRoot
* the project root folder.
* @param deploymentConfiguration
* the active deployment configuration
* @param path
* the file path.
* @return an existing {@link File} , or null if the file doesn't exist.
*/
public static File resolveFrontendPath(File projectRoot, String path) {
public static File resolveFrontendPath(File projectRoot,
DeploymentConfiguration deploymentConfiguration, String path) {
return resolveFrontendPath(projectRoot, path,
new File(projectRoot, FrontendUtils.FRONTEND));
getFrontendFolder(projectRoot, deploymentConfiguration));
}

private static File getFrontendFolder(File projectRoot,
DeploymentConfiguration deploymentConfiguration) {
String frontendFolderPath = deploymentConfiguration.getStringProperty(
FrontendUtils.PARAM_FRONTEND_DIR,
FrontendUtils.DEFAULT_FRONTEND_DIR);
if (frontendFolderPath.startsWith("./")) {
return new File(projectRoot, frontendFolderPath);
}
return new File(frontendFolderPath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ JsonObject getPlatformPinnedDependencies() throws IOException {
private JsonObject getFilteredVersionsFromResource(URL versionsResource,
String versionsOrigin) throws IOException {
JsonObject versionsJson;

try (InputStream content = versionsResource.openStream()) {
VersionsJsonConverter convert = new VersionsJsonConverter(
Json.parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Task generating the theme definition file 'theme.js' for importing
* application theme into the generated frontend directory.
*
* Default directory is ' ./frontend/generated'
* Default directory is ' ./src/main/frontend/generated'
*
* <p>
* For internal use only. May be renamed or removed in a future release.
Expand All @@ -45,14 +45,13 @@
*/
public class TaskUpdateThemeImport implements FallibleCommand {

private static final String JAR_RESOURCES = "frontend/"
+ FrontendUtils.GENERATED + FrontendUtils.JAR_RESOURCES_FOLDER;
public static final String APPLICATION_META_INF_RESOURCES = "src/main/resources/META-INF/resources";
public static final String APPLICATION_STATIC_RESOURCES = "src/main/resources/static";
private static final String EXPORT_MODULES_DEF = "export declare const applyTheme: (target: Node) => void;";

private final File themeImportFile;
private final File themeImportFileDefinition;
private final String generatedFolder;
private final ThemeDefinition theme;
private final Options options;

Expand All @@ -61,6 +60,9 @@ public class TaskUpdateThemeImport implements FallibleCommand {
this.options = options;
File frontendGeneratedFolder = new File(options.getFrontendDirectory(),
GENERATED);
generatedFolder = options.getNpmFolder().toPath()
.relativize(frontendGeneratedFolder.toPath()).toString()
.replaceAll("\\\\", "/");
themeImportFile = new File(frontendGeneratedFolder, THEME_IMPORTS_NAME);
themeImportFileDefinition = new File(frontendGeneratedFolder,
THEME_IMPORTS_D_TS_NAME);
Expand Down Expand Up @@ -125,8 +127,11 @@ private void verifyThemeDirectoryExistence()
if (existingAppThemeDirectories.size() >= 2) {

boolean themeFoundInJar = existingAppThemeDirectories.stream()
.map(File::getPath).anyMatch(path -> path
.contains(Paths.get(JAR_RESOURCES).toString()));
.map(File::getPath)
.anyMatch(path -> path.contains(Paths
.get(generatedFolder,
FrontendUtils.JAR_RESOURCES_FOLDER)
.toString()));

if (themeFoundInJar) {
String errorMessage = "Theme '%s' should not exist inside a "
Expand Down Expand Up @@ -154,16 +159,17 @@ private void verifyThemeDirectoryExistence()
private List<String> getAppThemePossiblePaths(String themePath) {
String frontendTheme = String.join("/", options.getNpmFolder().toPath()
.relativize(options.getFrontendDirectory().toPath()).toString(),
themePath);
themePath).replaceAll("\\\\", "/");

String themePathInMetaInfResources = String.join("/",
APPLICATION_META_INF_RESOURCES, themePath);

String themePathInStaticResources = String.join("/",
APPLICATION_STATIC_RESOURCES, themePath);

String themePathInClassPathResources = String.join("", JAR_RESOURCES,
"/", themePath);
String themePathInClassPathResources = String.join("",
generatedFolder + "/" + FrontendUtils.JAR_RESOURCES_FOLDER, "/",
themePath);

return Arrays.asList(frontendTheme, themePathInMetaInfResources,
themePathInStaticResources, themePathInClassPathResources);
Expand Down
Loading
Loading