Skip to content

Commit

Permalink
Merge pull request #4 from supercharge/esm
Browse files Browse the repository at this point in the history
Move to ESM
  • Loading branch information
marcuspoehls authored Dec 3, 2023
2 parents 89bc045 + a52497e commit e3b4e57
Show file tree
Hide file tree
Showing 26 changed files with 795 additions and 754 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
name: Run tests

on: [push, pull_request]
on:
push:
paths-ignore:
- 'README.md'

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
# the group name is composed of two elements:
# 1. this workflow name "run-tests"
# 2. the branch name retrieved via the "github.ref" variable
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
Expand All @@ -12,23 +18,23 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x]
node-version: [20.x, latest]

name: Node ${{ matrix.node-version }}

steps:
- name: Git checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Run tests
run: npm run test:full
run: npm test
env:
CI: true
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Changelog

## [1.2.2](https://github.com/supercharge/inertia/compare/v1.2.0...v1.2.1) - 2022-09-26
## [2.0.0](https://github.com/supercharge/inertia/compare/v1.2.2...v2.0.0) - 2023-xx-xx

### Updated
- bump dependencies

### Breaking Changes
- require Node.js v20
- this package migrated to ESM
- renamed `InertiaOptions` to `InertiaConfig`
- renaming the Inertia config file to `InertiaConfig` aligns with the Supercharge config naming where all exported config contracts have the `Config` suffix. And the `config/inertia.ts` file location aligns with the `InertiaConfig` naming, too.


## [1.2.2](https://github.com/supercharge/inertia/compare/v1.2.1...v1.2.2) - 2022-09-26

### Updated
- use `^` for Supercharge framework dependency versions to avoid release issues because of mismatching versions
Expand Down
49 changes: 0 additions & 49 deletions bin/test.js

This file was deleted.

8 changes: 4 additions & 4 deletions config/inertia.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

import { App } from '@supercharge/facades'
import { Application } from '@supercharge/contracts'
import { Inertia, InertiaOptions } from '@supercharge/inertia'
import { Inertia, InertiaConfig } from '@supercharge/inertia'

const inertiaConfig: InertiaOptions = {
const inertiaConfig: InertiaConfig = {
/**
* Defines the root template view that will be loaded on the first page visit.
* This root view template should be provided in the resources directory of
Expand Down Expand Up @@ -46,7 +46,7 @@ const inertiaConfig: InertiaOptions = {
* render function is required for Inertia to create the rendered HTML.
* Your file may use a default export or a named "render" export.
*/
resolveRenderFunctionFrom: 'bootstrap/ssr.js'
resolveRenderFunctionFrom: App.resolveFromBasePath('bootstrap/ssr/ssr.js')
}
}

