Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support esm generation #16

Merged
merged 7 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ ns1.register({
Hyperschema.toDisk(schema)
```

If you want to generate as ESM, simply use `import` instead of `require` above or set the option explictly in `toDisk` like so

```js
Hyperschema.toDisk(schema, { esm: true })
```

`index.js` will contain generated `compact-encoding` definitions. You can then load/use them as follows:
```js
const c = require('compact-encoding')
Expand Down
15 changes: 11 additions & 4 deletions index.js → builder.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ module.exports = class Hyperschema {
this.initializing = false
}

static esm = false

_getFullyQualifiedName (description) {
if (description.namespace === null) return description.name
return '@' + description.namespace + '/' + description.name
Expand Down Expand Up @@ -344,19 +346,24 @@ module.exports = class Hyperschema {
return json
}

toCode () {
return generateCode(this)
toCode ({ esm = this.constructor.esm } = {}) {
return generateCode(this, { esm })
}

static toDisk (hyperschema, dir) {
static toDisk (hyperschema, dir, opts) {
if (typeof dir === 'object' && dir) {
opts = dir
dir = null
}

if (!dir) dir = hyperschema.dir
fs.mkdirSync(dir, { recursive: true })

const jsonPath = p.join(p.resolve(dir), JSON_FILE_NAME)
const codePath = p.join(p.resolve(dir), CODE_FILE_NAME)

fs.writeFileSync(jsonPath, JSON.stringify(hyperschema.toJSON(), null, 2), { encoding: 'utf-8' })
fs.writeFileSync(codePath, hyperschema.toCode(), { encoding: 'utf-8' })
fs.writeFileSync(codePath, hyperschema.toCode(opts), { encoding: 'utf-8' })
}

static from (json) {
Expand Down
7 changes: 7 additions & 0 deletions builder.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Hyperschema from './builder.cjs'

class ESMHyperschema extends Hyperschema {
static esm = true
}

export default ESMHyperschema
19 changes: 16 additions & 3 deletions lib/codegen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const gen = require('generate-object-property')
const s = require('generate-string')

module.exports = function generateSchema (hyperschema) {
module.exports = function generateSchema (hyperschema, { esm = false } = {}) {
const structs = []
const structsByName = new Map()

Expand Down Expand Up @@ -30,7 +30,13 @@ module.exports = function generateSchema (hyperschema) {
str += '/* eslint-disable quotes */\n'
str += '\n'
str += `const VERSION = ${hyperschema.version}\n`
str += 'const { c } = require(\'hyperschema/runtime\')\n'

if (esm) {
str += 'import { c } from \'hyperschema/runtime\')\n'
} else {
str += 'const { c } = require(\'hyperschema/runtime\')\n'
}

str += '\n'
str += '// eslint-disable-next-line no-unused-vars\n'
str += 'let version = VERSION\n'
Expand Down Expand Up @@ -102,7 +108,14 @@ module.exports = function generateSchema (hyperschema) {
str += ' }\n'
str += '}\n'
str += '\n'
str += 'module.exports = { resolveStruct: getStruct, getStruct, getEnum, getEncoding, encode, decode, setVersion, version }\n'
str += 'const resolveStruct = getStruct // compat\n'
str += '\n'

if (esm) {
str += 'export { resolveStruct, getStruct, getEnum, getEncoding, encode, decode, setVersion, version }\n'
} else {
str += 'module.exports = { resolveStruct, getStruct, getEnum, getEncoding, encode, decode, setVersion, version }\n'
}

return str

Expand Down
37 changes: 24 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@
"name": "hyperschema",
"version": "1.5.0",
"description": "Create registries of declarative compact-encoding schemas",
"main": "index.js",
"files": [
"lib/**",
"index.js",
"lib/*.js",
"builder.js",
"builder.cjs",
"runtime.cjs",
"runtime.js"
],
"exports": {
".": {
"import": "./builder.mjs",
"default": "./builder.cjs"
},
"./runtime": {
"import": "./runtime.mjs",
"default": "./runtime.cjs"
}
},
"imports": {
"fs": {
"bare": "bare-fs",
"default": "fs"
},
"path": {
"bare": "bare-path",
"default": "path"
}
},
"scripts": {
"test": "standard && brittle test/index.js",
"test:bare": "standard && bare test/index.js"
Expand All @@ -28,16 +49,6 @@
"generate-object-property": "^2.0.0",
"generate-string": "^1.0.1"
},
"imports": {
"fs": {
"bare": "bare-fs",
"default": "fs"
},
"path": {
"bare": "bare-path",
"default": "path"
}
},
"devDependencies": {
"brittle": "^3.7.0",
"standard": "^17.1.0",
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions runtime.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import compact from 'compact-encoding'

export const c = compact
6 changes: 3 additions & 3 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const p = require('path')
const fs = require('fs')
const tmp = require('test-tmp')

const Hyperschema = require('../..')
const Hyperschema = require('../../builder.cjs')

class TestBuilder {
constructor (dir, test) {
Expand Down Expand Up @@ -37,9 +37,9 @@ async function makeDir (t) {
const dir = await tmp(t, { dir: p.join(__dirname, '../test-storage') })

// Copy the runtime into the tmp dir so that we don't need to override it in the codegen
const runtimePath = p.join(dir, 'node_modules', 'hyperschema', 'runtime.js')
const runtimePath = p.join(dir, 'node_modules', 'hyperschema', 'runtime.cjs')
await fs.promises.mkdir(p.dirname(runtimePath), { recursive: true })
await fs.promises.copyFile(p.resolve(dir, '../../../runtime.js'), runtimePath)
await fs.promises.copyFile(p.resolve(dir, '../../../runtime.cjs'), runtimePath)
return dir
}

Expand Down
Loading