Skip to content

Commit 70d1398

Browse files
vaadin-botplatoshamcollovati
authored
fix: add package.json overrides for workbox dependencies (#24008) (CP: 25.1) (#24111)
This PR cherry-picks changes from the original PR #24008 to branch 25.1. --- #### Original PR description > - Add PwaConfiguration parameter to TaskUpdateVite constructor > - Remove service worker plugin and its imports from vite.generated.ts when offline build is not enabled > - Use FrontendDependenciesScanner in TaskGeneratePackageJson to generate correct dev dependencies > - Enhance TaskUpdatePackages for proper override handling > - Move all workbox dependencies to `workbox/package.json` and only add when offline sw build is needed > - Add overrides for workbox dependencies to package.json > - Add overrides support to NodeUpdater > - Remove empty vaadin.overrides from package.json --------- Co-authored-by: Anton Platonov <anton@vaadin.com> Co-authored-by: Marco Collovati <marco@vaadin.com>
1 parent 637b5b8 commit 70d1398

32 files changed

+2153
-476
lines changed

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/AbstractUpdateImports.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ abstract class AbstractUpdateImports implements Runnable {
116116

117117
private final Map<Path, List<String>> resolvedImportPathsCache = new HashMap<>();
118118

119-
private FrontendDependenciesScanner scanner;
120-
121119
private ClassFinder classFinder;
122120

123121
final File generatedFlowImports;
@@ -129,19 +127,17 @@ abstract class AbstractUpdateImports implements Runnable {
129127

130128
private final GeneratedFilesSupport generatedFilesSupport;
131129

132-
AbstractUpdateImports(Options options,
133-
FrontendDependenciesScanner scanner) {
134-
this(options, scanner, new GeneratedFilesSupport());
130+
AbstractUpdateImports(Options options) {
131+
this(options, new GeneratedFilesSupport());
135132
}
136133

137-
AbstractUpdateImports(Options options, FrontendDependenciesScanner scanner,
134+
AbstractUpdateImports(Options options,
138135
GeneratedFilesSupport generatedFilesSupport) {
139136
this.generatedFilesSupport = generatedFilesSupport;
140137
this.options = options;
141-
this.scanner = scanner;
142138
this.classFinder = options.getClassFinder();
143139
this.themeToLocalPathConverter = createThemeToLocalPathConverter(
144-
scanner.getTheme());
140+
options.getFrontendDependenciesScanner().getTheme());
145141

146142
generatedFlowImports = FrontendUtils
147143
.getFlowGeneratedImports(options.getFrontendDirectory());
@@ -168,7 +164,8 @@ public void run() {
168164
getLogger().debug("Start updating imports file and chunk files.");
169165
long start = System.nanoTime();
170166

171-
Map<ChunkInfo, List<CssData>> css = scanner.getCss();
167+
Map<ChunkInfo, List<CssData>> css = options
168+
.getFrontendDependenciesScanner().getCss();
172169
Map<ChunkInfo, List<String>> javascript = getMergedJavascript();
173170

174171
Map<File, List<String>> output = process(css, javascript);
@@ -185,6 +182,8 @@ private Map<ChunkInfo, List<String>> getMergedJavascript() {
185182
long start = System.nanoTime();
186183

187184
Map<ChunkInfo, List<String>> javascript;
185+
final FrontendDependenciesScanner scanner = options
186+
.getFrontendDependenciesScanner();
188187
Map<ChunkInfo, List<String>> modules = scanner.getModules();
189188
Map<ChunkInfo, List<String>> scripts = scanner.getScripts();
190189

@@ -713,7 +712,8 @@ private Set<String> getUniqueEs6ImportPaths(Collection<String> modules) {
713712
Set<String> npmNotFound = new HashSet<>();
714713
Set<String> resourceNotFound = new HashSet<>();
715714
Set<String> es6ImportPaths = new LinkedHashSet<>();
716-
AbstractTheme theme = scanner.getTheme();
715+
AbstractTheme theme = options.getFrontendDependenciesScanner()
716+
.getTheme();
717717

718718
Set<String> visited = new HashSet<>();
719719

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/BundleValidationUtil.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ private static boolean needsBuildInternal(Options options,
263263
((ObjectNode) statsJson.get(FRONTEND_HASHES_STATS_KEY)).remove(
264264
FrontendUtils.GENERATED + FrontendUtils.COMMERCIAL_BANNER_JS);
265265

266-
if (!BundleValidationUtil.frontendImportsFound(statsJson, options,
267-
frontendDependencies)) {
266+
if (!BundleValidationUtil.frontendImportsFound(statsJson, options)) {
268267
UsageStatistics.markAsUsed(
269268
"flow/rebundle-reason-missing-frontend-import", null);
270269
return true;
@@ -344,8 +343,7 @@ public static JsonNode getPackageJson(Options options,
344343
public static JsonNode getDefaultPackageJson(Options options,
345344
FrontendDependenciesScanner frontendDependencies,
346345
ObjectNode packageJson) {
347-
NodeUpdater nodeUpdater = new NodeUpdater(frontendDependencies,
348-
options) {
346+
NodeUpdater nodeUpdater = new NodeUpdater(options) {
349347
@Override
350348
public void execute() {
351349
}
@@ -651,12 +649,11 @@ private static String getTag(
651649
}
652650

653651
public static boolean frontendImportsFound(JsonNode statsJson,
654-
Options options, FrontendDependenciesScanner frontendDependencies)
655-
throws IOException {
652+
Options options) throws IOException {
656653

657654
// Validate frontend requirements in flow-generated-imports.js
658655
final GenerateMainImports generateMainImports = new GenerateMainImports(
659-
frontendDependencies, options, statsJson);
656+
options, statsJson);
660657
generateMainImports.run();
661658
final List<String> imports = generateMainImports.getLines().stream()
662659
.filter(line -> line.startsWith("import"))

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/GenerateMainImports.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import com.vaadin.flow.internal.JacksonUtils;
3030
import com.vaadin.flow.server.frontend.scanner.CssData;
31-
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
3231

3332
/**
3433
* Collect generated-flow-imports content for project to use to determine if
@@ -43,9 +42,8 @@ public class GenerateMainImports extends AbstractUpdateImports {
4342
private JsonNode statsJson;
4443
private Map<File, List<String>> output;
4544

46-
public GenerateMainImports(FrontendDependenciesScanner frontendDepScanner,
47-
Options options, JsonNode statsJson) {
48-
super(options, frontendDepScanner);
45+
public GenerateMainImports(Options options, JsonNode statsJson) {
46+
super(options);
4947
this.statsJson = statsJson;
5048
}
5149

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/NodeTasks.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ public NodeTasks(Options options) {
203203
TaskUpdatePackages packageUpdater = null;
204204
if (options.isEnablePackagesUpdate()
205205
&& options.getJarFrontendResourcesFolder() != null) {
206-
packageUpdater = new TaskUpdatePackages(frontendDependencies,
207-
options);
206+
packageUpdater = new TaskUpdatePackages(options);
208207
commands.add(packageUpdater);
209208
}
210209

@@ -241,7 +240,7 @@ public NodeTasks(Options options) {
241240
// available)
242241
addEndpointServicesTasks(options);
243242

244-
commands.add(new TaskGenerateBootstrap(frontendDependencies, options));
243+
commands.add(new TaskGenerateBootstrap(options));
245244

246245
commands.add(new TaskGenerateFeatureFlags(options));
247246

@@ -279,7 +278,7 @@ public NodeTasks(Options options) {
279278
}
280279

281280
if (options.isEnableImportsUpdate()) {
282-
commands.add(new TaskUpdateImports(frontendDependencies, options));
281+
commands.add(new TaskUpdateImports(options));
283282

284283
commands.add(new TaskUpdateThemeImport(
285284
frontendDependencies.getThemeDefinition(), options));

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/NodeUpdater.java

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.function.Supplier;
3333
import java.util.stream.Collectors;
3434

35+
import org.jspecify.annotations.Nullable;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738
import tools.jackson.databind.JsonNode;
@@ -45,9 +46,8 @@
4546
import com.vaadin.flow.internal.StringUtil;
4647
import com.vaadin.flow.internal.hilla.EndpointRequestUtil;
4748
import com.vaadin.flow.server.Constants;
49+
import com.vaadin.flow.server.PwaConfiguration;
4850
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
49-
import com.vaadin.flow.server.frontend.scanner.FrontendDependencies;
50-
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
5151

5252
import static com.vaadin.flow.server.Constants.PACKAGE_JSON;
5353
import static com.vaadin.flow.server.Constants.PACKAGE_LOCK_JSON;
@@ -93,12 +93,6 @@ public abstract class NodeUpdater implements FallibleCommand {
9393
static final String VAADIN_VERSION = "vaadinVersion";
9494
static final String PROJECT_FOLDER = "projectFolder";
9595

96-
/**
97-
* The {@link FrontendDependencies} object representing the application
98-
* dependencies.
99-
*/
100-
protected final FrontendDependenciesScanner frontDeps;
101-
10296
final ClassFinder finder;
10397

10498
boolean modified;
@@ -110,15 +104,11 @@ public abstract class NodeUpdater implements FallibleCommand {
110104
/**
111105
* Constructor.
112106
*
113-
* @param frontendDependencies
114-
* a reusable frontend dependencies
115107
* @param options
116108
* the task options
117109
*/
118-
protected NodeUpdater(FrontendDependenciesScanner frontendDependencies,
119-
Options options) {
110+
protected NodeUpdater(Options options) {
120111
this.finder = options.getClassFinder();
121-
this.frontDeps = frontendDependencies;
122112
this.options = options;
123113
}
124114

@@ -324,27 +314,37 @@ Map<String, String> getDefaultDependencies() {
324314
return dependencies;
325315
}
326316

327-
Map<String, String> readDependencies(String id, String packageJsonKey) {
317+
@Nullable
318+
JsonNode readPackageJsonKey(String id, String packageJsonKey) {
319+
final JsonNode packageJson;
328320
try {
329-
Map<String, String> map = new HashMap<>();
330-
JsonNode dependencies = readPackageJson(id).get(packageJsonKey);
331-
if (dependencies == null) {
332-
log().error("Unable to find " + packageJsonKey + " from '" + id
333-
+ "'");
334-
return new HashMap<>();
335-
}
336-
for (String key : JacksonUtils.getKeys(dependencies)) {
337-
map.put(key, dependencies.get(key).asString());
338-
}
339-
340-
return map;
321+
packageJson = readPackageJson(id);
341322
} catch (IOException e) {
342323
log().error(
343324
"Unable to read " + packageJsonKey + " from '" + id + "'",
344325
e);
345-
return new HashMap<>();
326+
return null;
327+
}
328+
var value = packageJson.get(packageJsonKey);
329+
if (value == null) {
330+
log().error(
331+
"Unable to find " + packageJsonKey + " from '" + id + "'");
332+
}
333+
return value;
334+
}
335+
336+
Map<String, String> readDependencies(String id, String packageJsonKey) {
337+
Map<String, String> map = new HashMap<>();
338+
JsonNode dependencies = readPackageJsonKey(id, packageJsonKey);
339+
if (dependencies == null) {
340+
return map;
346341
}
347342

343+
for (String key : JacksonUtils.getKeys(dependencies)) {
344+
map.put(key, dependencies.get(key).asString());
345+
}
346+
347+
return map;
348348
}
349349

350350
JsonNode readPackageJson(String id) throws IOException {
@@ -395,14 +395,33 @@ Map<String, String> getDefaultDevDependencies() {
395395
}
396396

397397
// Add workbox dependencies only when PWA is enabled
398-
if (frontDeps != null && frontDeps.getPwaConfiguration() != null
399-
&& frontDeps.getPwaConfiguration().isEnabled()) {
398+
final PwaConfiguration pwaConfiguration = options
399+
.getFrontendDependenciesScanner().getPwaConfiguration();
400+
if (pwaConfiguration != null && pwaConfiguration.isOfflineEnabled()) {
400401
defaults.putAll(readDependencies("workbox", "devDependencies"));
401402
}
402403

403404
return defaults;
404405
}
405406

407+
ObjectNode getDefaultOverrides() {
408+
var overrides = JacksonUtils.createObjectNode();
409+
410+
final PwaConfiguration pwaConfiguration = options
411+
.getFrontendDependenciesScanner().getPwaConfiguration();
412+
// Currently, we only have overrides for workbox uses overrides, and
413+
// only when PWA is enabled
414+
final ObjectNode workboxOverrides = pwaConfiguration != null
415+
&& pwaConfiguration.isOfflineEnabled()
416+
? (ObjectNode) readPackageJsonKey("workbox",
417+
"overrides")
418+
: null;
419+
if (workboxOverrides != null) {
420+
overrides = overrides.setAll(workboxOverrides);
421+
}
422+
return overrides;
423+
}
424+
406425
/**
407426
* Updates default dependencies and development dependencies to
408427
* package.json.

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/TaskGenerateBootstrap.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.slf4j.LoggerFactory;
2525

2626
import com.vaadin.flow.internal.FrontendUtils;
27-
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
2827
import com.vaadin.flow.theme.ThemeDefinition;
2928

3029
import static com.vaadin.flow.internal.FrontendUtils.BOOTSTRAP_FILE_NAME;
@@ -47,13 +46,10 @@ public class TaskGenerateBootstrap extends AbstractTaskClientGenerator {
4746
static final String DEV_TOOLS_IMPORT = String.format(
4847
"import '%svaadin-dev-tools.js';%n",
4948
FrontendUtils.JAR_RESOURCES_IMPORT + "vaadin-dev-tools/");
50-
private final FrontendDependenciesScanner frontDeps;
5149
private final Options options;
5250
private List<TypeScriptBootstrapModifier> modifiers;
5351

54-
TaskGenerateBootstrap(FrontendDependenciesScanner frontDeps,
55-
Options options) {
56-
this.frontDeps = frontDeps;
52+
TaskGenerateBootstrap(Options options) {
5753
this.options = options;
5854
this.modifiers = new ArrayList<>();
5955
for (Class<? extends TypeScriptBootstrapModifier> modifierClass : options
@@ -84,7 +80,8 @@ protected String getFileContent() {
8480
lines.addAll(getThemeLines());
8581

8682
for (TypeScriptBootstrapModifier modifier : modifiers) {
87-
modifier.modify(lines, options, frontDeps);
83+
modifier.modify(lines, options,
84+
options.getFrontendDependenciesScanner());
8885
}
8986
lines.add(0,
9087
String.format("import './%s';%n", FEATURE_FLAGS_FILE_NAME));
@@ -100,7 +97,7 @@ protected File getGeneratedFile() {
10097

10198
@Override
10299
protected boolean shouldGenerate() {
103-
return frontDeps != null;
100+
return options.getFrontendDependenciesScanner() != null;
104101
}
105102

106103
private String getIndexTsEntryPath() {
@@ -118,7 +115,8 @@ private String getIndexTsEntryPath() {
118115
private Collection<String> getThemeLines() {
119116
Collection<String> lines = new ArrayList<>();
120117
lines.add("import './app-shell-imports.js';");
121-
ThemeDefinition themeDef = frontDeps.getThemeDefinition();
118+
ThemeDefinition themeDef = options.getFrontendDependenciesScanner()
119+
.getThemeDefinition();
122120
if (themeDef != null && !"".equals(themeDef.getName())) {
123121
lines.add("import './theme-" + themeDef.getName()
124122
+ ".global.generated.js';");

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/TaskGeneratePackageJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class TaskGeneratePackageJson extends NodeUpdater {
3636
* build options
3737
*/
3838
TaskGeneratePackageJson(Options options) {
39-
super(null, options);
39+
super(options);
4040
}
4141

4242
@Override

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/TaskUpdateImports.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.vaadin.flow.component.dependency.JsModule;
2121
import com.vaadin.flow.internal.FrontendUtils;
2222
import com.vaadin.flow.server.Constants;
23-
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
2423
import com.vaadin.flow.theme.Theme;
2524

2625
/**
@@ -37,9 +36,8 @@ public class TaskUpdateImports extends NodeUpdater {
3736

3837
private class UpdateMainImportsFile extends AbstractUpdateImports {
3938
UpdateMainImportsFile(Options options,
40-
FrontendDependenciesScanner scanner,
4139
GeneratedFilesSupport generatedFilesSupport) {
42-
super(options, scanner, generatedFilesSupport);
40+
super(options, generatedFilesSupport);
4341
}
4442

4543
@Override
@@ -59,14 +57,11 @@ protected String getImportsNotFoundMessage() {
5957
/**
6058
* Create an instance of the updater given all configurable parameters.
6159
*
62-
* @param frontendDepScanner
63-
* a reusable frontend dependencies scanner
6460
* @param options
6561
* options for the task
6662
*/
67-
TaskUpdateImports(FrontendDependenciesScanner frontendDepScanner,
68-
Options options) {
69-
super(frontendDepScanner, options);
63+
TaskUpdateImports(Options options) {
64+
super(options);
7065
}
7166

7267
@Override
@@ -77,7 +72,7 @@ public void setGeneratedFileSupport(GeneratedFilesSupport support) {
7772
@Override
7873
public void execute() {
7974
UpdateMainImportsFile mainUpdate = new UpdateMainImportsFile(options,
80-
frontDeps, generatedFilesSupport);
75+
generatedFilesSupport);
8176
mainUpdate.run();
8277
}
8378

0 commit comments

Comments
 (0)