Closed halo951 closed 1 year ago
and it is normal in <=v0.33.0
Thanks for the repro, I was able to successfully reproduce in 0.34.0
and not in 0.33.0
, getting this error:
~/projects/rpt2-repro-h4yxsj 1s
β― npm run build
$ rollup --config ./rollup.config.ts --configPlugin typescript2
[!] (plugin rpt2) Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
package.json (2:8)
1: {
2: "name": "rpt2-repro",
^
3: "version": "0.0.0",
4: "scripts": {
at error (/home/projects/rpt2-repro-h4yxsj/node_modules/rollup/dist/shared/rollup.js:198:30)
at Module.error (/home/projects/rpt2-repro-h4yxsj/node_modules/rollup/dist/shared/rollup.js:12553:16)
at Module.tryParse (/home/projects/rpt2-repro-h4yxsj/node_modules/rollup/dist/shared/rollup.js:12930:25)
at Module.setSource (/home/projects/rpt2-repro-h4yxsj/node_modules/rollup/dist/shared/rollup.js:12835:24)
at ModuleLoader.addModuleSource (/home/projects/rpt2-repro-h4yxsj/node_modules/rollup/dist/shared/rollup.js:22309:20)
0.34.0
shouldn't have changed this behavior though -- just added more type-checking and declaration generation for type-only files, so I'm actually not sure what broke... Here's the diff:
https://github.com/ezolenko/rollup-plugin-typescript2/compare/0.33.0.1...0.34.0#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80L208
I'll have to investigate this more when I get the time, nice catch on this change between 0.33.0
and 0.34.0
!
Thank you for your reply. I went to see what caused the mistake today
So I added some verbosity: 3
logging that was missing from your issue and repro:
"scripts": {
"build": "rollup --config ./rollup.config.ts --configPlugin \"typescript2={ verbosity: 3 }\"",
},
and the strange part is that the logs between versions are virtually identical (outside of version number, cache hash, and the error ofc, everything else is identical):
0.34.0
0.33.0
There is also no "resolving" being done in either log (and the resolveId
hook did not change in 0.34.0
), so this seems to be hit in the transform
hook.
The strange part there is that rpt2 shouldn't be transforming any JSON, since it's not in the default include
/exclude
. So upon first glance, the error seems correct. But it doesn't appear in 0.33.0
for some reason. π€
I have a feeling this is an odd edge-case for configPlugin
s in particular, where Rollup might handle JSON for them (or just not touch JSON at all, since configPlugin
s run in Node, which can interpret JSON itself). 0.34.0
forces a load
of all references
(this should be all imports basically) though, so maybe this is triggering an edge-case bug in Rollup itself because of that. I'll have to do a bit more investigating and add additional logging to confirm
I have a feeling this is an odd edge-case for
configPlugin
s in particular, where Rollup might handle JSON for them (or just not touch JSON at all, sinceconfigPlugin
s run in Node, which can interpret JSON itself).0.34.0
forces aload
of allreferences
(this should be all imports basically) though, so maybe this is triggering an edge-case bug in Rollup itself because of that. I'll have to do a bit more investigating and add additional logging to confirm
I did some additional logging and experimentation, and this hunch of mine seems to be roughly correct: the additional this.load
that was added in 0.34.0
to handle type-only files also runs on this JSON file. Skipping it for this file makes the error no longer appear.
We could potentially skip this.load
on JSON files, but this seems like a possible (likely) bug in Rollup's this.load
implementation, where it's not quite handling JSON the same way for configPlugin
s as the Rollup CLI does.
The addition of this.load
shouldn't have changed any behavior as Rollup should have loaded this file either way, as it's present in the JS. So that's why this seems like a Rollup bug to me.
For reference, here's the JS that rpt2 outputs:
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';
const banner = `/** ${pkg.name} */`;
export default () => {
return {
input: './src/index.ts',
output: {
banner,
exports: 'auto',
inlineDynamicImports: true,
format: 'cjs',
file: `dist/index.cjs.js`,
},
plugins: [
typescript({
clean: true,
useTsconfigDeclarationDir: true,
abortOnError: true,
}),
],
};
};
This is correct, so that suggests this isn't an rpt2 bug, but an upstream Rollup bug.
I'm also not 100% sure that rpt2 skipping JSON files would be valid behavior. A theoretical scenario, for instance, would be if another plugin were to transform the JSON to a type-only TS file, which would be a case rpt2 should handle. I need to think a bit more on this as this scenario might actually still be handled (in a different way)... π€
This is a very hacky workaround, but could also do:
const pkg = JSON.parse(await fs.readFile('./package.json', 'utf8'));
as this SO answer does (for a more detailed example).
(or ofc, add @rollup/plugin-json
as another configPlugin
)
I'm also not 100% sure that rpt2 skipping JSON files would be valid behavior. A theoretical scenario, for instance, would be if another plugin were to transform the JSON to a type-only TS file, which would be a case rpt2 should handle. I need to think a bit more on this as this scenario might actually still be handled (in a different way)... π€
I dug into this a bit, in particular, re-reading my initial logic and root cause analysis of the type-only issue, and I actually think this should be feasible.
Based on my logic there, this.resolve
/ this.load
was mainly necessary so that all the imports (references
in TS) go through all plugins + external
s and for recursive purposes. So, if I added some preemptive filtering, that should be fine, so long as the this.resolve
/ this.load
still happen afterward.
Basically, this.resolve
should only be removing references, so removing more preemptively shouldn't break that logic
So I think adding a fix to filter early should be ok.
I also use rpt2 as a configPlugin
and import package.json
in my rollup.config.ts
, so I think that's a pretty common (and personal) use-case to ensure works. Might also add some dogfooding here for that.
Looks like this has been coincidentally fixed by #451. By coincidence, this line ends up fixing this regression as well because it makes the exact same fix I outlined above π
.
Per https://github.com/ezolenko/rollup-plugin-typescript2/issues/446#issuecomment-1625859100, I actually made a very similar change in October, but unfortunately had pretty significant trouble getting automated configPlugin
tests to work (via dogfooding in this commit), so as a result, unfortunately never completed that work π
Running the repro with 0.35.0
has no more errors, so marking this as resolved now π
Will still be looking to merge in part of my branch there in order to automatically test and future-proof the configPlugin
functionality.
Troubleshooting
Does
tsc
have the same output? If so, please explain why this is incorrect behaviorno
Does your Rollup plugin order match this plugin's compatibility? If not, please elaborate
matched
Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction
https://stackblitz.com/edit/rpt2-repro-h4yxsj?file=rollup.config.ts
What happens and why it is incorrect
In the
rollup.config.ts
, use theimport pkg from 'package.json'
statement, and the plug-in does not handle. and it is normal in <=v0.33.0Environment
Versions
v0.34.0