From f18f01ca6b54aa816b0d14d169325813ef51e62b Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Thu, 28 Mar 2024 05:12:01 +0900 Subject: [PATCH 1/2] Publish both CJS and ESM --- .gitignore | 12 +-- .npmignore | 4 - build/dist.ts | 102 ------------------------- package.json | 48 +++++++----- rollup.config.js | 29 +++++++ src/test/numeric/test_special_maths.ts | 2 +- tsconfig.json | 2 +- 7 files changed, 63 insertions(+), 136 deletions(-) delete mode 100644 .npmignore delete mode 100644 build/dist.ts create mode 100644 rollup.config.js diff --git a/.gitignore b/.gitignore index 64a72aea..4ad89594 100644 --- a/.gitignore +++ b/.gitignore @@ -4,15 +4,7 @@ assets/benchmarks/ node_modules/ lib/ -dist/ package-lock.json -*.log - -#---- -# COMPILED FILES -#---- -*.js -*.js.map -*.d.ts -*.d.ts.map \ No newline at end of file +pnpm-lock.yaml +*.log \ No newline at end of file diff --git a/.npmignore b/.npmignore deleted file mode 100644 index f2aff2a5..00000000 --- a/.npmignore +++ /dev/null @@ -1,4 +0,0 @@ -test/ -benchmark/ - -*.js.map \ No newline at end of file diff --git a/build/dist.ts b/build/dist.ts deleted file mode 100644 index 7f9cfbcd..00000000 --- a/build/dist.ts +++ /dev/null @@ -1,102 +0,0 @@ -import cp from "child_process"; -import fs from "fs"; - -const ROOT = `${__dirname}/..`; -const DIST = `${ROOT}/dist`; - -async function documents(): Promise { - const FILES = [ - ".npmignore", - "CODE_OF_CONDUCT.md", - "CONTRIBUTING.md", - "LICENSE", - "package.json", - "README.md", - ]; - for (const file of FILES) - await fs.promises.link(`${ROOT}/${file}`, `${DIST}/${file}`); -} - -// const substitude = -// (content: string) => -// content.replace( -// /(import|export)(.*)from "(.\/|..\/)(.*)"/g, -// str => { -// const to: number = str.lastIndexOf(`"`); -// return [ -// str.substring(0, to), -// `.mjs"`, -// str.substring(to + 1) -// ].join(""); -// } -// ); - -// async function module(): Promise { -// const declaration = async (location: string)=> { -// const content = substitude( -// await fs.promises.readFile(location, "utf8") -// ); -// await fs.promises.unlink(location); -// await fs.promises.writeFile( -// `${location.substring(0, location.length - 5)}.d.mts`, -// content, -// "utf8" -// ); -// }; -// const javascript = async (location: string, file: string) => { -// const content = substitude( -// await fs.promises.readFile(location, "utf8") -// ) -// .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`, -// content, -// "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"; - -// 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 => { -// 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 javascript(location, file); -// else if (location.substr(-5) === ".d.ts") -// await declaration(location); -// else if (location.substr(-7) === ".js.map") -// await mapper(location); -// } -// }; - -// cp.execSync(`npx tsc -p tsconfig.module.json`, { stdio: "inherit" }); -// await iterate(DIST); -// } - -async function main(): Promise { - cp.execSync(`npx rimraf ${DIST}`, { stdio: "inherit" }); - await fs.promises.mkdir(DIST); - - await documents(); - // await module(); - cp.execSync(`npx tsc -p tsconfig.json`, { stdio: "inherit" }); -} -main(); diff --git a/package.json b/package.json index 420c59fd..30b73c1f 100644 --- a/package.json +++ b/package.json @@ -6,36 +6,38 @@ "email": "samchon.github@gmail.com", "url": "https://github.com/samchon" }, - "version": "2.5.16", - "typings": "./index.d.ts", - "main": "./index.js", + "version": "3.0.0", + "main": "./lib/index.js", + "typings": "./lib/index.d.ts", + "exports": { + ".": { + "types": "./lib/index.d.ts", + "require": "./lib/index.js", + "import": "./lib/index.mjs" + } + }, "scripts": { "api": "typedoc src --exclude \"**/+(test|benchmark)/**\" --excludeNotDocumented --plugin typedoc-plugin-external-module-name --plugin typedoc-plugin-exclude-references --out ../tstl@gh-pages/api", - "migration": "ts-node build/migration", "benchmark": "node benchmark", - "build": "npm run clean && npm run module && npm run compile && npm run test", - "clean": "rimraf dist", - "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 test:ts && npm run dist && cd dist && npm publish", - "package:next": "npm run package -- --tag next", - "prettier": "prettier --write ./**/*.ts", - "test": "node dist/test", - "test:ts": "ts-node src/test" + "build": "rimraf lib && tsc && rollup -c", + "dev": "rimraf lib && tsc --watch", + "prettier": "prettier --write ./src", + "test": "node lib/test" }, "devDependencies": { + "@rollup/plugin-terser": "^0.4.4", + "@rollup/plugin-typescript": "^11.1.6", "@types/cli": "^0.11.19", "@types/node": "^14.6.3", "cli": "^1.0.1", "prettier": "^2.7.1", "rimraf": "^3.0.2", + "rollup": "^4.13.1", "source-map-support": "^0.5.21", "ts-node": "^10.7.0", - "typedoc": "^0.22.13", - "typescript": "^4.6.2" + "tslib": "^2.6.2", + "typedoc": "^0.25.12", + "typescript": "^5.4.3" }, "homepage": "https://github.com/samchon/tstl", "repository": { @@ -97,5 +99,15 @@ "SharedLock", "UniqueLock", "Singleton" + ], + "files": [ + "LICENSE", + "README.md", + "lib/", + "src/", + "!lib/benchmark", + "!lib/test", + "!src/benchmark", + "!src/test" ] } diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..f04fb199 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,29 @@ +const typescript = require('@rollup/plugin-typescript'); +const terser = require('@rollup/plugin-terser'); + +module.exports = { + input: './src/index.ts', + output: { + dir: 'lib', + format: 'esm', + entryFileNames: '[name].mjs', + sourcemap: true, + }, + plugins: [ + typescript({ + tsconfig: "tsconfig.json", + module: "ES2020", + target: "ES2020", + }), + terser({ + format: { + comments: 'some', + beautify: true, + ecma: '2020', + }, + compress: false, + mangle: false, + module: true, + }), + ] +}; \ No newline at end of file diff --git a/src/test/numeric/test_special_maths.ts b/src/test/numeric/test_special_maths.ts index ac8c60f1..d5135968 100644 --- a/src/test/numeric/test_special_maths.ts +++ b/src/test/numeric/test_special_maths.ts @@ -4,7 +4,7 @@ import * as std from "../../index"; const PATH = __filename.substr(-2) === "ts" ? `${__dirname}/special_math` - : `${__dirname}/special_math`.replace("dist", "src"); + : `${__dirname}/special_math`.replace("lib", "src"); export function similar(x: number, y: number, precision = 0.05): boolean { const diff: number = _Difference(x, y); diff --git a/tsconfig.json b/tsconfig.json index b5a09aaa..82071b86 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ // "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. */ + "outDir": "./lib", /* Redirect output structure to the directory. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ From 88631224da7543cf89e2671ac1ed330ef6c26d34 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Thu, 28 Mar 2024 05:14:44 +0900 Subject: [PATCH 2/2] Fix github actions bug --- .github/workflows/build.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a6adeb59..0b540f46 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,11 +5,10 @@ jobs: Ubuntu: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: - node-version: 12.x + node-version: 20.x - run: npm install - - run: npm run module - - run: npm run compile + - run: npm run build - run: npm run test \ No newline at end of file