From 8d1653386744ebb3e83466ae5d261fefbdfe5c8e Mon Sep 17 00:00:00 2001 From: Ewan Breakey Date: Tue, 17 May 2022 15:28:58 +1000 Subject: [PATCH 1/9] Fix error logging and adjust package exports --- helpers/package.json | 4 ++++ index.ts | 4 ++-- yarn.lock | 7 +++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/helpers/package.json b/helpers/package.json index ca5392b..34b433d 100644 --- a/helpers/package.json +++ b/helpers/package.json @@ -12,6 +12,10 @@ "url": "https://github.com/stevent-team/epoxy.git", "directory": "helpers" }, + "exports": { + ".": "./dist/index.js", + "./helpers": "./helpers/dist/index.js" + }, "scripts": { "build": "parcel build" }, diff --git a/index.ts b/index.ts index d9ef105..15326be 100644 --- a/index.ts +++ b/index.ts @@ -10,8 +10,8 @@ const serve = async ({ target, routeFile, host, port, index }) => { let routes: Routes = {} if (routeFile) { const resolvedRoutes = path.resolve(__dirname, routeFile) - routes = await loadRoutes(resolvedRoutes).catch(() => { - throw new Error(`Failed to load routes file: ${resolvedRoutes}`) + routes = await loadRoutes(resolvedRoutes).catch(e => { + throw new Error(`Failed to load routes file "${resolvedRoutes}": ${e}`) }) } diff --git a/yarn.lock b/yarn.lock index c2c6dfc..63c360f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -656,6 +656,13 @@ chrome-trace-event "^1.0.2" nullthrows "^1.1.1" +"@stevent-team/epoxy@file:.": + version "1.0.0" + dependencies: + cors "^2.8.5" + express "^4.18.1" + yargs "^17.5.1" + "@swc/helpers@^0.3.6": version "0.3.13" resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.3.13.tgz#b9af856aaa3804fefdd1544632dde35b7b6ff978" From 36d67c069c3fbfa9bb264210b4ee972f2ee1531c Mon Sep 17 00:00:00 2001 From: Ewan Breakey Date: Tue, 17 May 2022 15:45:11 +1000 Subject: [PATCH 2/9] Fix package resolution --- helpers/package.json | 4 ---- package.json | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/helpers/package.json b/helpers/package.json index 34b433d..ca5392b 100644 --- a/helpers/package.json +++ b/helpers/package.json @@ -12,10 +12,6 @@ "url": "https://github.com/stevent-team/epoxy.git", "directory": "helpers" }, - "exports": { - ".": "./dist/index.js", - "./helpers": "./helpers/dist/index.js" - }, "scripts": { "build": "parcel build" }, diff --git a/package.json b/package.json index 2f8c254..efd8a53 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,10 @@ "engines": { "node": ">=10" }, + "exports": { + ".": "./dist/index.js", + "./helpers": "./helpers/dist/index.js" + }, "scripts": { "build": "parcel build" }, From 5fdae4e32913da0237ab2d73befd2497956598f3 Mon Sep 17 00:00:00 2001 From: Ewan Breakey Date: Tue, 17 May 2022 15:49:37 +1000 Subject: [PATCH 3/9] Publish --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e6314f9..3098939 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stevent-team/epoxy", - "version": "1.0.1", + "version": "1.0.2", "description": "Lightweight server-side per-route html injection", "keywords": [ "epoxy", From 6f2a43b286634fd3289b71f8fbc4b464d376b3e0 Mon Sep 17 00:00:00 2001 From: Ewan Breakey Date: Tue, 17 May 2022 15:52:56 +1000 Subject: [PATCH 4/9] v1.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3098939..ed5a815 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stevent-team/epoxy", - "version": "1.0.2", + "version": "1.0.3", "description": "Lightweight server-side per-route html injection", "keywords": [ "epoxy", From 57657dfbe23776e83bbb35988bd4d13431ce2799 Mon Sep 17 00:00:00 2001 From: Ewan Breakey Date: Tue, 17 May 2022 15:54:17 +1000 Subject: [PATCH 5/9] v1.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ed5a815..9e8fb5b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stevent-team/epoxy", - "version": "1.0.3", + "version": "1.0.4", "description": "Lightweight server-side per-route html injection", "keywords": [ "epoxy", From fecba4b933d9a42f5ce7ad1ae0103a4713c81b90 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Wed, 18 May 2022 11:36:08 +1000 Subject: [PATCH 6/9] Load module from module --- src/loadRoutes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/loadRoutes.ts b/src/loadRoutes.ts index 2960d46..8077be3 100644 --- a/src/loadRoutes.ts +++ b/src/loadRoutes.ts @@ -1,7 +1,7 @@ // Dynamically import the routes file const loadRoutes = async (routeFile: string) => { const module = await import(routeFile) - return module?.default + return module?.default?.default ?? module?.default } export default loadRoutes From 8da6f923306547a95d9949410a86ca8709ac7696 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Wed, 18 May 2022 13:39:13 +1000 Subject: [PATCH 7/9] Cut out null meta fields --- helpers/src/createMeta.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/src/createMeta.ts b/helpers/src/createMeta.ts index 4bdfbb3..de72f89 100644 --- a/helpers/src/createMeta.ts +++ b/helpers/src/createMeta.ts @@ -5,7 +5,7 @@ type MetaTags = { const createMeta = (metaTags: MetaTags): string => Object.entries(metaTags) .reduce((html, [property, content]) => - `${html}`, + (property && content) ? `${html}` : html, '' ) From a291a250bdeff9fa471c4d21756c8f8f87ef4504 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Wed, 18 May 2022 13:43:39 +1000 Subject: [PATCH 8/9] Compile routes file from Epoxy --- README.md | 8 ++++---- package.json | 4 +++- routes/README.md | 1 + src/loadRoutes.ts | 33 +++++++++++++++++++++++++++++++-- yarn.lock | 17 +++++------------ 5 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 routes/README.md diff --git a/README.md b/README.md index 5de687f..4b91434 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Options: ## Example -Create a script to define your dynamic routes. *(requires .mjs extension)* +Create a script to define your dynamic routes. ```js // Handlers are async functions returning HTML to be injected @@ -84,7 +84,7 @@ export default { Then serve your static directory and dynamic routes! ```bash -epoxy ./dist ./routes.mjs +epoxy ./dist ./routes.js ``` or setup an npm script @@ -93,14 +93,14 @@ or setup an npm script // package.json { "scripts": { - "serve": "epoxy ./dist ./routes.mjs" + "serve": "epoxy ./dist ./routes.js" } } ``` ## API -Your route handler must be a javascript module (i.e. have the file extension `mjs`) so that it can be imported and used by Epoxy. It also has to export a default object with the structure: +Your route handler will be built by Epoxy using Parcel when Epoxy is started. It also has to export a default object with the structure: ```js export default { diff --git a/package.json b/package.json index 9e8fb5b..16adfdd 100644 --- a/package.json +++ b/package.json @@ -29,12 +29,14 @@ "devDependencies": { "@types/express": "^4.17.13", "@types/node": "^17.0.33", - "parcel": "^2.5.0", "typescript": "^4.6.4" }, "dependencies": { + "@parcel/config-default": "^2.5.0", + "@parcel/core": "^2.5.0", "cors": "^2.8.5", "express": "^4.18.1", + "parcel": "^2.5.0", "yargs": "^17.5.1" } } diff --git a/routes/README.md b/routes/README.md new file mode 100644 index 0000000..005a4c4 --- /dev/null +++ b/routes/README.md @@ -0,0 +1 @@ +Your routes file will compiled into this directory when Epoxy is started diff --git a/src/loadRoutes.ts b/src/loadRoutes.ts index 8077be3..5ed8b1e 100644 --- a/src/loadRoutes.ts +++ b/src/loadRoutes.ts @@ -1,6 +1,35 @@ -// Dynamically import the routes file +import path from 'path' +import { Parcel } from '@parcel/core' + +// Compile the routes file, then import it const loadRoutes = async (routeFile: string) => { - const module = await import(routeFile) + const distDir = path.resolve(__dirname, '../routes/') + + // Setup bundler + const bundler = new Parcel({ + entries: routeFile, + defaultConfig: '@parcel/config-default', + mode: 'production', + shouldDisableCache: true, + targets: { + 'main': { + context: 'node', + distDir, + engines: { + node: ">=10" + }, + scopeHoist: false, + optimize: false, + } + } + }) + + // Build + const { buildTime } = await bundler.run() + console.log(`✨ Built routes file in ${buildTime}ms`) + + // Import + const module = await import(path.resolve(distDir, path.basename(routeFile))) return module?.default?.default ?? module?.default } diff --git a/yarn.lock b/yarn.lock index 63c360f..e8229f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15,9 +15,9 @@ integrity sha512-hsEnFemeiW4D08A5gUAZxLBTXpZ39P+a+DGDsHw1yxqyQ/jzFEnxf5uTEGp+3bzAbNOxU1paTgYS4ECU/IgfDw== "@babel/highlight@^7.16.7": - version "7.17.9" - resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.17.9.tgz#61b2ee7f32ea0454612def4fccdae0de232b73e3" - integrity sha512-J9PfEKCbFIv2X5bjTMiZu6Vf341N05QIY+d6FvVKynkG1S7G0j3I0QoRtWIrXhZ+/Nlb5Q0MzqL7TokEJ5BNHg== + version "7.17.12" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.17.12.tgz#257de56ee5afbd20451ac0a75686b6b404257351" + integrity sha512-7yykMVF3hfZY2jsHZEEgLc+3x4o1O+fYyULu11GynEUQNwB6lua+IIQn1FiJxNucd5UlyJryrwsOh8PL9Sn8Qg== dependencies: "@babel/helper-validator-identifier" "^7.16.7" chalk "^2.0.0" @@ -79,7 +79,7 @@ dependencies: "@parcel/plugin" "2.5.0" -"@parcel/config-default@2.5.0": +"@parcel/config-default@2.5.0", "@parcel/config-default@^2.5.0": version "2.5.0" resolved "https://registry.yarnpkg.com/@parcel/config-default/-/config-default-2.5.0.tgz#31caa12f6d37f3ae1df68e639dc276039f927603" integrity sha512-r30V61958SONvP9I8KV8s44ZOFq0H219VyFjPysraSabHjZ+KMaCTQOuqaDtUMa272sHUQkBcZxKYj5jYPJlZg== @@ -115,7 +115,7 @@ "@parcel/transformer-react-refresh-wrap" "2.5.0" "@parcel/transformer-svg" "2.5.0" -"@parcel/core@2.5.0": +"@parcel/core@2.5.0", "@parcel/core@^2.5.0": version "2.5.0" resolved "https://registry.yarnpkg.com/@parcel/core/-/core-2.5.0.tgz#13f60be9124a6a3e33aff32715acfc5ebade9dd2" integrity sha512-dygDmPsfAYJKTnUftcbEzjCik7AAaPbFvJW8ETYz8diyjkAG9y6hvCAZIrJE5pNOjFzg32en4v4UWv8Sqlzl9g== @@ -656,13 +656,6 @@ chrome-trace-event "^1.0.2" nullthrows "^1.1.1" -"@stevent-team/epoxy@file:.": - version "1.0.0" - dependencies: - cors "^2.8.5" - express "^4.18.1" - yargs "^17.5.1" - "@swc/helpers@^0.3.6": version "0.3.13" resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.3.13.tgz#b9af856aaa3804fefdd1544632dde35b7b6ff978" From 4bec7950bbfdee4585261eb5744555b987664fd0 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Wed, 18 May 2022 13:48:40 +1000 Subject: [PATCH 9/9] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 16adfdd..e935461 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stevent-team/epoxy", - "version": "1.0.4", + "version": "1.1.0", "description": "Lightweight server-side per-route html injection", "keywords": [ "epoxy",