{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9017","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9017/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9017/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9017/events","html_url":"https://github.com/Microsoft/TypeScript/issues/9017","id":159091306,"number":9017,"title":"Suggestion: CompilerHost should be able to \"plug into\" a LanguageServiceHost ","user":{"login":"TheLarkInn","id":3408176,"avatar_url":"https://avatars.githubusercontent.com/u/3408176?v=3","gravatar_id":"","url":"https://api.github.com/users/TheLarkInn","html_url":"https://github.com/TheLarkInn","followers_url":"https://api.github.com/users/TheLarkInn/followers","following_url":"https://api.github.com/users/TheLarkInn/following{/other_user}","gists_url":"https://api.github.com/users/TheLarkInn/gists{/gist_id}","starred_url":"https://api.github.com/users/TheLarkInn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheLarkInn/subscriptions","organizations_url":"https://api.github.com/users/TheLarkInn/orgs","repos_url":"https://api.github.com/users/TheLarkInn/repos","events_url":"https://api.github.com/users/TheLarkInn/events{/privacy}","received_events_url":"https://api.github.com/users/TheLarkInn/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/API","name":"API","color":"bfe5bf"},{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/In%20Discussion","name":"In Discussion","color":"556b75"},{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Suggestion","name":"Suggestion","color":"006b75"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-06-08T06:49:28Z","updated_at":"2016-06-08T21:45:11Z","closed_at":null,"body":"### Background\r\nThe traditional method of transpiling a typescript application is through `ts.CompilerHost`. This is awesome, and there are slowly becoming more and more CompilerHost based [\"flavors\"](https://github.com/angular/angular/blob/master/tools/%40angular/tsc-wrapped/src/compiler_host.ts) of `tsc` that developers are wanting to use. I spoke about this with @alexeagle and @mhegazy briefly on this as well:\r\n\r\n### Webpack Loaders\r\nA webpack loader in its most simple form does the following: \r\n```javascript\r\nfunction takeASingleFileThatIsServedToThisFunctionViaWebpack(rawFileStringContent) {\r\n  let newlyUpdatedRawFileString = rawFileStringContent + 'console.log(WOW THIS MAKES SENSE);'\r\n\r\n\r\n  return newlyUpdatedRawFileString;\r\n}\r\n```\r\nAs you can see all a loader does is take a raw source, and return it. On top of this webpack handles file resolution through some crafty regex:\r\n\r\n```\r\nloaders: [\r\n  {\r\n    test: /\\.ts/,  // this regex string tells webpack which files to 'send' to the loader\r\n    loader: ‘ts-loader’ // this specifies the loader to use (resolves to node modules)\r\n  }\r\n]\r\n```\r\n\r\n### Webpack Typescript Loaders Cannot & Should Not Use `ts.CompilerHost`\r\nSo knowing what we know about how a file is tranformed and resolved through webpack, `ts.CompilerHost` is not an option for a ts-webpack loader. _Why?_ Loaders are a powerful tool in webpack because they can be chained together with other loaders. _For example: [This Angular2 TS Loader I wrote](http://github.com/thelarkinn/angular2-template-loader)_. CompilerHost (as you know probably) runs a build/transpiliation against a whole project, and not one file per time. Therefore if you tried to plop a CompilerHost in a webpack loader, it would lose the context between each file.\r\n\r\n### Webpack TS Loaders & `ts.LanguageServiceHost`. \r\nBecause of this, the current typescript webpack loaders that are being used (ts-loader, awesome-typescript-loader) are both using `ts.LanguageServiceHost`. _Why?_ Because LSG's support transpilation at a long-term lifetime one-file-at-a-time transpilation, persisting the transpile context back to the `ts.Program`. (This is tranditionally useful for IDE'S yes, but its a match made in heaven for a webpack loader). I would call this an 'edge-case' request, however, with 5+ million downloads a month, webpack is increasingly more popular than any bundler at the moment. \r\n\r\n### The Problem: \r\nTrying to use `ts.CompilerHost` implementations in a `ts.LanguageServiceHost` is a huge pain, and doesn't port well into an existing LSH. Its true that they share some functions, however there lacks a way to use a CompilerHost in a LanguageService incrementally.  \r\n\r\n### The Solution.  \r\nCreate some way to attach the functionality of a custom CompilerHost implementation, and then perform it on a per-file basis, updating the `LanguageServiceHost ` to help persist the transpilation/program contex. This will allow not only @s-panferov and @TypeStrong able to create plugin's systems that allow for super powerful TS builds in webpack. Or instead a plugin system where you add/override certain functionalities to an existing LSH.\r\n\r\n### Please refer to this conversation thread of me explaining the issue to @DanielRosenwasser with lots of examples:\r\nStart: https://gitter.im/Microsoft/TypeScript?at=5757ab04662b042b7e596b70 \r\nEnd: https://gitter.im/Microsoft/TypeScript?at=5757b77445cf128e5f1d7429\r\n\r\n### References \r\nhttps://github.com/Microsoft/TypeScript/issues/6508#issuecomment-224464201\r\nhttps://github.com/angular/angular/issues/8759\r\nhttps://github.com/TypeStrong/ts-loader/issues/223\r\nhttps://github.com/TypeStrong/ts-loader/issues/\r\nhttps://github.com/s-panferov/awesome-typescript-loader/issues/153\r\nhttps://github.com/TypeStrong/ts-loader/issues/224"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/224738960","html_url":"https://github.com/Microsoft/TypeScript/issues/9017#issuecomment-224738960","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9017","id":224738960,"user":{"login":"TheLarkInn","id":3408176,"avatar_url":"https://avatars.githubusercontent.com/u/3408176?v=3","gravatar_id":"","url":"https://api.github.com/users/TheLarkInn","html_url":"https://github.com/TheLarkInn","followers_url":"https://api.github.com/users/TheLarkInn/followers","following_url":"https://api.github.com/users/TheLarkInn/following{/other_user}","gists_url":"https://api.github.com/users/TheLarkInn/gists{/gist_id}","starred_url":"https://api.github.com/users/TheLarkInn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheLarkInn/subscriptions","organizations_url":"https://api.github.com/users/TheLarkInn/orgs","repos_url":"https://api.github.com/users/TheLarkInn/repos","events_url":"https://api.github.com/users/TheLarkInn/events{/privacy}","received_events_url":"https://api.github.com/users/TheLarkInn/received_events","type":"User","site_admin":false},"created_at":"2016-06-08T21:45:11Z","updated_at":"2016-06-08T21:45:11Z","body":"CC @s-panferov @jbrantly, please chime up if I'm not explaining this right. \r\n\r\n@mhegazy so I said above that webpack is responsible for serving individual files *one at a time* to a loader. In that case, you can't use CompilerHost on one TS file at a time (because you lose program context between transpilation).\r\n\r\nLSH provides the ability to build up your Program of files, and then additionally update them only compiling that one file in the context of the Program. Is there something I am missing? "}}
{"actor":{"display_login":"TheLarkInn"}}