From 39cf0c6eb29383e1b54fa2ba26b1143668239268 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Sat, 11 Jan 2025 12:13:07 +0100 Subject: [PATCH 1/4] support esm generation --- index.js | 4 ++-- lib/codegen.js | 19 ++++++++++++++++--- package.json | 7 +++++++ runtime.js => runtime.cjs | 0 runtime.mjs | 3 +++ test/helpers/index.js | 2 +- 6 files changed, 29 insertions(+), 6 deletions(-) rename runtime.js => runtime.cjs (100%) create mode 100644 runtime.mjs diff --git a/index.js b/index.js index 5738e21..69d16ee 100644 --- a/index.js +++ b/index.js @@ -344,8 +344,8 @@ module.exports = class Hyperschema { return json } - toCode () { - return generateCode(this) + toCode (opts) { + return generateCode(this, opts) } static toDisk (hyperschema, dir) { diff --git a/lib/codegen.js b/lib/codegen.js index 4092f48..15f6922 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -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 diff --git a/package.json b/package.json index cc99f76..7da57db 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/runtime.js b/runtime.cjs similarity index 100% rename from runtime.js rename to runtime.cjs diff --git a/runtime.mjs b/runtime.mjs new file mode 100644 index 0000000..4e646e7 --- /dev/null +++ b/runtime.mjs @@ -0,0 +1,3 @@ +import compact from 'compact-encoding' + +export const c = compact diff --git a/test/helpers/index.js b/test/helpers/index.js index f273f1e..0ef6afa 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -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 } From c8df530196983abaa0af08413f718b5c16a4884c Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Sat, 11 Jan 2025 12:21:29 +0100 Subject: [PATCH 2/4] esm generates esm and vice versa --- README.md | 6 ++++++ index.js => index.cjs | 15 +++++++++++---- index.mjs | 7 +++++++ package.json | 5 ++++- 4 files changed, 28 insertions(+), 5 deletions(-) rename index.js => index.cjs (97%) create mode 100644 index.mjs diff --git a/README.md b/README.md index 66c168c..39aab03 100644 --- a/README.md +++ b/README.md @@ -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') diff --git a/index.js b/index.cjs similarity index 97% rename from index.js rename to index.cjs index 69d16ee..9d74768 100644 --- a/index.js +++ b/index.cjs @@ -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 @@ -344,11 +346,16 @@ module.exports = class Hyperschema { return json } - toCode (opts) { - return generateCode(this, opts) + toCode ({ esm = this.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 }) @@ -356,7 +363,7 @@ module.exports = class Hyperschema { 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) { diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..99c10a6 --- /dev/null +++ b/index.mjs @@ -0,0 +1,7 @@ +import Hyperschema from './index.cjs' + +class ESMHyperschema extends Hyperschema { + static esm = true +} + +export default ESMHyperschema diff --git a/package.json b/package.json index 7da57db..15e5547 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,10 @@ "runtime.js" ], "exports": { - ".": "./index.js", + ".": { + "import": "./index.mjs", + "default": "./index.cjs" + }, "./runtime": { "import": "./runtime.mjs", "default": "./runtime.cjs" From 714a8bb9422ecf22c7a4b7fe10c20c57af8eb026 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Sat, 11 Jan 2025 12:24:39 +0100 Subject: [PATCH 3/4] fix typo --- index.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.cjs b/index.cjs index 9d74768..061ea2d 100644 --- a/index.cjs +++ b/index.cjs @@ -346,7 +346,7 @@ module.exports = class Hyperschema { return json } - toCode ({ esm = this.esm } = {}) { + toCode ({ esm = this.constructor.esm } = {}) { return generateCode(this, { esm }) } From 57c2edd087dd91e8072200ac04bc48ecdf05665a Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Sat, 11 Jan 2025 12:36:55 +0100 Subject: [PATCH 4/4] fix exports --- index.cjs => builder.cjs | 0 index.mjs => builder.mjs | 0 package.json | 31 ++++++++++++++++--------------- 3 files changed, 16 insertions(+), 15 deletions(-) rename index.cjs => builder.cjs (100%) rename index.mjs => builder.mjs (100%) diff --git a/index.cjs b/builder.cjs similarity index 100% rename from index.cjs rename to builder.cjs diff --git a/index.mjs b/builder.mjs similarity index 100% rename from index.mjs rename to builder.mjs diff --git a/package.json b/package.json index 15e5547..ade2ebd 100644 --- a/package.json +++ b/package.json @@ -2,22 +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": "./index.mjs", - "default": "./index.cjs" + "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" @@ -38,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",