diff --git a/frontend/package.json b/frontend/package.json index 58c623f..e00ff3d 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -18,6 +18,8 @@ "bare-client-custom": "file:../bare-client-custom", "client": "workspace:*", "corium": "file:../corium", + "esbuild": "^0.19.1", + "esbuild-plugin-inline-import": "^1.0.1", "firebase": "^10.1.0", "firebase-config": "workspace:*", "protocol": "workspace:*", diff --git a/frontend/public/sw.js b/frontend/public/sw.js deleted file mode 100644 index 19f2f48..0000000 --- a/frontend/public/sw.js +++ /dev/null @@ -1,14 +0,0 @@ -/*global UVServiceWorker,__uv$config*/ -/* - * Stock service worker script. - * Users can provide their own sw.js if they need to extend the functionality of the service worker. - * Ideally, this will be registered under the scope in uv.config.js so it will not need to be modified. - * However, if a user changes the location of uv.bundle.js/uv.config.js or sw.js is not relative to them, they will need to modify this script locally. - */ -importScripts('uv/uv.bundle.js'); -importScripts('uv.config.js'); -importScripts(__uv$config.sw || 'uv.sw.js'); - -const sw = new UVServiceWorker(); - -self.addEventListener('fetch', (event) => event.respondWith(sw.fetch(event))); diff --git a/frontend/sw.js b/frontend/sw.js new file mode 100644 index 0000000..97abe36 --- /dev/null +++ b/frontend/sw.js @@ -0,0 +1,45 @@ +/*global UVServiceWorker,__uv$config*/ +/* + * Stock service worker script. + * Users can provide their own sw.js if they need to extend the functionality of the service worker. + * Ideally, this will be registered under the scope in uv.config.js so it will not need to be modified. + * However, if a user changes the location of uv.bundle.js/uv.config.js or sw.js is not relative to them, they will need to modify this script locally. + */ + +import index from "sw-filemap" + +let filemap = JSON.parse(index); + +importScripts('uv/uv.bundle.js'); +importScripts('uv.config.js'); +importScripts(__uv$config.sw || 'uv.sw.js'); + + +const sw = new UVServiceWorker(); + +self.addEventListener('fetch', (event) => { + let url = new URL(event.request.url).pathname; + // console.log(url); + if (url == "/") + url = "/index.html"; + if (filemap[url]) { + + let contenttype = "text/plain"; + + if (url.includes(".js")) + contenttype = "application/javascript"; + else if (url.includes(".html")) + contenttype = "text/html"; + else if (url.includes(".css")) + contenttype = "text/css"; + + + event.respondWith(new Response(filemap[url], { + headers: { + "content-type": contenttype + } + })); + } else { + event.respondWith(sw.fetch(event)) + } +}); diff --git a/frontend/sw_transform.js b/frontend/sw_transform.js new file mode 100644 index 0000000..9a24880 --- /dev/null +++ b/frontend/sw_transform.js @@ -0,0 +1,75 @@ +import { build } from 'esbuild'; +import inlineImportPlugin from 'esbuild-plugin-inline-import'; +import path from "path"; +import fs from "fs/promises"; + +const transform = options => { + const { filter, namespace, transform } = Object.assign( + { + filter: /sw-filemap/, + + + namespace: '_' + Math.random().toString(36).substr(2, 9), + + transform: async (contents, args) => contents + }, + options + ); + + return { + name: 'esbuild-sw-transformer', + setup(build) { + build.onResolve({ filter }, args => { + const realPath = args.path.replace(filter, ''); + return { + path: path.resolve(args.resolveDir, realPath), + namespace + }; + }); + + build.onLoad({ filter: /.*/, namespace }, async args => { + let map = { + + }; + + + async function transformDir(dir) { + let entries = await fs.readdir(dir); + for (let entry of entries) { + console.log(entry); + + if ((await fs.lstat(`${dir}/${entry}`)).isDirectory()) { + await transformDir(`${dir}/${entry}`); + } else { + map[`${dir}/${entry}`.replace("./", "/")] = (await fs.readFile(`${dir}/${entry}`)).toString(); + } + } + } + + await transformDir("."); + + // + // console.log(map); + // let contents = await fs.readFile(args.path, 'utf8'); + + // if (typeof transform === 'function') { + // contents = await transform(contents, args); + // } + + return { + contents: JSON.stringify(map), + loader: 'text' + }; + }); + } + }; +}; +build({ + entryPoints: ['../sw.js'], + bundle: true, + outfile: 'sw.js', + plugins: [ + // Always include this plugin before others + transform() + ] +}).catch(() => process.exit(1)) \ No newline at end of file diff --git a/frontend/thin-wrapper.html b/frontend/thin-wrapper.html new file mode 100644 index 0000000..c4c96c9 --- /dev/null +++ b/frontend/thin-wrapper.html @@ -0,0 +1,33 @@ + + + + + + + Homework + + + + + +