Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Includes baseUrl in parsed path when it shouldn't #1

Open
spentak opened this issue Jan 17, 2018 · 24 comments
Open

Includes baseUrl in parsed path when it shouldn't #1

spentak opened this issue Jan 17, 2018 · 24 comments

Comments

@spentak
Copy link

spentak commented Jan 17, 2018

Great work on this well needed package!

I think there may be an issue with resolving paths. With the correct setup for paths I get an error.

So I have:

Project
    src
    dist

My tsconfig:

        "outDir": "dist",
        "baseUrl": "./",
        "paths": {
            "api/*": ["src/api/*"],
            "utilities/*": ["src/utilities/*"],
            "middleware/*": ["src/middleware/*"],
            "resolvers/*": ["src/resolvers/*"],
            "graphql-schemas/*": ["src/graphql-schemas/*"]
        },

The line of code:

import { graphqlSchema } from 'graphql-schemas/schemas';

Trace:

Error: Cannot find module '../src/graphql-schemas/schemas'

Expected parsed path: ../graphql-schemas/schemas

The paths that the parser are putting out are using the baseUrl as the root (it appears), but in reality they should be in relation to the directory in which they live? Like a reverse traverse upwards until they find the file?

And if i take the src out of the arrays, TypeScript wont' compile because it can't resolve it. Can't have one without the other :)

@spentak spentak changed the title Looks for dist folder in wrong spot Includes baseUrl in parsed path when it shouldn't Jan 17, 2018
@spentak
Copy link
Author

spentak commented Jan 17, 2018

Possible solution would be to allow an argument to the CLI --jsPath

tspath -f --jsPath ./dist (instead of reading from tsconfig baseUrl)

@duffman
Copy link
Owner

duffman commented Jan 20, 2018

HI there! I will reproduce your setup and look for possible solutions! brb :) thanks for helping me squash a potential bug!

@erikjalevik
Copy link

erikjalevik commented Apr 30, 2018

Unfortunately @spentak's solution doesn't work for me. I have:

"outDir": "dist",
"baseUrl": "./src"

and after building the project, I run:

./node_modules/.bin/tspath -f -jsPath ./dist

TSPath then tries to find the dist folder under src:

Error: ENOENT: no such file or directory, scandir: 'D:\...\projectName\src\dist'

It should be looking in 'D:...\projectName\dist'.

EDIT: only after writing this do I notice that your solution is only a SUGGESTED solution. Doh.

@ShepelievD
Copy link

@erikjalevik the same

@erikjalevik
Copy link

I also put up a SO question here:

https://stackoverflow.com/questions/50101147/how-to-configure-electron-to-use-typescripts-baseurl

But still no replies.

@ShepelievD
Copy link

@erikjalevik I have a decision, but my webstorm breaks, will answer you once success

@ShepelievD
Copy link

ShepelievD commented May 8, 2018

@erikjalevik what I did:

my tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "target": "esnext",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "@config/*": ["src/config/*"],
      "@models/*": ["src/models/*"],
      "@routes/*": ["src/routes/*"],
      "@src/types/*": ["src/types/*"],
      "@utils/*": ["src/utils/*"],
      "@graphql/*": ["src/graphql/*"],
      "*": [
        "node_modules/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}

I made tsconfig.path.json

{
  "compilerOptions": {
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "@config/*": ["config/*"],
      "@models/*": ["models/*"],
      "@routes/*": ["routes/*"],
      "@src/types/*": ["types/*"],
      "@utils/*": ["utils/*"],
      "@graphql/*": ["graphql/*"],
      "*": ["node_modules/*"]
    }
  }
}

what I have to do is change TS_CONFIG to my custom own, ideally, we have to pass option to config file. But still it looks not quite good, I guess for first time we have to do it.

@duffman what do you think?

@ShepelievD
Copy link

@erikjalevik https://github.com/ShepelievD/tspath check it, example usage tspath -f ./dist -c tsconfig.path.json

Yeah, I know that is tricky, but I need it urgent :)

@dnt294
Copy link

dnt294 commented May 12, 2018

@ShepelievD 404 not found :(

@ShepelievD
Copy link

@kaanoo2904 yes, removed it, because decided to use https://github.com/ilearnio/module-alias

package.json:

{
  ...
  "_moduleAliases": {
    "@config": "dist/config",
    "@models": "dist/models",
    "@routes": "dist/routes",
    "@types": "dist/types",
    "@utils": "dist/utils",
    "@graphql": "dist/graphql"
  },
  ...
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "target": "esnext",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": "src",
    "paths": {
      "@config/*": ["config/*"],
      "@models/*": ["models/*"],
      "@routes/*": ["routes/*"],
      "@types/*": ["types/*"],
      "@utils/*": ["utils/*"],
      "@graphql/*": ["graphql/*"]
    }
  },
  "include": ["src/**/*"]
}

