Skip to content
Draft
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
66 changes: 51 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
}
}
32 changes: 32 additions & 0 deletions tools/imports-plugin.tsup.js
Original file line number Diff line number Diff line change
@@ -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\((?<quote>['"])(?<import>\.[^'"]+)\.js['"]\)/g;
const regexEsm =
/from(?<space>[\s]*)(?<quote>['"])(?<import>\.[^'"]+)\.js['"]/g;
return {
code: code
.replace(regexCjs, 'require($<quote>$<import>.cjs$<quote>)')
.replace(regexEsm, 'from$<space>$<quote>$<import>.cjs$<quote>'),
};
}
return code;
},
});
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
39 changes: 22 additions & 17 deletions tsup.config.js
Original file line number Diff line number Diff line change
@@ -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()],
},
]);
Loading