-
Notifications
You must be signed in to change notification settings - Fork 71
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): declaration maps should have correct sources #221
Conversation
Sorry for ignoring this for a while -- this breaks something -- if you build the plugin itself with this change, it drops types into To test do |
So I had to learn a lot about Rollup plugins and lifecycle to write this PR. Then without further guidance here had to do a lot of learning of this codebase and the TS API. As a result, it took me a while to understand how to fix this. Debugging the PR
I figured out why this was happening relatively quickly, that's because
I added some logging during output that displays this as well. While entry name: /Users/agilgur5/Desktop/GitHub/rollup-plugin-typescript2/node_modules/.cache/rollup-plugin-typescript2/placeholder/partial.d.ts.map
dtsmap entry text: {"version":3,"file":"partial.d.ts","sourceRoot":"","sources":["../../../../src/partial.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,MAAM,OAAO,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;CAAG,CAAC"} That can't be changed to Rollup's Current PR doesn't workIn the current PR, the changes apparently have no effect on the output because they change rollup-plugin-typescript2/src/index.ts Line 261 in 2f51110
Trying different solutionsBetween March - June-ish, I came back to this a few times to add more logging and understand this as well as to try to somehow set The rollup-plugin-typescript2/src/index.ts Line 114 in 2f51110
I thought to lazily initialize this, but that's not possible, it's needed in the next plugin lifecycle steps, especially It can't be done in But Rollup is designed that way intentionally, any output transformations should be left until SolutionBasically the only option is to transform the declaration maps' I checked map = JSON.parse(text) as SourceMap;
map.file = options.declarationPaths.fileName; So I'm going to rework this PR to have a similar approach and transform Other issuesBut TSDX is having a lot of other issues with |
f107eba
to
a325fca
Compare
// don't mutate the entry because generateBundle gets called multiple times | ||
let entryText = entry.text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this took me a few hours to debug and was by far the most time-consuming piece 😕 I originally changed entry.text
itself which then got changed on top of itself for each output...
Ideally I think generateBundle
should have some caching so this isn't repeated per output. I could do that with a cache key inside of declarations{}
, but there's a good question as to what that cache key should be (in this case, I think the declarationDir
variable is the only thing that is subject to change, but that sounds brittle if more stuff gets added).
a325fca
to
ec0568b
Compare
- previously, declarationDir was set to cwd if useTsconfigDeclarationDir wasn't true, however, declarations aren't output to cwd, but to Rollup's output destination, so this was incorrect - instead, don't set declarationDir, which defaults it to outDir, which is currently set to a placeholder - previously, it rewrote declarations to output to Rollup's dest from cwd, now rewrite from outDir placeholder instead - and add a rewrite of sources to match relative path from Rollup's output dest instead of outDir placeholder - also change the one line in the docs that says it'll be `process.cwd()`; every other reference says it'll be the output dest
@ezolenko this should be good to go now -- if you run As I wrote in my opening comment, a test set-up like #135 would be great to have; could add the test I have in jaredpalmer/tsdx#488 here then. |
@agilgur5 looks good now. The only problem I see is sources in type maps are in windows format if I build it on windows: nvm, fixed it |
Ah, I don't run on a Windows machine, but if that's not normal behavior (wouldn't know), then could use |
@agilgur5 Ok, PR is in master, could you doublecheck and I'll do a release |
Looks like you added I saw you added On |
@ezolenko any update on the above or a release? |
@ezolenko you merged some of my other PRs, but didn't comment on the above and there still hasn't been a release 😕 |
Sorry, was distracted. When you run npm install it will pull patch updated of deps and I that might affect source maps a bit (maybe line number changed because of comments or something). I'll do a release later today. |
What about this part?
|
Re normalize for emit file, seems to have no effects either way, rollup handles it fine, but might affect downstream plugins that want to modify maps. Normalize is more correct in principle though, so I guess we should keep it and see if problems show up. |
Ok, yea I read in some Rollup issues (can't find which one it was) that it allows whichever internally, which means plugins have to handle and do the normalization. rollup/rollup#2952 suggests this is particularly relevant for |
- fancy source maps for declarations - apparently I left these out of `ts-library-base` so they didn't get copied here either - but most of my tsconfig there was copied too, so I suspect I left it out either because of @wessberg/rollup-plugin-ts's bugs with it or because TSDX previously had bugs with it - c.f. ezolenko/rollup-plugin-typescript2#221 and the worse downstream version I had written first: jaredpalmer/tsdx#488 - Unfortunately I did encounter a small error with declaration maps when using rpts2 as a configPlugin, due to an unexpected edge case in code I wrote myself in the above PR - wrote up an issue for it and will probably PR it myself too: ezolenko/rollup-plugin-typescript2#310 - as a workaround, I wrote a small `tsconfig.rollup.json` to be used with rpts2 as a configPlugin that disables `declarationMap` - and also adds some optimizations over my base tsconfig - took me a bit to figure out how to use options with configPlugins, as that was one of the reasons I didn't use @rollup/plugin-babel as the configPlugin - I still can't get it to read my `babel.config.js` even with options as it has no option for this (it auto-detects it) :/
- fancy source maps for declarations - apparently I left these out of `ts-library-base` so they didn't get copied here either - but most of my tsconfig there was copied too, so I suspect I left it out either because of @wessberg/rollup-plugin-ts's bugs with it or because TSDX previously had bugs with it - c.f. ezolenko/rollup-plugin-typescript2#221 and the worse downstream version I had written first: jaredpalmer/tsdx#488 - Unfortunately I did encounter a small error with declaration maps when using rpts2 as a configPlugin, due to an unexpected edge case in code I wrote myself in the above PR - wrote up an issue for it and will probably PR it myself too: ezolenko/rollup-plugin-typescript2#310 - as a workaround, I wrote a small `tsconfig.rollup.json` to be used with rpts2 as a configPlugin that disables `declarationMap` - and also adds some optimizations over my base tsconfig - took me a bit to figure out how to use options with configPlugins, as that was one of the reasons I didn't use @rollup/plugin-babel as the configPlugin - I still can't get it to read my `babel.config.js` even with options as it has no option for this (it auto-detects it) :/
change declarationDir default to be Rollup's output destination
instead of cwd
true, however, declarations aren't output to cwd, but to Rollup's
output destination, so this was incorrect
also change the one line in the docs that says it'll be
process.cwd()
; every other reference says it'll be the output destFixes #204 and downstream jaredpalmer/tsdx#479 . Better solution to downstream jaredpalmer/tsdx#488
Couldn't change
get-options-overrides
because theoptions
hook only getsInputOptions
; it's not tillgenerateBundle
that we get access toOutputOptions
.RE: multiple output dirs: Rollup will call
generateBundle
for each output as far as I understand.InputOptions
is passed torollup
which generates abundle
object, andbundle.write
is called with eachOutputOptions
-- it's a two step+ process. I think that's also why the type isOutputOptions
and notOutputOptions[]
(though the code doesn't use Rollup'sPlugin
interface; but that also defines it asgenerateBundle
with justOutputOptions
).Would be great to add tests to ensure the declaration maps are correct now, but there doesn't seem to be any test set-up 😕 #135 seems to partially address that but was never completed.
Can see jaredpalmer/tsdx#488 for how I tested
sources
there