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 #15

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Changes from 1 commit
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
Next Next commit
support esm generation
  • Loading branch information
mafintosh committed Jan 11, 2025
commit 39cf0c6eb29383e1b54fa2ba26b1143668239268
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -344,8 +344,8 @@ module.exports = class Hyperschema {
return json
}

toCode () {
return generateCode(this)
toCode (opts) {
return generateCode(this, opts)
}

static toDisk (hyperschema, dir) {
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()

@@ -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'
@@ -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

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -8,6 +8,13 @@
"index.js",
"runtime.js"
],
"exports": {
".": "./index.js",
"./runtime": {
"import": "./runtime.mjs",
"default": "./runtime.cjs"
}
},
"scripts": {
"test": "standard && brittle test/index.js",
"test:bare": "standard && bare test/index.js"
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
2 changes: 1 addition & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ async function makeDir (t) {
// 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')
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
}

Loading