-
-
Notifications
You must be signed in to change notification settings - Fork 431
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
webpack 5 migration #1251
webpack 5 migration #1251
Conversation
Would this be a good point to drop support for experimentalWatchApi? As far as I can see it has never worked and it adds complexity to the code. |
Don't forget to update webpack-cli to 4.5.0 (I think it's the latest). |
Go for it @appzuka - added to checklist above |
Good shout - added to checklist above |
@johnnyreilly How do you normally run the loader when you are working? Do you have an example project you are running locally where you build with a local copy of the loader? In short: What's your most optimized workflow? Edit: I cloned a simple Webpack 4 + Typescript repo, upgraded all deps and added a symlink to my local ts-loader clone. Should be enough I think. |
@johnnyreilly: How do you wanna handle these scenarios? |
That seems like a legitimate workflow @JonWallsten - to your question about the fix: yeah maybe. Question: is it legitimate that a module may be a |
@johnnyreilly / @appzuka: Am I correctly to assume that the first images shows where we ask TypeScript to compile and the second we pass those assets to Webpack? Edit: So it seems like the ts-code is emitted in the first image and the |
According to the debugger, the current module I'm looking at is a |
If that's what the debugger says, I trust it! |
I can't promise that all modules are NormalModules, but if we trust the types, the |
I put some logging statements to clarify where assets are transpiled and emitted. When running on a small project (index.ts with sub.ts as a dependency) I get the following output:
I believe the program.emit statement in getEmitOutput is where the output from the TypeScript compiler is obtained. The input is a single .ts file and the output is a .js file and .d.ts and .d.ts.map files, if required. The .js file is returned to webpack as the result of the compilation. This happens until all .ts files have been processed. Then, during the processAssets hook, makeAfterCompile goes through all the files the TypeScript instance viewed, gets the outputs files from program.emit again, and adds .d.ts and .d.ts.map files as assets. It seems inefficient that program.emit is called multiple times to get the output from .ts files, but I believe the output is cached so on subsequent calls it is just hitting the cache. On this small example, each asset is only emitted to webpack once. So it seems that the asset output caching recommended by @alexander-akait may not be required. However, when I run this in watch mode the assets are emitted even if their contents have not changed, so there could be a benefit in implementing a cache around the asset output in watch mode. There could also be a benefit in more complex projects. |
@appzuka: Thanks for the explanation! Do we know why the files are emitted in different steps? Is it supposed to be like that or just legacy? |
Hey @sokra I hope you are keeping well! I seem to remember you put together a doc which detailed the API changes between webpack 4 and 5 targeted at maintainers of loaders and plugins. We're finally getting round to building Do you happen to remember where that doc lives? For example we're trying to track down All help gratefully received! |
I understand why the js is returned separately from the .d.ts files but not why they are returned at different stages. A simple loader as described at https://webpack.js.org/contribute/writing-a-loader/ just takes an input file and transforms it to a string that is valid Javascript. In the case of ts-loader the input file is a .ts file, which is transformed into Javascript but also .d.ts and other files need to be added to the assets in the output directory. The Javascript output by typescript is not emitted as an asset but rather passed back to webpack for inclusion in the bundle. What is not clear to me is why the .d.ts and other files are emitted together when all the .ts files have been processed rather than being emitted as assets during the getEmitOutput. It would make things simpler as we would not need the compilation/processAssets hooks at all, but perhaps there is a reason it was done this way. It seems to me that processAssets is not really the right place to generate the assets. In the Terser example processAssets was used but then Terser is a plugin which processes assets generated by other plugins and loaders. In ts-loader we are not processing the assets, we are creating them. I'm wary of changing the architecture in case we are repeating a mistake someone fixed several years ago, although simplifying ts-loader may also help improve performance and reduce errors. |
I have a feeling that there was work done around this but it was a long time ago and frankly I don't remember the details. I'm open to changes being made but it's possible there's a reason things are the way they are now and I'm just not recalling |
It kind of make sense though that the d.ts and d.ts.map files are concidered additional files since they are not part of the bundle and Webpack is not built around TypeScript. So even though they are vital to us, it's not vital to Webpack in general. Or am i missing something? |
Sorry I missed this @JonWallsten - I don't quite follow the question. Incidentally, it turns out that webpack isn't exporting all types... So some we'll have to hold onto ourselves |
Okay - the code compiles now; I borrowed a bunch of stuff from your branch @JonWallsten - thanks! No tests pass alas |
Execution tests passing! (or it looks like it!) |
This is like jeopardy. You give the answer I ask the question! 😂 Why is ts-files and d.ts/d.ts.map files emitted in different places in the code and Webpack lifecycle. I would say it's because declaration files are not part of the "code" in that sense, it's additional bonus stuff that will end up as separate files, not part of the bundle, in contrast to the transpiled/compiled (which is it?) code and the sourcemaps. |
Did you have a chance to try it out @JonWallsten? All feedback welcome! |
Sorry! Got stuck with build issues in our project yesterday. I will try it out today instead. |
I have tried to build a prod build, dev builds with |
I guess we will get additional feedback once released! :) |
@johnnyreilly , @JonWallsten, I don't believe I have contributed to this branch but I built a couple of my projects with the latest feat/webpack-5 branch today. They worked fine and I did not notice any performance or size issues. Well done. |
Thanks @appzuka (very pleased that you gave it a test!) |
// Consider changing this when ts-loader is built using webpack5 | ||
}; | ||
// It may be better to add assets at the processAssets stage (https://webpack.js.org/api/compilation-hooks/#processassets) | ||
// This requires Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, which does not exist in webpack4 |
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.
@johnnyreilly: I forgot about this. I think it was part of my PR when I sidetracked with the GitHub Workflow thing.
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.
Let's fix that in a minor later!
Cool - I'll finish writing up the blog post and we'll ship ⛴️ |
@johnnyreilly: Woho! Super good work @johnnyreilly! 🏆Sorry I couldn't be more helpful the last couple of weeks! |
Thanks @JonWallsten - I appreciate the help that you gave! |
This PR will be where the migration of ts-loader to webpack 5 takes place. Help is appreciated!
enhanced-resolve
,webpack-cli
,webpack
)drop support for experimental watch API- see webpack 5 migration #1251 (comment)CHANGELOG.md
README.md
cc @alexander-akait @appzuka @JonWallsten - all advice and contributions towards this are gratefully received.
Feel free to PR or push into this branch to work on this.
Some discussion here: webpack/webpack#12475 (reply in thread)
UPDATE
Okay I think we're ready to ship. I'm going to: