diff --git a/package.json b/package.json index 5441842..67e4dd4 100644 --- a/package.json +++ b/package.json @@ -8,11 +8,24 @@ "module": "./dist.esm/index.mjs", "cdn": "./dist.umd/msgpack.min.js", "unpkg": "./dist.umd/msgpack.min.js", - "types": "./dist.esm/index.d.ts", + "types": "./dist.esm/index.d.mts", + "exports": { + ".": { + "import": { + "types": "./dist.esm/index.d.mts", + "default": "./dist.esm/index.mjs" + }, + "require": { + "types": "./dist.cjs/index.d.cts", + "default": "./dist.cjs/index.cjs" + } + }, + "./package.json": "./package.json" + }, "sideEffects": false, "scripts": { "build": "npm publish --dry-run", - "prepare": "npm run clean && webpack --bail && tsgo --build tsconfig.dist.cjs.json tsconfig.dist.esm.json && tsimp tools/fix-ext.mts --mjs dist.esm/*.js dist.esm/*/*.js && tsimp tools/fix-ext.mts --cjs dist.cjs/*.js dist.cjs/*/*.js", + "prepare": "npm run clean && webpack --bail && tsgo --build tsconfig.dist.cjs.json tsconfig.dist.esm.json && tsimp tools/fix-ext.mts --mjs dist.esm/*.js dist.esm/*/*.js dist.esm/*.d.ts dist.esm/*/*.d.ts && tsimp tools/fix-ext.mts --cjs dist.cjs/*.js dist.cjs/*/*.js dist.cjs/*.d.ts dist.cjs/*/*.d.ts", "prepublishOnly": "npm run test:dist", "clean": "rimraf build dist dist.*", "test": "mocha 'test/**/*.test.ts'", diff --git a/tools/fix-ext.mts b/tools/fix-ext.mts index bd6f737..d7cd283 100644 --- a/tools/fix-ext.mts +++ b/tools/fix-ext.mts @@ -4,25 +4,39 @@ const mode = process.argv[2]; // --cjs or --mjs const files = process.argv.slice(3); const ext = mode === "--cjs" ? "cjs" : "mjs"; +const dtsExt = mode === "--cjs" ? "d.cts" : "d.mts"; console.info(`Fixing ${mode} files with extension ${ext}`); for (const file of files) { - const fileMjs = file.replace(/\.js$/, `.${ext}`); - console.info(`Processing ${file} => ${fileMjs}`); - // .js => .mjs - const content = fs.readFileSync(file).toString("utf-8"); - const newContent = content - .replace(/\bfrom "(\.\.?\/[^"]+)\.js";/g, `from "$1.${ext}";`) - .replace(/\bimport "(\.\.?\/[^"]+)\.js";/g, `import "$1.${ext}";`) - .replace(/\brequire\("(\.\.?\/[^"]+)\.js"\)/g, `require("$1.${ext}");`) - .replace(/\/\/# sourceMappingURL=(.+)\.js\.map$/, `//# sourceMappingURL=$1.${ext}.map`); - fs.writeFileSync(fileMjs, newContent); - fs.unlinkSync(file); + if (file.endsWith(".d.ts")) { + // Handle declaration files: .d.ts => .d.mts or .d.cts + const newFile = file.replace(/\.d\.ts$/, `.${dtsExt}`); + console.info(`Processing ${file} => ${newFile}`); + const content = fs.readFileSync(file).toString("utf-8"); + // Fix import paths: .ts => .mjs or .cjs + const newContent = content + .replace(/\bfrom "(\.\.?\/[^"]+)\.ts";/g, `from "$1.${ext}";`) + .replace(/\bimport "(\.\.?\/[^"]+)\.ts";/g, `import "$1.${ext}";`); + fs.writeFileSync(newFile, newContent); + fs.unlinkSync(file); + } else if (file.endsWith(".js")) { + // Handle JS files: .js => .mjs or .cjs + const fileMjs = file.replace(/\.js$/, `.${ext}`); + console.info(`Processing ${file} => ${fileMjs}`); + const content = fs.readFileSync(file).toString("utf-8"); + const newContent = content + .replace(/\bfrom "(\.\.?\/[^"]+)\.js";/g, `from "$1.${ext}";`) + .replace(/\bimport "(\.\.?\/[^"]+)\.js";/g, `import "$1.${ext}";`) + .replace(/\brequire\("(\.\.?\/[^"]+)\.js"\)/g, `require("$1.${ext}");`) + .replace(/\/\/# sourceMappingURL=(.+)\.js\.map$/, `//# sourceMappingURL=$1.${ext}.map`); + fs.writeFileSync(fileMjs, newContent); + fs.unlinkSync(file); - // .js.map => .mjs.map - const mapping = JSON.parse(fs.readFileSync(`${file}.map`).toString("utf-8")); - mapping.file = mapping.file.replace(/\.js$/, ext); - fs.writeFileSync(`${fileMjs}.map`, JSON.stringify(mapping)); - fs.unlinkSync(`${file}.map`); + // .js.map => .mjs.map + const mapping = JSON.parse(fs.readFileSync(`${file}.map`).toString("utf-8")); + mapping.file = mapping.file.replace(/\.js$/, ext); + fs.writeFileSync(`${fileMjs}.map`, JSON.stringify(mapping)); + fs.unlinkSync(`${file}.map`); + } } diff --git a/tsconfig.dist.cjs.json b/tsconfig.dist.cjs.json index 8225a84..3a5c60d 100644 --- a/tsconfig.dist.cjs.json +++ b/tsconfig.dist.cjs.json @@ -3,7 +3,7 @@ "compilerOptions": { "module": "CommonJS", "outDir": "./dist.cjs", - "declaration": false, + "declaration": true, "noEmitOnError": true, "noEmit": false, "rewriteRelativeImportExtensions": true,