Expand Down
52 changes: 27 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,38 @@
"url": "https://github.com/supercharge/inertia/issues"
},
"dependencies": {
"@supercharge/contracts": "^3.9.0",
"@supercharge/contracts": "~4.0.0-alpha.1",
"@supercharge/fs": "~3.4.0",
"@supercharge/goodies": "~1.11.1",
"@supercharge/support": "^3.9.0",
"dedent": "~0.7.0"
"@supercharge/goodies": "~2.0.0",
"@supercharge/support": "~4.0.0-alpha.1",
"dedent": "~1.5.1"
},
"devDependencies": {
"@japa/run-failed-tests": "~1.1.0",
"@japa/runner": "~2.2.1",
"@japa/spec-reporter": "~1.3.1",
"@supercharge/core": "~3.9.0",
"@supercharge/eslint-config-typescript": "~2.3.1",
"@supercharge/http": "~3.9.0",
"@supercharge/tsconfig": "~4.0.0",
"@supercharge/view": "~3.9.0",
"@types/dedent": "~0.7.0",
"c8": "~7.12.0",
"eslint": "~8.24.0",
"expect": "~29.0.3",
"supertest": "~6.2.4",
"typescript": "~4.8.3"
"@supercharge/core": "~4.0.0-alpha.1",
"@supercharge/eslint-config-typescript": "~4.0.1",
"@supercharge/http": "~4.0.0-alpha.1",
"@supercharge/tsconfig": "~7.0.0",
"@supercharge/view": "~4.0.0-alpha.1",
"@types/dedent": "~0.7.2",
"c8": "~8.0.1",
"eslint": "~8.55.0",
"expect": "~29.7.0",
"supertest": "~6.3.3",
"typescript": "~5.3.2",
"uvu": "~0.5.6"
},
"engines": {
"node": ">=16"
"node": ">=20"
},
"files": [
"dist"
],
"type": "module",
"main": "dist/index.js",
"types": "dist",
"exports": {
".": "./dist/index.js"
},
"homepage": "https://github.com/supercharge/inertia",
"keywords": [
"inertia",
Expand All @@ -45,7 +49,6 @@
"superchargejs"
],
"license": "MIT",
"main": "dist",
"publishConfig": {
"access": "public"
},
Expand All @@ -59,9 +62,8 @@
"lint": "eslint src",
"lint:fix": "npm run lint -- --fix",
"posttest": "c8 report --reporter=html",
"test": "npm run build && npm run test:run",
"test:full": "npm run build && npm run lint && npm run test:run",
"test:run": "c8 node bin/test.js"
},
"types": "dist"
"test": "npm run build && npm run lint && npm run test:coverage",
"test:coverage": "c8 --include=dist npm run test:run",
"test:run": "uvu --ignore helpers --ignore fixtures"
}
}
16 changes: 8 additions & 8 deletions src/contracts/config-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import { Application } from '@supercharge/contracts'

/**
* Defines the types for the Inertia version configuration.
*/
export type InertiaVersion = InertiaVersionValue | InertiaVersionFunction
export type InertiaVersionValue = string | number | undefined
export type InertiaVersionFunction = (app: Application) => Promise<InertiaVersionValue> | InertiaVersionValue

