Skip to content

Commit

Permalink
Merge pull request #6 from stevent-team/fix/error-reporting
Browse files Browse the repository at this point in the history
Fix Error Reporting
  • Loading branch information
GRA0007 authored May 18, 2022
2 parents 8cd3f1e + 4bec795 commit 843f1b3
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion helpers/src/createMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type MetaTags = {
const createMeta = (metaTags: MetaTags): string =>
Object.entries(metaTags)
.reduce((html, [property, content]) =>
`${html}<meta property="${property}" content="${content}">`,
(property && content) ? `${html}<meta property="${property}" content="${content}">` : html,
''
)

Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const serve = async ({ target, routeFile, host, port, index }) => {
let routes: Routes = {}
if (routeFile) {
const resolvedRoutes = path.resolve(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}`)
})
}

Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stevent-team/epoxy",
"version": "1.0.1",
"version": "1.1.0",
"description": "Lightweight server-side per-route html injection",
"keywords": [
"epoxy",
Expand All @@ -19,18 +19,24 @@
"engines": {
"node": ">=10"
},
"exports": {
".": "./dist/index.js",
"./helpers": "./helpers/dist/index.js"
},
"scripts": {
"build": "parcel build"
},
"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"
}
}
1 change: 1 addition & 0 deletions routes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Your routes file will compiled into this directory when Epoxy is started
35 changes: 32 additions & 3 deletions src/loadRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
// 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)
return module?.default
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
}

export default loadRoutes
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -79,7 +79,7 @@
dependencies:
"@parcel/plugin" "2.5.0"

"@parcel/[email protected]":
"@parcel/[email protected]", "@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==
Expand Down Expand Up @@ -115,7 +115,7 @@
"@parcel/transformer-react-refresh-wrap" "2.5.0"
"@parcel/transformer-svg" "2.5.0"

"@parcel/[email protected]":
"@parcel/[email protected]", "@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==
Expand Down

0 comments on commit 843f1b3

Please sign in to comment.