jonkwheeler / tsconfig-replace-paths

Replace absolute paths to relative paths for package compilation
MIT License
72 stars 11 forks source link

it's... not working? #31

Closed Lawlzer closed 2 years ago

Lawlzer commented 2 years ago

Hey! First of all, sorry for the least-useful-title ever. I'm not entirely sure what to phrase it as, I'm not entirely sure what the problem is.

First of all, if it's easier for you to share an example working repo, I'll gladly just -borrow- that and figure out what dumb think I did on my end. I'm pretty sure I'm just misconfiguring something simple, but I have no idea what (and I couldn't find any working examples -- I tried the bits of code I found, but none seemed to work for me)

Alternatively, my actual issue: When I run npm build, it runs fine, but when I run node ./build/index.js, it immediately returns an error.

Relevant npm build script: rimraf ./build && tsc --project tsconfig.json && tsconfig-replace-paths --project tsconfig.json -v

Relevant tsconfig.json:

"outDir": "build", /* Specify an output folder for all emitted files. */
    "rootDir": "./src", /* Specify the root folder within your source files. */
    "baseUrl": ".", /* Specify the base directory to resolve non-relative module names. */
    "paths": {
      "@": [
        "./src"
      ],
      "@/*": [
        "./src/*"
      ]
    },```

Running commands (build & running the compiled code)

PS C:\Users\Kevin\Downloads\tepm6969> npm run build

build rimraf ./build && tsc --project tsconfig.json && tsconfig-replace-paths --project tsconfig.json -v

Using tsconfig: C:\Users\Kevin\Downloads\tepm6969\tsconfig.json Using compilerOptions.rootDir from your tsconfig Using compilerOptions.outDir from your tsconfig Using out: C:\Users\Kevin\Downloads\tepm6969\build baseUrl: . rootDir: C:\Users\Kevin\Downloads\tepm6969\src outDir: C:\Users\Kevin\Downloads\tepm6969\build paths: { "@": [ "./src" ], "@/": [ "./src/" ] } basePath: C:\Users\Kevin\Downloads\tepm6969 outPath: C:\Users\Kevin\Downloads\tepm6969\build aliases: [ { "prefix": "@", "aliasPaths": [ "C:\Users\Kevin\Downloads\tepm6969\src" ] }, { "prefix": "@/", "aliasPaths": [ "C:\Users\Kevin\Downloads\tepm6969\src" ] } ] Replaced 0 paths in 0 files PS C:\Users\Kevin\Downloads\tepm6969> node .\build\index.js
node:internal/modules/cjs/loader:936 throw err; ^

Error: Cannot find module '@/1/1' Require stack:

./src/index.ts

import { hi } from '@/1/1';
hi(); 

./src/1/1.ts is a simple export so I won't include it here (just a logging function)

./build/index.js (COMPILED) code (maybe this will help?)

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _1_1 = require("@/1/1");
(0, _1_1.hi)();

If you'd like more information, I'd be happy to help (or if you just want a fully reproducing repo of the error, but it's probably easier if you just share a working version <3) Either way, thanks in advance!

jonkwheeler commented 2 years ago

if it's easier for you to share an example working repo

Going to throw a repo at you and then YES, give that a shot. I use this on all my repos and don't have any issues... but maybe my repo setup is unique.

Repo 1:

https://github.com/jonkwheeler/styled-velocity/blob/master/tsconfig.json https://github.com/jonkwheeler/ScrollScene/blob/master/package.json#L39 https://github.com/jonkwheeler/styled-velocity/blob/master/package.json#L37 https://github.com/jonkwheeler/styled-velocity/blob/master/tsconfig.types.cjs.json https://github.com/jonkwheeler/styled-velocity/blob/master/tsconfig.types.esm.json

Repo 2:

https://github.com/jonkwheeler/ScrollScene/blob/master/tsconfig.json https://github.com/jonkwheeler/ScrollScene/blob/master/package.json#L39 https://github.com/jonkwheeler/ScrollScene/blob/master/package.json#L35 https://github.com/jonkwheeler/ScrollScene/blob/master/tsconfig.types.cjs.json https://github.com/jonkwheeler/ScrollScene/blob/master/tsconfig.types.esm.json

I essentially use Babel to compile everything, but you don't have too, and then run build:types after on those compiled files. My configs are pretty basic and all my files are contained within a src folder, though you could change what the folder is called "rootDir": "./src",.

Lawlzer commented 2 years ago

Thanks!

I didn't want to use Babel (for one, I've never used Babel), so I tried removing Babel from yours / making it output JS... but I'm terrible with Babel :P

I got it to work with WebPack though, so thanks for the help :)

jonkwheeler commented 2 years ago

Nice @Lawlzer. Yeah like I said you don't need to use Babel. I do though.

Happy you got it working!