Skip to content

Commit

Permalink
Merge pull request #144 from msgpack/mjs
Browse files Browse the repository at this point in the history
change the esm extension to .mjs
  • Loading branch information
gfx authored Oct 12, 2020
2 parents d1c88ac + 64cbb2a commit 7c957a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 15 additions & 4 deletions tools/esmify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}

0 comments on commit 7c957a0

Please sign in to comment.