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

(fix): added rollup plugin @zerollup/ts-transform-paths, injected as … #374

Closed
wants to merge 2 commits into from

Conversation

ambroseus
Copy link
Contributor

@ambroseus ambroseus commented Dec 15, 2019

…transformer in rollup-plugin-typescript2 config

closes #336

decided to not fix tsdx configs but provide info for solution: #379 (comment)

Edit/Summary From @jaredpalmer:

When using tsdx with larger packages, it is useful to use typescript path aliasing to avoid relative imports. This change allows tsdx (rollup and typescript) to respect such options.

Before

import Button from 'components/Button'
// becomes
const Button = require('components/button') // omitting interop stuff

After

import Button from 'components/Button'
// becomes
const Button = require('./components/button') // omitting interop stuff

@ambroseus
Copy link
Contributor Author

tsdx tests:
Screenshot from 2019-12-15 14-05-33

my monorepo build with fixed version:
https://github.com/DCKit/dckit/blob/master/packages/%40dckit/ui/tsconfig.json
Screenshot from 2019-12-15 14-39-22
Screenshot from 2019-12-15 14-37-18

@swyxio
Copy link
Collaborator

swyxio commented Dec 15, 2019

ok nice work, think this one needs @jaredpalmer to look at it before we merge

@ambroseus
Copy link
Contributor Author

ok nice work, think this one needs @jaredpalmer to look at it before we merge

yep)

meanwhile I've checked official @reduxjs/toolkit build with fixed version

Screenshot from 2019-12-15 18-00-22

Eugene Samonenko added 2 commits December 18, 2019 18:53
Copy link
Collaborator

@agilgur5 agilgur5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a test to ensure this works? Would require running a compiled module that imports something out of src/ to check that it was actually imported. A bit convoluted.

Beyond that, I wonder if this is handled by the parcel playground and storybook template correctly, or if support needs to be added for those too. Not sure, but those can be added later if needed

@@ -50,6 +51,9 @@ export async function createRollupConfig(opts: TsdxOptions) {
tsconfigJSON = fs.readJSONSync(resolveApp('tsconfig.json'));
} catch (e) {}

const pathTransformer = (service: any): any =>
transformPaths(service.getProgram());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be more specific than any? transformPaths is written in TS, so I think it can be.

Also do you know if the second arg isn't necessary? It has an exclude option, not sure if we should pass tsconfigJSON.exclude into there or if it's read automatically (and the exclude option is just an override maybe?? idk the docs aren't comprehensive)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, yea) transformPaths has types LanguageService and CustomTransformer - need to use,
to be fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@ambroseus ambroseus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've build my lib with patched version with paths config, and then check with simple react app - no problem found

but fully agree - need to check any possible combinations and add tests
to be implemented)

@jaredpalmer jaredpalmer reopened this Dec 19, 2019
@jaredpalmer
Copy link
Owner

sorry did not mean to close

@jaredpalmer
Copy link
Owner

jaredpalmer commented Dec 19, 2019

Questions:

  • AFAIK TS transforms are not that popular? Should/can we do this with babel? am i talking out of ass? idk.
  • How/does this impact monorepo's with symlinks and ts paths? For example, you can setup lerna and ts to understand symlinked local packages using tsconfig paths. The result is that that you can have @project/a resolve to packages/a and @project/b resolve to packages/b inside of packages/c. I think that should work fine. However, you can also resolve them to the src of each local package (@project/a resolve to packages/a/src) and then what happens? My guess is that the importing package would need to do the transforms?Thoughts? Maybe that makes sense. idk.

@ambroseus
Copy link
Contributor Author

ambroseus commented Dec 19, 2019

  • AFAIK TS transforms are not that popular? Should/can we do this with babel? am i talking out of ass? idk.

mm.. yeah, I'll check possible solution with babel
(as for me, I like babel-plugin-module-resolver)
Screenshot from 2019-12-19 19-18-49

  • How/does this impact monorepo's with symlinks and ts paths? For example, you can setup lerna and ts to understand symlinked local packages using tsconfig paths

I already use lerna monorepo + tsdx (my patched version with ts-paths plugin) in each package + test CRA app + lerna bootstrap --hoist
https://github.com/DCKit/dckit/blob/master/packages/%40dckit/ui/tsconfig.json
it works)
DISCLAIMER: I use only esm module format

@ambroseus
Copy link
Contributor Author

@jaredpalmer @agilgur5 we can avoid use of transformers in rpts2 and use babel-plugin-module-resolver with paths from tsconfig.json. just compare (checked with 'basic' tsdx template)

Screenshot from 2019-12-19 23-24-46

@ambroseus
Copy link
Contributor Author

ambroseus commented Dec 19, 2019

OMG.. tleunen/babel-plugin-module-resolver#336 (comment)
solution with babel-plugin-module-transformer is not so simple like with ts-transform-paths transformer...

@ambroseus
Copy link
Contributor Author

ambroseus commented Dec 20, 2019

Useful info: https://mitchellsimoens.com/2019/08/07/why-typescript-paths-failed-me/
@agilgur5 there ^^^ is an answer (see Second Issue) why second param with excludes may need

