From b7d4a9e53715221e1a4fc9f60bf48b5f5b48dc6f Mon Sep 17 00:00:00 2001 From: daengdaengLee Date: Mon, 28 Aug 2023 20:42:04 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20typescript=20=EB=B9=8C=EB=93=9C=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EB=B9=8C?= =?UTF-8?q?=EB=93=9C=20=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cjs/tsconfig.json | 1 - esm/tsconfig.json | 2 -- package.json | 4 ++-- script/backup-package-json.mjs | 13 +++++++++++++ script/restore-package-json.mjs | 19 +++++++++++++++++++ script/update-package-json-type.mjs | 25 +++++++++++++++++++++++++ tsconfig.json | 9 +++++++-- 7 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 script/backup-package-json.mjs create mode 100644 script/restore-package-json.mjs create mode 100644 script/update-package-json-type.mjs diff --git a/cjs/tsconfig.json b/cjs/tsconfig.json index a628ed3..2a087db 100644 --- a/cjs/tsconfig.json +++ b/cjs/tsconfig.json @@ -1,7 +1,6 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "module": "CommonJS", "outDir": "./" } } diff --git a/esm/tsconfig.json b/esm/tsconfig.json index 859c3f3..2a087db 100644 --- a/esm/tsconfig.json +++ b/esm/tsconfig.json @@ -1,8 +1,6 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "module": "ES2020", - "moduleResolution": "node16", "outDir": "./" } } diff --git a/package.json b/package.json index 539b699..681cc66 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,8 @@ "clean:cjs": "tsc --build ./cjs/tsconfig.json --clean", "clean:esm": "tsc --build ./esm/tsconfig.json --clean", "clean": "npm run clean:cjs && npm run clean:esm", - "build:cjs": "tsc --build ./cjs/tsconfig.json", - "build:esm": "tsc --build ./esm/tsconfig.json", + "build:cjs": "node script/backup-package-json.mjs && node script/update-package-json-type.mjs commonjs && tsc --build ./cjs/tsconfig.json && node script/restore-package-json.mjs", + "build:esm": "node script/backup-package-json.mjs && node script/update-package-json-type.mjs module && tsc --build ./esm/tsconfig.json && node script/restore-package-json.mjs", "build": "npm run clean && npm run format:check && npm run build:cjs && npm run build:esm", "test": "jest" }, diff --git a/script/backup-package-json.mjs b/script/backup-package-json.mjs new file mode 100644 index 0000000..e1bf975 --- /dev/null +++ b/script/backup-package-json.mjs @@ -0,0 +1,13 @@ +import fs from "node:fs"; +import path from "node:path"; +import url from "node:url"; + +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); + +const packageJsonPath = path.resolve(__dirname, `../package.json`); +const packageJsonBackupPath = path.resolve(__dirname, `../package.json.backup`); +fs.copyFileSync( + packageJsonPath, + packageJsonBackupPath, + fs.constants.COPYFILE_EXCL, +); diff --git a/script/restore-package-json.mjs b/script/restore-package-json.mjs new file mode 100644 index 0000000..57a0347 --- /dev/null +++ b/script/restore-package-json.mjs @@ -0,0 +1,19 @@ +import fs from "node:fs"; +import path from "node:path"; +import url from "node:url"; + +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); + +const packageJsonBackupPath = path.resolve(__dirname, `../package.json.backup`); +const packageJsonPath = path.resolve(__dirname, `../package.json`); +let packageJson = fs + .readFileSync(packageJsonBackupPath, { + encoding: `utf-8`, + }) + .trim(); +packageJson = `${packageJson}\n`; +fs.writeFileSync(packageJsonPath, packageJson, { + encoding: `utf-8`, + flag: `w`, +}); +fs.rmSync(packageJsonBackupPath); diff --git a/script/update-package-json-type.mjs b/script/update-package-json-type.mjs new file mode 100644 index 0000000..a2ac0b4 --- /dev/null +++ b/script/update-package-json-type.mjs @@ -0,0 +1,25 @@ +import fs from "node:fs"; +import path from "node:path"; +import url from "node:url"; + +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); + +const COMMON_JS = `commonjs`; +const MODULE = `module`; + +const packageJsonType = process.argv.at(2) ?? null; +if (packageJsonType !== COMMON_JS && packageJsonType !== MODULE) { + throw new Error( + `invalid arguments. use one of "${COMMON_JS}" or "${MODULE}".`, + ); +} + +const packageJsonPath = path.resolve(__dirname, `../package.json`); +const packageJson = JSON.parse( + fs.readFileSync(packageJsonPath, { encoding: `utf-8` }), +); +packageJson.type = packageJsonType; +fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), { + encoding: `utf-8`, + flag: `w`, +}); diff --git a/tsconfig.json b/tsconfig.json index 7578e52..1e2c355 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,12 @@ { "extends": "@tsconfig/node18/tsconfig.json", - "include": ["src/**/*"], - "exclude": ["src/**/*.test.ts"], + "include": [ + "src/**/*" + ], + "exclude": [ + "src/**/*.test.ts", + "src/test-util/**/*" + ], "compilerOptions": { "declaration": true, "declarationMap": true,