Skip to content

Commit

Permalink
require node 20 and migrate to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspoehls committed Nov 18, 2023
1 parent cb56ff5 commit f174e12
Show file tree
Hide file tree
Showing 22 changed files with 755 additions and 730 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# 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


## [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.

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": "^3.20.4",
"@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": "^3.20.4",
"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": "~3.20.4",
"@supercharge/eslint-config-typescript": "~4.0.0",
"@supercharge/http": "~3.20.4",
"@supercharge/tsconfig": "~7.0.0",
"@supercharge/view": "~3.20.4",
"@types/dedent": "~0.7.2",
"c8": "~8.0.1",
"eslint": "~8.54.0",
"expect": "~29.7.0",
"supertest": "~6.3.3",
"typescript": "~5.2.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"
}
}
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'
2 changes: 1 addition & 1 deletion 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 { InertiaOptions } from './config-contract.js'

/**
* Defines the Inertia page contract.
Expand Down
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
8 changes: 4 additions & 4 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 { InertiaOptions, InertiaVersionValue, PageContract } from './contracts/index.js'

export class InertiaResponse extends SharesData {
/**
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 { InertiaOptions } 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<InertiaOptions>('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: InertiaOptions): 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)
}
}
7 changes: 3 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

import { esmRequire } from '@supercharge/goodies'
import { resolveDefaultImport } from '@supercharge/goodies'

type RenderFunction = Function | { render: Function }

/**
* Returns the SSR "render" function from the given `path`.
*/
export function resolveRenderFunctionFrom (renderFunctionPath: string): Function {
// TODO: cache require() calls
const renderFn = esmRequire<RenderFunction>(renderFunctionPath)
export async function resolveRenderFunctionFrom (renderFunctionPath: string): Promise<Function> {
const renderFn = await resolveDefaultImport<{ default: RenderFunction | undefined }>(renderFunctionPath)

if (typeof renderFn === 'function') {
return renderFn
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/ssr.default-export-commonjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

const render = require('./ssr.named-export-commonjs.cjs')

module.exports = render
11 changes: 8 additions & 3 deletions test/fixtures/ssr.default-export.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict'

const render = require('./ssr.module-export')

exports.default = render
export default function render (page) {
return {
head: [
'<title>Supercharge Inertia SSR</title>'
],
body: `<h1>Hello Test SSR: ${page.props.name}</h1>`
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = function render (page) {
exports.render = function render (page) {
return {
head: [
'<title>Supercharge Inertia SSR</title>'
Expand Down
7 changes: 5 additions & 2 deletions test/fixtures/ssr.named-export.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

const render = require('./ssr.module-export')
import RenderFunction from './ssr.named-export-commonjs.cjs'

exports.render = render
export const render = RenderFunction

// this export default is required by ESM
export default render
5 changes: 4 additions & 1 deletion test/fixtures/ssr.string-export.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
'use strict'

exports.render = 'Should Fail'
export const render = 'Should Fail'

// this export default is required by ESM
export default render
29 changes: 19 additions & 10 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict'

const Path = require('path')
const { InertiaServiceProvider } = require('../../dist')
const { HttpServiceProvider } = require('@supercharge/http')
const { ViewServiceProvider } = require('@supercharge/view')
const { Application, ErrorHandler } = require('@supercharge/core')
import Path from 'node:path'
import { fileURLToPath } from 'node:url'
import { ViewServiceProvider } from '@supercharge/view'
import { InertiaServiceProvider } from '../../dist/index.js'
import { Application, ErrorHandler } from '@supercharge/core'
import { HttpServiceProvider, Server } from '@supercharge/http'

const __dirname = Path.dirname(fileURLToPath(import.meta.url))

/**
* Returns a test application.
Expand All @@ -13,7 +16,7 @@ const { Application, ErrorHandler } = require('@supercharge/core')
*
* @returns {Promise<Application>}
*/
async function createApp (inertiaConfig = {}) {
export async function createApp (inertiaConfig = {}) {
const app = Application
.createWithAppRoot(Path.resolve(__dirname))
.withErrorHandler(ErrorHandler)
Expand Down Expand Up @@ -52,7 +55,7 @@ async function createApp (inertiaConfig = {}) {
*
* @returns {Promise<Application>}
*/
async function createSsrApp (inertiaSsrConfig = {}) {
export async function createSsrApp (inertiaSsrConfig = {}) {
return await createApp({
ssr: {
enabled: true,
Expand All @@ -61,7 +64,13 @@ async function createSsrApp (inertiaSsrConfig = {}) {
})
}

module.exports = {
createApp,
createSsrApp
/**
* @param {Application} app
*/
export function createServer (app) {
return new Server(
app,
app.config().get('app', {}),
app.config().get('http', {})
)
}
Loading

0 comments on commit f174e12

Please sign in to comment.