Skip to content

Commit e12f503

Browse files
vaadin-botcaaladormshabarov
authored
feat: bundle should take into account index.ts (#15893) (#15895)
* feat: bundle should take into account index.ts Require rebundling for changes in frontend/index.ts file. Fixes #15883 * Update flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunDevBundleBuild.java --------- Co-authored-by: caalador <mikael.grankvist@vaadin.com> Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
1 parent 709a152 commit e12f503

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunDevBundleBuild.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ private static boolean packagedThemeAddedOrUpdated(Options options,
249249

250250
private static boolean frontendImportsFound(JsonObject statsJson,
251251
Options options, ClassFinder finder,
252-
FrontendDependenciesScanner frontendDependencies) {
252+
FrontendDependenciesScanner frontendDependencies)
253+
throws IOException {
253254

254255
// Validate frontend requirements in flow-generated-imports.js
255256
final GenerateMainImports generateMainImports = new GenerateMainImports(
@@ -313,6 +314,25 @@ private static boolean frontendImportsFound(JsonObject statsJson,
313314
faulty.toString());
314315
return false;
315316
}
317+
318+
File indexTs = new File(options.getFrontendDirectory(),
319+
FrontendUtils.INDEX_TS);
320+
if (indexTs.exists()) {
321+
if (!frontendHashes.hasKey(FrontendUtils.INDEX_TS)) {
322+
return false;
323+
}
324+
String content = FileUtils
325+
.readFileToString(indexTs, StandardCharsets.UTF_8)
326+
.replaceAll("\\r\\n", "\n");
327+
final String contentHash = StringUtil.getHash(content,
328+
StandardCharsets.UTF_8);
329+
if (!frontendHashes.getString(FrontendUtils.INDEX_TS)
330+
.equals(contentHash)) {
331+
getLogger().info("'index.ts' is not up to date");
332+
return false;
333+
}
334+
}
335+
316336
return true;
317337
}
318338

flow-server/src/main/resources/vite.generated.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ function statsExtracterPlugin(): PluginOption {
223223
// @ts-ignore
224224
frontendFiles[`${fileKey}`] = hash;
225225
});
226+
// If a index.ts exists hash it to be able to see if it changes.
227+
if (existsSync(path.resolve(frontendFolder, "index.ts"))) {
228+
const fileBuffer = readFileSync(path.resolve(frontendFolder, "index.ts"), {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
229+
const hash = createHash('sha256').update(fileBuffer, 'utf8').digest("hex");
230+
// @ts-ignore
231+
frontendFiles[`index.ts`] = hash;
232+
}
226233

227234
const themeJsonHashes = { };
228235
const themesFolder = path.resolve(jarResourcesFolder, "themes");

flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskRunDevBundleBuildTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,49 @@ public void reusedTheme_previouslyAddedThemes_noUpdates_noBundleRebuild()
10931093
}
10941094
}
10951095

1096+
@Test
1097+
public void changeInIndexTs_rebuildRequired() throws IOException {
1098+
createPackageJsonStub("{\"dependencies\": {}, "
1099+
+ "\"vaadin\": { \"hash\": \"aHash\"} }");
1100+
File frontendFolder = temporaryFolder.newFolder(FrontendUtils.FRONTEND);
1101+
1102+
File indexTs = new File(frontendFolder, FrontendUtils.INDEX_TS);
1103+
indexTs.createNewFile();
1104+
1105+
FileUtils.write(indexTs, "window.alert('');", StandardCharsets.UTF_8);
1106+
1107+
final FrontendDependenciesScanner depScanner = Mockito
1108+
.mock(FrontendDependenciesScanner.class);
1109+
1110+
JsonObject stats = getBasicStats();
1111+
stats.getObject(FRONTEND_HASHES).put(FrontendUtils.INDEX_TS,
1112+
"15931fa8c20e3c060c8ea491831e95cc8463962700a9bfb82c8e3844cf608f04");
1113+
1114+
try (MockedStatic<FrontendUtils> utils = Mockito
1115+
.mockStatic(FrontendUtils.class)) {
1116+
utils.when(() -> FrontendUtils.getDevBundleFolder(Mockito.any()))
1117+
.thenReturn(temporaryFolder.getRoot());
1118+
utils.when(() -> FrontendUtils
1119+
.findBundleStatsJson(temporaryFolder.getRoot()))
1120+
.thenReturn(stats.toJson());
1121+
1122+
boolean needsBuild = TaskRunDevBundleBuild
1123+
.needsBuildInternal(options, depScanner, finder);
1124+
Assert.assertFalse(
1125+
"'index.ts' equal content should not require bundling",
1126+
needsBuild);
1127+
1128+
FileUtils.write(indexTs, "window.alert('hello');",
1129+
StandardCharsets.UTF_8);
1130+
1131+
needsBuild = TaskRunDevBundleBuild.needsBuildInternal(options,
1132+
depScanner, finder);
1133+
Assert.assertTrue(
1134+
"changed content for 'index.ts' should require bundling",
1135+
needsBuild);
1136+
}
1137+
}
1138+
10961139
private void createPackageJsonStub(String content) throws IOException {
10971140
File packageJson = new File(temporaryFolder.getRoot(),
10981141
Constants.PACKAGE_JSON);

0 commit comments

Comments
 (0)