Skip to content

Commit

Permalink
fix: build the service worker without writing it to FS (#14182)
Browse files Browse the repository at this point in the history
Prevents restart of application during startup.

Fixes #13123
Fixes #14022

Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
  • Loading branch information
mcollovati and vursen committed Jul 27, 2022
1 parent e937623 commit d391029
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 47 deletions.
106 changes: 62 additions & 44 deletions flow-server/src/main/resources/vite.generated.ts
Expand Up @@ -53,8 +53,50 @@ const hasExportedWebComponents = existsSync(path.resolve(frontendFolder, 'web-co
console.trace = () => {};
console.debug = () => {};

function buildSWPlugin(): PluginOption {
function buildSWPlugin(opts): PluginOption {
let config: ResolvedConfig;
const devMode = opts.devMode;

const swObj = {}

async function build(action: 'generate' | 'write') {
const includedPluginNames = [
'alias',
'vite:resolve',
'vite:esbuild',
'rollup-plugin-dynamic-import-variables',
'vite:esbuild-transpile',
'vite:terser',
]
const plugins: rollup.Plugin[] = config.plugins.filter((p) => {
return includedPluginNames.includes(p.name)
});
plugins.push(
replace({
values: {
'process.env.NODE_ENV': JSON.stringify(config.mode),
...config.define,
},
preventAssignment: true
})
);
const bundle = await rollup.rollup({
input: path.resolve(settings.clientServiceWorkerSource),
plugins
});

try {
return await bundle[action]({
file: path.resolve(frontendBundleFolder, 'sw.js'),
format: 'es',
exports: 'none',
sourcemap: config.command === 'serve' || config.build.sourcemap,
inlineDynamicImports: true,
});
} finally {
await bundle.close();
}
}

return {
name: 'vaadin:build-sw',
Expand All @@ -63,49 +105,25 @@ function buildSWPlugin(): PluginOption {
config = resolvedConfig;
},
async buildStart() {
const includedPluginNames = [
'alias',
'vite:resolve',
'vite:esbuild',
'rollup-plugin-dynamic-import-variables',
'vite:esbuild-transpile',
'vite:terser'
];
const rollupPlugins: rollup.Plugin[] = config.plugins.filter((p) => {
return includedPluginNames.includes(p.name);
});
rollupPlugins.push(
replace({
values: {
'process.env.NODE_ENV': JSON.stringify(config.mode),
...config.define
},
preventAssignment: true
})
);

const rollupOutput: rollup.OutputOptions = {
file: path.resolve(frontendBundleFolder, 'sw.js'),
format: 'es',
exports: 'none',
sourcemap: config.command === 'serve' || config.build.sourcemap,
inlineDynamicImports: true
};

const rollupConfig: rollup.RollupOptions = {
input: path.resolve(settings.clientServiceWorkerSource),
output: rollupOutput,
plugins: rollupPlugins
};

const bundle = await rollup.rollup(rollupConfig);
try {
await bundle.write(rollupOutput);
} finally {
await bundle.close();
if (devMode) {
const { output } = await build('generate');
swObj.code = output[0].code;
swObj.map = output[0].map;
} else {
await build('write');
}
}
};
},
async load(id) {
if (id.endsWith('sw.js')) {
return '';
}
},
async transform(_code, id) {
if (id.endsWith('sw.js')) {
return swObj;
}
},
}
}

function injectManifestToSWPlugin(): PluginOption {
Expand Down Expand Up @@ -448,7 +466,7 @@ export const vaadinConfig: UserConfigFn = (env) => {
!devMode && brotli(),
devMode && vaadinBundlesPlugin(),
devMode && setHmrPortToServerPort(),
settings.offlineEnabled && buildSWPlugin(),
settings.offlineEnabled && buildSWPlugin({ devMode }),
settings.offlineEnabled && injectManifestToSWPlugin(),
!devMode && statsExtracterPlugin(),
themePlugin({devMode}),
Expand Down
Expand Up @@ -24,6 +24,9 @@
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.server.InitParameters;
import com.vaadin.flow.server.VaadinServletContext;
Expand All @@ -32,11 +35,9 @@

import static com.vaadin.flow.server.Constants.VAADIN_MAPPING;
import static com.vaadin.flow.server.frontend.FrontendUtils.INDEX_HTML;
import static com.vaadin.flow.server.frontend.FrontendUtils.SERVICE_WORKER_SRC_JS;
import static com.vaadin.flow.server.frontend.FrontendUtils.WEB_COMPONENT_HTML;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Handles communication with a Vite server.
* <p>
Expand Down Expand Up @@ -142,6 +143,11 @@ public HttpURLConnection prepareConnection(String path, String method)
+ VAADIN_MAPPING + WEB_COMPONENT_HTML, method);
}

if (path.equals("/" + SERVICE_WORKER_SRC_JS)) {
return super.prepareConnection(getContextPath() + "/"
+ VAADIN_MAPPING + SERVICE_WORKER_SRC_JS, method);
}

return super.prepareConnection(getContextPath() + path, method);
}

Expand Down

0 comments on commit d391029

Please sign in to comment.