diff --git a/package.json b/package.json index b45a8d0..30b43cb 100644 --- a/package.json +++ b/package.json @@ -2,23 +2,10 @@ "name": "cbor2", "version": "1.8.0", "description": "Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC8949).", - "exports": { - ".": "./lib/index.js", - "./comment": "./lib/comment.js", - "./decoder": "./lib/decoder.js", - "./diagnostic": "./lib/diagnostic.js", - "./encoder": "./lib/encoder.js", - "./simple": "./lib/simple.js", - "./sorts": "./lib/sorts.js", - "./tag": "./lib/tag.js", - "./types": "./lib/types.js", - "./utils": "./lib/utils.js", - "./writer": "./lib/writer.js" - }, + "type": "module", "files": [ "lib" ], - "type": "module", "repository": { "type": "git", "url": "git+ssh://git@github.com/hildjj/cbor2.git" @@ -74,5 +61,54 @@ "packageManager": "pnpm@9.14.4", "engines": { "node": ">=18" - } + }, + "main": "./lib/cjs/index.cjs", + "module": "./lib/index.js", + "types": "./lib/index.d.ts", + "exports": { + ".": { + "require": "./lib/cjs/index.cjs", + "import": "./lib/index.js" + }, + "./comment": { + "require": "./lib/cjs/comment.cjs", + "import": "./lib/comment.js" + }, + "./decoder": { + "require": "./lib/cjs/decoder.cjs", + "import": "./lib/decoder.js" + }, + "./diagnostic": { + "require": "./lib/cjs/diagnostic.cjs", + "import": "./lib/diagnostic.js" + }, + "./encoder": { + "require": "./lib/cjs/encoder.cjs", + "import": "./lib/encoder.js" + }, + "./simple": { + "require": "./lib/cjs/simple.cjs", + "import": "./lib/simple.js" + }, + "./sorts": { + "require": "./lib/cjs/sorts.cjs", + "import": "./lib/sorts.js" + }, + "./tag": { + "require": "./lib/cjs/tag.cjs", + "import": "./lib/tag.js" + }, + "./types": { + "require": "./lib/cjs/types.cjs", + "import": "./lib/types.js" + }, + "./utils": { + "require": "./lib/cjs/utils.cjs", + "import": "./lib/utils.js" + }, + "./writer": { + "require": "./lib/cjs/writer.cjs", + "import": "./lib/writer.js" + } + } } diff --git a/tools/imports-plugin.tsup.js b/tools/imports-plugin.tsup.js new file mode 100644 index 0000000..3264482 --- /dev/null +++ b/tools/imports-plugin.tsup.js @@ -0,0 +1,32 @@ +/** + * Taken from: https://github.com/egoist/tsup/issues/953#issuecomment-2294998890 + * + * On TSUP when we compile to CJS, file extensions are set as .cjs but + * in the compiled files, the imports are still pointing to .js files. + * + * This plugin replaces all the imports in CJS files to point to .cjs files. + * + * - require('./path') → require('./path.cjs') in `.cjs` files + * - require('./path.js') → require('./path.cjs') in `.cjs` files + * - from './path' → from './path.cjs' in `.cjs` files + * - from './path.js' → from './path.cjs' in `.cjs` files + * - from '../path' → from '../path.cjs' in `.cjs` files + */ + +export const cjsImportsPlugin = () => ({ + name: 'cjs-imports', + + renderChunk(code) { + if (this.format === 'cjs') { + const regexCjs = /require\((?['"])(?\.[^'"]+)\.js['"]\)/g; + const regexEsm = + /from(?[\s]*)(?['"])(?\.[^'"]+)\.js['"]/g; + return { + code: code + .replace(regexCjs, 'require($$.cjs$)') + .replace(regexEsm, 'from$$$.cjs$'), + }; + } + return code; + }, +}); diff --git a/tsconfig.json b/tsconfig.json index 773fc08..e77f2df 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,9 +25,9 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "ES2022", /* Specify what module code is generated. */ + "module": "node16", /* Specify what module code is generated. */ // "rootDir": "./", /* Specify the root folder within your source files. */ - // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "node16", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ diff --git a/tsup.config.js b/tsup.config.js index bf65c9f..3ddae6a 100644 --- a/tsup.config.js +++ b/tsup.config.js @@ -1,25 +1,30 @@ +import {cjsImportsPlugin} from './tools/imports-plugin.tsup.js'; import {defineConfig} from 'tsup'; -import {fileURLToPath} from 'node:url'; -import fs from 'node:fs/promises'; -import path from 'node:path'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - -const files = await fs.readdir(path.join(__dirname, 'src')); -const entry = files - .filter(f => f.endsWith('.ts')) - .map(f => path.join('src', f)); - -export default defineConfig({ +const commonSettings = { clean: true, dts: true, - entry, - format: 'esm', + entry: ['src/*.ts'], minify: true, - outDir: 'lib', - sourcemap: false, + sourcemap: true, splitting: false, target: 'es2022', bundle: false, -}); + cjsInterop: true, +}; + +export default defineConfig([ + // ESM configuration + { + ...commonSettings, + format: ['esm'], + outDir: 'lib', + }, + // CJS configuration + { + ...commonSettings, + format: ['cjs'], + outDir: 'lib/cjs', + plugins: [cjsImportsPlugin()], + }, +]);