Skip to content

Commit

Permalink
fix: Do not force Vite to use a single chunk (#12286)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed Nov 8, 2021
1 parent a84f90e commit 2165d2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
Expand Up @@ -35,8 +35,6 @@
import com.vaadin.flow.internal.BootstrapHandlerHelper;
import com.vaadin.flow.internal.BrowserLiveReload;
import com.vaadin.flow.internal.BrowserLiveReloadAccessor;
import com.vaadin.flow.internal.DevModeHandler;
import com.vaadin.flow.internal.DevModeHandlerManager;
import com.vaadin.flow.internal.UsageStatisticsExporter;
import com.vaadin.flow.internal.springcsrf.SpringCsrfTokenUtil;
import com.vaadin.flow.server.AppShellRegistry;
Expand Down Expand Up @@ -270,7 +268,7 @@ private static Document getIndexHtmlDocument(VaadinService service)
Document indexHtmlDocument = Jsoup.parse(index);
if (FeatureFlags.get(service.getContext())
.isEnabled(FeatureFlags.VITE)) {
modifyIndexHtmlForVite(service, indexHtmlDocument);
modifyIndexHtmlForVite(indexHtmlDocument);
}
return indexHtmlDocument;
}
Expand All @@ -292,22 +290,10 @@ private static Document getIndexHtmlDocument(VaadinService service)
throw new IOException(message);
}

private static void modifyIndexHtmlForVite(VaadinService service,
Document indexHtmlDocument) {
Optional<DevModeHandler> devModeHandler = DevModeHandlerManager
.getDevModeHandler(service);
if (devModeHandler.isPresent()) {
indexHtmlDocument.head()
.appendChild(new Element(SCRIPT).attr("type", "module")
.attr("src", Constants.VAADIN_MAPPING
+ FrontendUtils.GENERATED
+ FrontendUtils.BOOTSTRAP_FILE_NAME));

// Workaround for https://github.com/vitejs/vite/issues/5142
indexHtmlDocument.head().prepend(
"<script type='text/javascript'>window.JSCompiler_renameProperty = function(a) { return a;}</script>");

}
private static void modifyIndexHtmlForVite(Document indexHtmlDocument) {
// Workaround for https://github.com/vitejs/vite/issues/5142
indexHtmlDocument.head().prepend(
"<script type='text/javascript'>window.JSCompiler_renameProperty = function(a) { return a;}</script>");
}

// Holds parsed index.html to avoid re-parsing on every request in
Expand Down
24 changes: 16 additions & 8 deletions flow-server/src/main/resources/vite.generated.ts
Expand Up @@ -9,8 +9,7 @@ import * as net from 'net';

import { processThemeResources } from '@vaadin/application-theme-plugin/theme-handle.js';
import settings from '#settingsImport#';
import { UserConfigFn } from 'vite';
import { defineConfig } from 'vite';
import { UserConfigFn, defineConfig, HtmlTagDescriptor } from 'vite';

const frontendFolder = path.resolve(__dirname, settings.frontendFolder);
const themeFolder = path.resolve(frontendFolder, settings.themeFolder);
Expand Down Expand Up @@ -88,12 +87,7 @@ export const vaadinConfig: UserConfigFn = (env) => {
assetsDir: 'VAADIN/build',
rollupOptions: {
input: {
main: path.resolve(frontendFolder, 'index.html'),
generated: path.resolve(frontendFolder, 'generated/vaadin.ts')
},
output: {
// Produce only one chunk that gets imported into index.html
manualChunks: () => 'everything.js'
indexhtml: path.resolve(frontendFolder, 'index.html')
}
}
},
Expand All @@ -106,6 +100,20 @@ export const vaadinConfig: UserConfigFn = (env) => {
handleHotUpdate(context) {
updateTheme(path.resolve(context.file));
}
},
{
name: 'inject-entrypoint-script',
transformIndexHtml: {
enforce: 'pre',
transform() {
const vaadinScript: HtmlTagDescriptor = {
tag: 'script',
attrs: { type: 'module', src: './generated/vaadin.ts' },
injectTo: 'head'
};
return [vaadinScript];
}
}
}
]
};
Expand Down

0 comments on commit 2165d2c

Please sign in to comment.