/**
* Defines the Inertia configuration.
*/
export interface InertiaOptions {
export interface InertiaConfig {
/**
* Defines the root template view that will be loaded on the first page visit.
* This root view template should be provided in the resources directory of
Expand Down Expand Up @@ -47,10 +54,3 @@ export interface InertiaOptions {
resolveRenderFunctionFrom?: string
}
}

/**
* Defines the types for the Inertia version configuration.
*/
export type InertiaVersion = InertiaVersionValue | InertiaVersionFunction
export type InertiaVersionValue = string | number | undefined
export type InertiaVersionFunction = (app: Application) => Promise<InertiaVersionValue> | InertiaVersionValue
4 changes: 2 additions & 2 deletions src/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'

export * from './page-contract'
export * from './config-contract'
export * from './page-contract.js'
export * from './config-contract.js'
4 changes: 2 additions & 2 deletions src/contracts/page-contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import { InertiaOptions } from './config-contract'
import { InertiaConfig } from './config-contract.js'

/**
* Defines the Inertia page contract.
Expand All @@ -9,5 +9,5 @@ export interface PageContract {
component: string
props: Record<string, unknown>
url?: string
version?: InertiaOptions['version']
version?: InertiaConfig['version']
}
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

export * from './contracts'
export * from './inertia'
export * from './inertia-middleware'
export * from './inertia-request'
export * from './inertia-response'
export * from './inertia-service-provider'
export * from './contracts/index.js'
export * from './inertia.js'
export * from './inertia-middleware.js'
export * from './inertia-request.js'
export * from './inertia-response.js'
export * from './inertia-service-provider.js'
2 changes: 1 addition & 1 deletion src/inertia-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import { SharesData } from './shares-data'
import { SharesData } from './shares-data.js'

export class InertiaRequest extends SharesData {
/**
Expand Down
12 changes: 6 additions & 6 deletions src/inertia-response.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

import Os from 'node:os'
import { SharesData } from './shares-data'
import { resolveRenderFunctionFrom } from './utils'
import { SharesData } from './shares-data.js'
import { isAsyncFunction } from '@supercharge/goodies'
import { resolveRenderFunctionFrom } from './utils.js'
import { Application, HttpContext, HttpResponse } from '@supercharge/contracts'
import { InertiaOptions, InertiaVersionValue, PageContract } from './contracts'
import { InertiaConfig, InertiaVersionValue, PageContract } from './contracts/index.js'

export class InertiaResponse extends SharesData {
/**
Expand All @@ -21,12 +21,12 @@ export class InertiaResponse extends SharesData {
/**
* Stores the reference to the Inertia configuration.
*/
private readonly config: InertiaOptions
private readonly config: InertiaConfig

/**
* Create a new instance.
*/
constructor (app: Application, { request, response }: HttpContext, config: InertiaOptions) {
constructor (app: Application, { request, response }: HttpContext, config: InertiaConfig) {
super(request)

this.app = app
Expand Down Expand Up @@ -220,7 +220,7 @@ export class InertiaResponse extends SharesData {
* a "render" function either via a "default" export a named "render" export.
*/
protected async renderSsrPage (page: PageContract): Promise<{ head: string[], body: string }> {
const render = resolveRenderFunctionFrom(this.config.ssr!.resolveRenderFunctionFrom!)
const render = await resolveRenderFunctionFrom(this.config.ssr!.resolveRenderFunctionFrom!)

return render(page)
}
Expand Down
26 changes: 15 additions & 11 deletions src/inertia-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import Fs from 'node:fs'
import dedent from 'dedent'
import { InertiaOptions } from './contracts'
import { InertiaRequest } from './inertia-request'
import { resolveRenderFunctionFrom } from './utils'
import { InertiaResponse } from './inertia-response'
import { InertiaConfig } from './contracts/index.js'
import { InertiaRequest } from './inertia-request.js'
import { ServiceProvider } from '@supercharge/support'
import { resolveRenderFunctionFrom } from './utils.js'
import { InertiaResponse } from './inertia-response.js'
import { Application, HttpRequest, HttpRequestCtor, HttpResponse, HttpResponseCtor, ViewEngine } from '@supercharge/contracts'

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ export class InertiaServiceProvider extends ServiceProvider {
override async boot (): Promise<void> {
this.registerInertiaPartialViews()
this.registerInertiaRequestMacros()
this.registerInertiaResponseMacros()
await this.registerInertiaResponseMacros()
}

/**
Expand Down Expand Up @@ -105,15 +105,19 @@ export class InertiaServiceProvider extends ServiceProvider {
/**
* Register the Inertia resposne macros.
*/
protected registerInertiaResponseMacros (): void {
protected async registerInertiaResponseMacros (): Promise<void> {
const app = this.app().make<Application>('app')
const Response = this.app().make<HttpResponseCtor>('response')
const inertiaConfig = app.config().get<InertiaOptions>('inertia', { view: 'app', ssr: { enabled: false } })
const inertiaConfig = app.config().get<InertiaConfig>('inertia', {
view: 'app',
ssr: { enabled: false }
})

if (inertiaConfig.ssr?.enabled) {
this.ensureSsrRenderFunction(inertiaConfig)
await this.ensureSsrRenderFunction(inertiaConfig)
}

const Response = this.app().make<HttpResponseCtor>('response')

Response.macro('inertia', function (this: HttpResponse) {
return new InertiaResponse(app, this.ctx(), inertiaConfig)
})
Expand All @@ -122,7 +126,7 @@ export class InertiaServiceProvider extends ServiceProvider {
/**
* Ensure the configured SSR render function is available.
*/
protected ensureSsrRenderFunction (inertiaConfig: InertiaOptions): void {
protected async ensureSsrRenderFunction (inertiaConfig: InertiaConfig): Promise<void> {
const renderFunctionPath = inertiaConfig.ssr?.resolveRenderFunctionFrom

if (!renderFunctionPath) {
Expand All @@ -133,6 +137,6 @@ export class InertiaServiceProvider extends ServiceProvider {
throw new Error(`Inertia SSR is enabled but we cannot resolve the file at "${renderFunctionPath}".`)
}

resolveRenderFunctionFrom(renderFunctionPath)
await resolveRenderFunctionFrom(renderFunctionPath)
}
}
Loading

0 comments on commit e3b4e57

Please sign in to comment.