|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.base.devserver; |
| 17 | + |
| 18 | +import java.io.Closeable; |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import com.vaadin.flow.internal.BrowserLiveReload; |
| 28 | +import com.vaadin.flow.internal.BrowserLiveReloadAccessor; |
| 29 | +import com.vaadin.flow.server.VaadinContext; |
| 30 | + |
| 31 | +/** |
| 32 | + * Watches given resourcesFolder for CSS file changes and performs hot CSS |
| 33 | + * updates via BrowserLiveReload. |
| 34 | + */ |
| 35 | +class PublicResourcesCssLiveUpdater implements Closeable { |
| 36 | + |
| 37 | + private final File resourcesFolder; |
| 38 | + private final VaadinContext context; |
| 39 | + private FileWatcher watcher; |
| 40 | + |
| 41 | + PublicResourcesCssLiveUpdater(File resourcesFolder, VaadinContext context) |
| 42 | + throws IOException { |
| 43 | + this.resourcesFolder = resourcesFolder; |
| 44 | + this.context = context; |
| 45 | + initWatcher(); |
| 46 | + } |
| 47 | + |
| 48 | + private void initWatcher() throws IOException { |
| 49 | + if (resourcesFolder == null || !resourcesFolder.isDirectory()) { |
| 50 | + getLogger().debug( |
| 51 | + "Public resources folder {} not found, skipping watcher", |
| 52 | + resourcesFolder); |
| 53 | + return; |
| 54 | + } |
| 55 | + var liveReloadOpt = BrowserLiveReloadAccessor |
| 56 | + .getLiveReloadFromContext(context); |
| 57 | + if (liveReloadOpt.isEmpty()) { |
| 58 | + getLogger().debug( |
| 59 | + "Browser live reload not available, skipping public resources watcher"); |
| 60 | + return; |
| 61 | + } |
| 62 | + BrowserLiveReload liveReload = liveReloadOpt.get(); |
| 63 | + watcher = new FileWatcher(changed -> { |
| 64 | + try { |
| 65 | + if (changed.isFile() && changed.getName().endsWith(".css")) { |
| 66 | + // Path to be used from the browser: "/" + relative path |
| 67 | + String rel = resourcesFolder.toPath() |
| 68 | + .relativize(changed.toPath()).toString() |
| 69 | + .replace(File.separatorChar, '/'); |
| 70 | + String browserPath = "/" + rel; |
| 71 | + String contents = Files.readString(changed.toPath(), |
| 72 | + StandardCharsets.UTF_8); |
| 73 | + liveReload.update(browserPath, contents); |
| 74 | + } |
| 75 | + } catch (Exception e) { |
| 76 | + getLogger().error( |
| 77 | + "Unable to perform hot update of public resource CSS " |
| 78 | + + changed, |
| 79 | + e); |
| 80 | + try { |
| 81 | + liveReload.reload(); |
| 82 | + } catch (Exception ignore) { |
| 83 | + // no-op |
| 84 | + } |
| 85 | + } |
| 86 | + }, resourcesFolder); |
| 87 | + watcher.start(); |
| 88 | + getLogger().debug("Watching {} for public CSS changes", |
| 89 | + resourcesFolder); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void close() throws IOException { |
| 94 | + if (watcher != null) { |
| 95 | + watcher.stop(); |
| 96 | + watcher = null; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private Logger getLogger() { |
| 101 | + return LoggerFactory.getLogger(getClass()); |
| 102 | + } |
| 103 | +} |
0 commit comments