diff --git a/package.json b/package.json index b77589a2..4903865a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "The MessagePack community", "license": "ISC", "main": "./dist/index.js", - "module": "./dist.es5+esm/index.js", + "module": "./dist.es5+esm/index.mjs", "cdn": "./dist.es5+umd/msgpack.min.js", "unpkg": "./dist.es5+umd/msgpack.min.js", "types": "./dist/index.d.ts", diff --git a/tools/esmify.ts b/tools/esmify.ts index b3a22a4c..f91e18f7 100644 --- a/tools/esmify.ts +++ b/tools/esmify.ts @@ -6,8 +6,19 @@ import fs from "fs"; const files = process.argv.slice(2); for (const file of files) { - console.info(`Processing ${file}`); - const content = fs.readFileSync(file).toString("utf8"); - const newContent = content.replace(/\bfrom "([^"]+)";/g, 'from "$1.js";'); - fs.writeFileSync(file, newContent); + const fileMjs = file.replace(/\.js$/, ".mjs"); + console.info(`Processing ${file} => ${fileMjs}`); + // .js => .mjs + const content = fs.readFileSync(file).toString("utf-8"); + const newContent = content.replace(/\bfrom "([^"]+)";/g, 'from "$1.mjs";') + .replace(/\/\/# sourceMappingURL=(.+)\.js\.map$/, + "//# sourceMappingURL=$1.mjs.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$/, ".mjs"); + fs.writeFileSync(`${fileMjs}.map`, JSON.stringify(mapping)); + fs.unlinkSync(`${file}.map`); }