Also do you know if the second arg isn't necessary? It has an exclude option, not sure if we should pass tsconfigJSON.exclude into there or if it's read automatically

@ambroseus
Copy link
Contributor Author

@agilgur5 @jaredpalmer proof with 'storybook' template & ts-transform-paths :)

Screenshot from 2019-12-20 17-15-45

@jaredpalmer
Copy link
Owner

So my question is if this should be part of the tsdx out of the box. Is this really that common of a thing? Seems like it is niche? Someone could easily just publish a “plugin” that adds this and we could link to it

@ambroseus
Copy link
Contributor Author

So my question is if this should be part of the tsdx out of the box. Is this really that common of a thing? Seems like it is niche? Someone could easily just publish a “plugin” that adds this and we could link to it

@jaredpalmer I know many developers who use aliases plugins to deal with "../../../" hell.. But also agree that adding yet another plugin for plugin for plugin in the core is not excellent idea..

so.. I reject my PR and add comment to HOWTOs how to setup aliasing (@sw-yx it would be good to pin HOWTOs issue.. :)

@ambroseus ambroseus closed this Dec 21, 2019
@ambroseus ambroseus deleted the fix-tsconfig-paths branch December 21, 2019 17:48
@ambroseus ambroseus mentioned this pull request Dec 21, 2019
@agilgur5
Copy link
Collaborator

agilgur5 commented Dec 28, 2019

So just to summarize:

OMG.. tleunen/babel-plugin-module-resolver#336 (comment)
solution with babel-plugin-module-transformer is not so simple like with ts-transform-paths transformer...

  1. babel-plugin-module-resolver is a lot more complex than ts-transform-paths

Useful info: https://mitchellsimoens.com/2019/08/07/why-typescript-paths-failed-me/

    1. TS paths are not meant to be used like this per Module path maps are not resolved in emitted code microsoft/TypeScript#10866 (comment) .
    2. Adding a transformer like ts-transform-paths can cause even more issues listed in that blog post, summarized at the end as: "I’m not saying paths are bad just if you’re gonna use them in a way they are not intended to be used, expect a bumpy road".
    3. tscpaths and ts-transform-import-path-rewrite are some alternatives (there are more), but seem more immature than ts-transform-paths, at least afaict from quick glance.

So babel-plugin-module-resolver actually seems to be a more appropriate use than a TS transform, but neither should really change the usage of paths as they shouldn't be used like that.

In either case, it seems like trying to resolve paths is more of a hassle than worthwhile, and potentially a very fragile feature, so I think I'd agree that it should be left to user-land as a result. That being said, as it is a common use case, I do think there should be an easier way to configure it.

As objectHashIgnoreUnknownHack of #294 et al is also a common use case, it probably makes sense more generally to expose the rpts2 config somehow so it can be modified without having to straight copy the source code (which is brittle as that will change). Wrapping rpts2 in a plugin like I mentioned in #389 (comment) might make overriding it a bit easier, but maybe there's another method that makes sense for this as well.

@0vidiu
Copy link

0vidiu commented Jan 7, 2020

Copy/pasta snippet incoming for people in need:

// babel.config.js
module.exports = {
  // ...
  plugins: [
    // ... other plugins
    [
      'babel-plugin-module-resolver',
      {
        root: './',
        alias: require('./tsconfig.json').compilerOptions.paths,
      },
    ],
  ],
};

This works wonderfully in combination with TsconfigPathsPlugin (Webpack plugin). I declare my aliases in tsconfig.json and they work in Webpack, Storybook, Babel (for tests), while it also keeps VSCode happy. Hope this helps someone.

@ambroseus
Copy link
Contributor Author

thanks @0vidiu, added to HOWTOs #379 (comment)

@slorber
Copy link
Contributor

slorber commented Mar 31, 2020

thanks @0vidiu @ambroseus

This workaround works fine for the JS output, but the typedefs are emitted in different files, and they still have the absolute links, making the typedef files unable to resolve each others. Any idea how to solve it?

@jensbodal
Copy link

The https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths transformer should handle type files properly from what their docs say

@sujingclg
Copy link

sujingclg commented Feb 16, 2022

Copy/pasta snippet incoming for people in need:

// babel.config.js
module.exports = {
  // ...
  plugins: [
    // ... other plugins
    [
      'babel-plugin-module-resolver',
      {
        root: './',
        alias: require('./tsconfig.json').compilerOptions.paths,
      },
    ],
  ],
};

This works wonderfully in combination with TsconfigPathsPlugin (Webpack plugin). I declare my aliases in tsconfig.json and they work in Webpack, Storybook, Babel (for tests), while it also keeps VSCode happy. Hope this helps someone.

It seems not working at "babel-plugin-module-resolver": "^4.1.0", and can be fixed by changing babel.config.js as follow.

module.exports = {
  plugins: [
    [
      'module-resolver',
      {
        alias: {
          "@": "./src"
        }
      },
    ],
  ],
}

the key of resolving this problem is changing alias value from array to string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: TS Paths Aliases Related to using aliases with TypeScript paths
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using TS Paths as aliases
8 participants