Skip to content

Commit

Permalink
Merge pull request #103 from samchon/v2.5
Browse files Browse the repository at this point in the history
Close #102 - support both CommonJS and EsModule
  • Loading branch information
samchon committed Oct 9, 2022
2 parents 679a37b + 3366811 commit 69de515
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ jobs:
Ubuntu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 12.x
- run: npm install
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function main(): void
map.emplace(5, "Fifth");
map.set(9, "Nineth");

for (let it = map.begin(); !it.equals(map.end()); it = it.next())
for (const it of map)
console.log(it.first, it.second);

const it: std.TreeMap.Iterator<number, string> = map.lower_bound(3);
Expand Down
55 changes: 47 additions & 8 deletions build/dist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as fs from "fs";
import cp from "child_process";
import fs from "fs";

async function main(): Promise<void>
const ROOT = `${__dirname}/..`;
const DIST = `${ROOT}/dist`;

async function documents(): Promise<void>
{
const FILES = [
".npmignore",
Expand All @@ -10,14 +14,49 @@ async function main(): Promise<void>
"package.json",
"README.md"
];
const ROOT = `${__dirname}/..`;
const DIST = `${ROOT}/dist`;

for (const file of FILES)
{
if (fs.existsSync(`${DIST}/${file}`) === true)
await fs.promises.unlink(`${DIST}/${file}`);
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" });

if (mode === "CommonJS") return;
const iterate = async (path: string): Promise<void> =>
{
const directory: string[] = await fs.promises.readdir(path);
for (const file of directory) {
const location: string = `${path}/${file}`;
const stats: fs.Stats = await fs.promises.stat(location);

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

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

await documents();
await compile("EsModule");
await compile("CommonJS");
}
main();
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"email": "samchon.github@gmail.com",
"url": "https://github.com/samchon"
},
"version": "2.5.8",
"version": "2.5.9",
"main": "./index.js",
"module": "./index.mjs",
"esnext": "./index.mjs",
"typings": "./index.d.ts",
"scripts": {
"api": "typedoc src --exclude \"**/+(test|benchmark)/**\" --excludeNotDocumented --plugin typedoc-plugin-external-module-name --plugin typedoc-plugin-exclude-references --out ../tstl@gh-pages/api",
Expand All @@ -18,8 +20,9 @@
"compile": "tsc",
"dev": "tsc --watch",
"dev:ts": "tsc --watch --noEmit --module esnext",
"dist": "ts-node build/dist",
"module": "tsc --noEmit --module amd && tsc --noEmit --module system && tsc --noEmit --module umd && tsc --noEmit --module esnext",
"package": "npm run build && ts-node build/dist && cd dist && npm publish",
"package": "npm run test:ts && npm run dist && cd dist && npm publish",
"package:next": "npm run package -- --tag next",
"test": "node dist/test",
"test:ts": "ts-node src/test"
Expand Down
19 changes: 11 additions & 8 deletions src/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const EXTENSION = __filename.substr(-2);
if (EXTENSION === "js")
require("source-map-support").install();
const EXTENSION = (() => {
const index: number = __filename.lastIndexOf(".");
return __filename.substring(index + 1);
})();
if (EXTENSION === "js" || EXTENSION === "mjs")
require("source-map-support").install();

import * as cli from "cli";
import * as fs from "fs";
Expand All @@ -27,18 +30,18 @@ async function iterate(command: ICommand, path: string): Promise<void>
const fileList: string[] = await fs.promises.readdir(path);
for (const file of fileList)
{
const currentPath: string = `${path}/${file}`;
const stats: fs.Stats = await fs.promises.lstat(currentPath);
const location: string = `${path}/${file}`;
const stats: fs.Stats = await fs.promises.lstat(location);

if (stats.isDirectory() === true && file !== "internal" && file !== "manual")
{
await iterate(command, currentPath);
await iterate(command, location);
continue;
}
else if (file.substr(-3) !== `.${EXTENSION}` || currentPath === `${__dirname}/index.${EXTENSION}`)
else if (file.substr(-(EXTENSION.length + 1)) !== `.${EXTENSION}` || location === `${__dirname}/index.${EXTENSION}`)
continue;

const external: IModule = await import(currentPath.substr(0, currentPath.length - 3));
const external: IModule = await import(location);
for (const key in external)
{
// WHETHER TESTING TARGET OR NOT
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES2015",
"module": "ES2020",
"downlevelIteration": false
}
}

0 comments on commit 69de515

Please sign in to comment.