Skip to content

Commit

Permalink
fix: typescript 빌드 설정 수정 및 빌드 스크립트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
daengdaengLee committed Aug 28, 2023
1 parent 7665f02 commit b7d4a9e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 7 deletions.
1 change: 0 additions & 1 deletion cjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "./"
}
}
2 changes: 0 additions & 2 deletions esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "ES2020",
"moduleResolution": "node16",
"outDir": "./"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
13 changes: 13 additions & 0 deletions script/backup-package-json.mjs
Original file line number Diff line number Diff line change
@@ -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,
);
19 changes: 19 additions & 0 deletions script/restore-package-json.mjs
Original file line number Diff line number Diff line change
@@ -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);
25 changes: 25 additions & 0 deletions script/update-package-json-type.mjs
Original file line number Diff line number Diff line change
@@ -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`,
});
9 changes: 7 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit b7d4a9e

Please sign in to comment.