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

Watcher not working using TypeScript? #72

Closed
kaosdev opened this issue Mar 27, 2021 · 13 comments · Fixed by #74 · May be fixed by YonatanKra/karma-rollup-preprocessor#1
Closed

Watcher not working using TypeScript? #72

kaosdev opened this issue Mar 27, 2021 · 13 comments · Fixed by #74 · May be fixed by YonatanKra/karma-rollup-preprocessor#1

Comments

@kaosdev
Copy link

kaosdev commented Mar 27, 2021

Seems like that karma auto watch break when using karma-rollup-preprocessor.
The tests are run when i change something, but with no effect.
Indeed if I make a test fail, I see karma running but it will state that tests passes.

It may be a configuration problem, so here I made a small reproduction of the issue https://github.com/kaosdev/karma-rollup-preprocessor-watch-issue

I saw there are other issue related to auto watch not working, but this seems different, and as far as I understand this feature changed recently (maybe with 7.0.4?).

@jlmakes jlmakes changed the title auto watch not working Watcher not working using TypeScript? Mar 28, 2021
@jlmakes
Copy link
Owner

jlmakes commented Mar 28, 2021

Thanks for the reproduction. I removed TypeScript from the project and everything works normally. That narrows things.

@kaosdev
Copy link
Author

kaosdev commented Mar 28, 2021

Can confirm that, using babel watch works correctly. After digging a while I found that @rollup/plugin-typescript uses an internal watcher, maybe this is causing some errors?

@jlmakes
Copy link
Owner

jlmakes commented Mar 30, 2021

karma-rollup-preprocessor has its own watcher that manually re-runs Karma, but presently I don't deeply understand how Karma goes about generating/serving temporary files. Although, if it works without the TS plugin, it makes me think the problem lies in how @rollup/plugin-typescript names its output files, perhaps related to the file watcher or a caching strategy. 🤔

There is a bit of file name shuffling in order to get source maps working, and to account for file extension changes. If there is a fix that can be implemented in preprocessor land (as opposed to in the plug-in itself) it likely has to do with that.

There are other users that have been using TypeScript though: https://github.com/jlmakes/karma-rollup-preprocessor/blob/master/CHANGELOG.md#fixed-6 but it looks like it was done using Babel.

@MMMalik
Copy link

MMMalik commented Apr 20, 2021

I can confirm that there is an issue with watching files and official TS plugin @rollup/plugin-typescript. Switching to @rollup/plugin-babel helped in my case:

My plugins inside rollupPreprocessor in karma.conf.js:

plugins: [
	progress(),
	babel( {
		babelHelpers: 'bundled',
		presets: [
			'@babel/preset-env',
			'@babel/preset-typescript',
			'@babel/preset-react'
		],
		extensions: [ '.ts', '.tsx', '.js' ],
		exclude: 'node_modules/**'
	} ),
	nodeResolve( {
		preferBuiltins: false,
		extensions: [ '.ts', '.tsx', '.js' ]
	} ),
	commonJs(),
	polyfillNode()
],

With babel plugin it's just transpilation (no type checking) but I don't think it's a big deal in case of generating test bundle.

@xenobytezero
Copy link

xenobytezero commented Oct 6, 2021

So I have been spending a few days looking at this and it looks like it's partially related to @rollup/typescript and partially down to the structure of this preprocessor.

After diving through the @rollup/typescript code, it looks like the plugins "emitted files" cache is maintained at the plugin instance level, not per Rollup run. Since this preprocessor calls rollup.rollup() multiple times with the same config, the instance of the plugin is maintained as does the cache, hence every time the file changes the previously compiled one is emitted again.

I assume something different happens when running rollup.watch() as my main build (using an identical rollup config) works fine.

Is there a way to modify the preprocessor to use rollup.watch() and its internal watcher, while still saying to Karma "hey stuff has changed, you should do what you need to" in the same way as refreshFiles does?

I'm going to keep messing around with this on my end to see if I can make it work, but any pointers would be great.

@xenobytezero
Copy link

Follow up, after doing an ugly version of the processor that uses rollup.watch() rather than rollup.rollup() I am getting the output I expect, where the files are rebuilt and refreshing the debug page gives the updated files/test results.

It's very specific to my setup right now but it's doing what I need so I will clean it up and if people need it I can post.

@jlmakes
Copy link
Owner

jlmakes commented Nov 7, 2021

@xenobytezero I'd be curious to see your changes. If you have the time, please submit a PR.

@xenobytezero
Copy link

The changes are tied up in code it's a little tricky to open source right now, will see what I can do.

@YonatanKra
Copy link
Contributor

YonatanKra commented Nov 14, 2021

@jlmakes I made it work.
I can't find the logic in the tests in order to write tests for it.
How do the tests work? All I see is a bunch of modules that import each other.
Please see this change: #74

YonatanKra added a commit to YonatanKra/karma-rollup-preprocessor that referenced this issue Nov 14, 2021
@YonatanKra
Copy link
Contributor

@jlmakes - do you think you could integrate this into the package?

jlmakes pushed a commit that referenced this issue Nov 22, 2021
@mattalxndr
Copy link

Just spend an hour solving this. My mistake for not looking further after noticing that my version matched your package.json at master.

Are you going to release soon?

@jlmakes
Copy link
Owner

jlmakes commented Feb 7, 2022

Thanks for your patience, the fix landed in 7.0.8 @mattalexx @YonatanKra

@mattalxndr
Copy link

Thanks @jlmakes

cc @Gusted

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