SWC 적용

00_8_3·2022년 8월 2일
1

Migration To Prisma

목록 보기
9/11

Prisma로 마이그레이션 중
느린 빌드 속도와 테스팅 속도로 답답함을 느껴
그 유명한 SWC로 전환 하였습니다.

기존 레포가 commonjs이기 때문에 똑같이 commonjs로 설정을 맞추어 줍니다.

esm으로 진행하면 무슨무슨 function이 정의 되지 않았다. 또는 constuctor가 없다. 와 같은 에러 발생.

swc with build

commonjs로 작성된 모듈을 위해
.swcrc파일을 tsconfig와 맞추어야 합니다.

{
 "jsc": {
   "target": "es2016",
   "parser": {
     "syntax": "typescript",
     "decorators": true,
   //   "dynamicImport": true,
     "dynamicImport": false,
     "privateMethod": false,
     "functionBind": false,
     "exportDefaultFrom": false,
     "exportNamespaceFrom": false,
     "decoratorsBeforeExport": false,
     "topLevelAwait": false,
     "importMeta": false
   },
   "transform": {
     "decoratorMetadata": true,
     "legacyDecorator": true
   },
   "keepClassNames": true,
   "externalHelpers": true,
   "loose": false
 },
 "module": {
   "type": "commonjs",
   "strict": false,
   "strictMode": true,
   "lazy": false,
   "noInterop": false
 },
 "exclude": ["./src/**/.*.spec.ts$", "./**/.*.js$"],
 "sourceMaps": true
}

swc with jest

  • jest.config.ts
import type { Config } from "@jest/types";
// import fs from "fs"; // 1
// const swcConfig = JSON.parse(fs.readFileSync(`${__dirname}/.swcrc`, "utf-8")); // 2

const config: Config.InitialOptions = {
  roots: ["<rootDir>/src/"],
  clearMocks: true,
  preset: "ts-jest",
  testEnvironment: "node",
  moduleFileExtensions: ["js", "ts", "json"],
  transform: {
    // "^.+\\.ts$": ["@swc/jest", { ...swcConfig }], // 3
    "^.+\\.ts$": "@swc/jest",
    // "^.+\\.ts$": "ts-jest",
  },
  testRegex: "\\.test\\.ts$",
  globals: {
    "ts-jest": {
      diagnostics: true,
    },
  },
  testTimeout: 30000,

};
export default config;

.swcrc의 경우 tsconfig의 역할을 하는 것인데
어느 글에서 보기를 1,2,3의 주석을 사용하라 했었으나
오히려 사용하니 jest 오류발생.

"^.+\\.ts$": "@swc/jest",만 적용.

출처

https://swc.rs/docs/configuration/modules#commonjs

0개의 댓글