@iSuslov
Copy link

iSuslov commented Aug 21, 2018

so.. it is useless because of this

@duffman
Copy link
Owner

duffman commented Sep 12, 2018

so.. it is useless because of this

No, you are pretty useless at reading and interpreting stuff I´d say... not even tspath can help you there

@duffman
Copy link
Owner

duffman commented Sep 13, 2018

This has been resolved in 1.3.5

Regarding the suggestion of having tspath accept a param specifying the dist folder, I really don´t see when you´d use that? Being able to make it independent of the tsconfig.json would perhaps be something you could use in a headless build process, I don´t know but to single out the dist directory makes no real sense?

@0x80
Copy link

0x80 commented Jan 24, 2019

@duffman I'm a little confused. I am experiencing the same issue as the OP using version 1.3.7. And if this has been resolved why is this issue still open?

My compiler options contain:

{
    "outDir": "build",
    "baseUrl": ".",
    "paths": {
      "@src/*": ["src/*"],
      "@modules/*": ["src/modules/*"],
      "@utils/*": ["src/utils/*"]
    },
	"include": ["src/**/*"]
}

Paths in the build directory are all one level off. They contain "src" when they shouldn't. What would be a fix for this?

@0x80
Copy link

0x80 commented Jan 24, 2019

For now I've worked around it using module-alias with the following in my package.json:

"_moduleAliases": {
    "@src": "build",
    "@modules": "build/modules",
    "@utils": "build/utils"
  },

My deployed code now runs (using google cloud functions). I'd still prefer to rewrite the paths with tspath though.

@nilswx
Copy link

nilswx commented Jan 28, 2019

The fact that this is still unresolved is completely bananas. 🍌

@duffman
Copy link
Owner

duffman commented Feb 2, 2019 via email

@nilswx
Copy link

nilswx commented Feb 4, 2019

For runtime resolution of src/... files to a build or dist directory, I can confirm that https://github.com/ilearnio/module-alias works beautifully. It also doesn't need a tsconfig.json at runtime. Make sure to programmatically register your @app (or whatever) module to __dirname, so that it ends up looking under your build directory when resolving your absolute paths.

If you prefer 'build time' rewriting with a compiler plugin, the only ttypescript plugin that I actually got to work in this src scenario was https://github.com/zerkalica/zerollup. It gets the job done and requires no runtime magic, but keep in mind that from now on you need to tell your IDE (VS Code / IntelliJ / ...) to compile with ttsc rather than tsc.

IntelliJ in particular is not a fan of this, and I'd randomly end up with a build directory that still had the @absolute import paths untouched, so I abandoned compile time rewriting in favor of runtime resolution with a module alias.

I hope this is of help to someone. 🙂

@nilswx
Copy link

nilswx commented Feb 4, 2019

@duffman: I was hoping that tspath would recognize the use of rootDir in tsconfig.json and strip it from the paths.

@0x80
Copy link

0x80 commented Feb 4, 2019

@duffman Sorry for not being clear. With the tsconfig I use, src will not be part of the paths in the build (or dist) output.

{
    "outDir": "build",
    "baseUrl": ".",
    "paths": {
      "@src/*": ["src/*"],
      "@modules/*": ["src/modules/*"],
      "@utils/*": ["src/utils/*"]
    },
	"include": ["src/**/*"]
}

A file at /src/modules/foo.ts will compile to /build/modules/foo.js and /src/bar.ts to /build/bar.js. If bar.ts was referencing foo.ts via path alias @modules/foo, the output of tspath will currently be ./src/modules/foo.js.

Alternatively I have tried this config:

{
    "outDir": "build",
    "baseUrl": "./src",
    "paths": {
      "@src/*": ["./*"],
      "@modules/*": ["modules/*"],
      "@utils/*": ["utils/*"]
    },
	"include": ["src/**/*"]
}

This effectively results in the same output file structure. If I then try to run tspath, I get an error because it tries to read from /build/src.

So it seems to me there is an incompatibility between tspath and tsc, in how the tsconfig is interpreted. It is not something I would like or know how to solve by running a shell script or adding a build tool like Gulp.

@acathur
Copy link

acathur commented Apr 2, 2019

Hi guys, I have created a pull request to solve this problem. #27

@jdtzmn
Copy link

jdtzmn commented Mar 27, 2020

@duffman Can we get @acathur's PR merged? Are we waiting on something?

@JosephKrusling
Copy link

Since this is still unchanged, I recommend migrating to https://github.com/joonhocho/tscpaths

@duffman
Copy link
Owner

duffman commented Apr 1, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests