Skip to content

Commit

Permalink
Complement #102 - edit mjs files
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Oct 10, 2022
1 parent d3c995b commit 80e0f88
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
63 changes: 41 additions & 22 deletions build/dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,45 @@ async function documents(): Promise<void> {
"package.json",
"README.md",
];

for (const file of FILES)
await fs.promises.link(`${ROOT}/${file}`, `${DIST}/${file}`);
}

async function compile(mode: "CommonJS" | "EsModule"): Promise<void> {
const extension: string = mode === "CommonJS" ? "cjs" : "mjs";
const file: string = `${ROOT}/tsconfig.${
mode === "CommonJS" ? "" : "module."
}json`;
cp.execSync(`tsc -p ${file}`, { stdio: "inherit" });
async function module(): Promise<void> {
const javascript = async (location: string, file: string) => {
const content: string = await fs.promises.readFile(location, "utf8");
const replaced: string = content
.replace(/(import|export)(.*)from "(.\/|..\/)(.*)"/g, str => {
const to: number = str.lastIndexOf(`"`);
return [
str.substring(0, to),
`.mjs"`,
str.substring(to + 1)
].join("");
})
.replace(
`//# sourceMappingURL=${file}.map`,
`//# sourceMappingURL=${file.substring(0, file.length - 3)}.mjs.map`
);
await fs.promises.unlink(location);
await fs.promises.writeFile(
`${location.substring(0, location.length - 3)}.mjs`,
replaced,
"utf8"
);
};
const mapper = async (location: string) => {
const content: string = await fs.promises.readFile(location, "utf8");
const parsed: {file: string } = JSON.parse(content);
parsed.file = parsed.file.substring(0, parsed.file.length - 3) + ".mjs";

if (mode === "CommonJS") return;
await fs.promises.unlink(location);
await fs.promises.writeFile(
`${location.substring(0, location.length - 7)}.mjs.map`,
JSON.stringify(parsed),
"utf8"
);
};
const iterate = async (path: string): Promise<void> => {
const directory: string[] = await fs.promises.readdir(path);
for (const file of directory) {
Expand All @@ -34,29 +60,22 @@ async function compile(mode: "CommonJS" | "EsModule"): Promise<void> {

if (stats.isDirectory()) await iterate(location);
else if (location.substr(-3) === ".js")
await fs.promises.rename(
location,
`${location.substr(0, location.length - 3)}.${extension}`,
);
await javascript(location, file);
else if (location.substr(-7) === ".js.map")
await fs.promises.rename(
location,
`${location.substr(
0,
location.length - 7,
)}.${extension}.map`,
);
await mapper(location);
}
};

cp.execSync(`npx tsc -p tsconfig.module.json`, { stdio: "inherit" });
await iterate(DIST);
}

async function main(): Promise<void> {
cp.execSync(`rimraf ${DIST}`, { stdio: "inherit" });
cp.execSync(`npx rimraf ${DIST}`, { stdio: "inherit" });
await fs.promises.mkdir(DIST);

await documents();
await compile("EsModule");
await compile("CommonJS");
await module();
cp.execSync(`npx tsc -p tsconfig.json`, { stdio: "inherit" });
}
main();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"email": "samchon.github@gmail.com",
"url": "https://github.com/samchon"
},
"version": "2.5.9",
"version": "2.5.10",
"main": "./index.js",
"module": "./index.mjs",
"esnext": "./index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/internal/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export function _Get_root(): IGlobal {
/**
* @internal
*/
var __s_pRoot: IGlobal | null = null;
let __s_pRoot: IGlobal | null = null;
2 changes: 1 addition & 1 deletion src/utility/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @module std
*/
//================================================================
var is_node_: boolean | null = null;
let is_node_: boolean | null = null;

/**
* Test whether the code is running on NodeJS.
Expand Down

0 comments on commit 80e0f88

Please sign in to comment.