LoginSignup
15
2

More than 1 year has passed since last update.

TypeScriptで相対パスのモジュールをimportしたかった

Last updated at Posted at 2021-12-02

相対パスでモジュールimportしたかったので、ttypescript@zerollup/ts-transform-paths を利用していたけど、typescriptのアップデートで壊れちゃった。

../src/lib/paresArgvと書かなくても、こんなかんじのができてた。

import { parseArgv } from 'lib/parseArgv'

調べてみたら、同じ目にあってる人もいた。

ts-patch

ts-patchを導入すれば、ttypescriptと同等のことをやってくれるっぽい。

対応方法

雑に直した差分を貼っとく
tsconfig.jsonにbaseUrl, paths, pluginsへの設定がない人は入れときましょう。

diff --git a/package.json b/package.json
index dcf37b8..276f3df 100644
--- a/package.json
+++ b/package.json
@@ -3,8 +3,9 @@
   "scripts": {
+    "prepare": "ts-patch install -s",
-    "build": "ttsc -P ./tsconfig.json",
+    "build": "tsc -P ./tsconfig.json",
@@ -25,7 +26,6 @@
-    "@zerollup/ts-transform-paths": "^1.7.18",
+    "ts-patch": "^2.0.1",
-    "ttypescript": "^1.5.12",
+    "typescript-transform-paths": "^3.3.1"

diff --git a/tsconfig.json b/tsconfig.json
index 4a4cf58..a12c084 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -7,7 +7,7 @@
     "outDir": "./dist",
     "rootDir": "./src",
     "strict": true,
-    "plugins": [{ "transform": "@zerollup/ts-transform-paths" }],
+    "plugins": [{ "transform": "typescript-transform-paths" }],
     "baseUrl": "src",
     "paths": {
       "*": ["*"]
     },

tsonfig.json

つかってる設定ファイルもペタリ

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2021",
    "module": "CommonJS",
    "lib": ["es2021"],
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "plugins": [{ "transform": "typescript-transform-paths" }],
    "moduleResolution": "node",
    "baseUrl": "src",
    "paths": {
      "*": ["*"]
    },
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "noUnusedLocals": false,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedIndexedAccess": true
  },
  "include": ["src/**/*"],
  "exclude": ["dist", "node_modules"],
  "compileOnSave": false
}

15
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
2