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 plugin #5
Comments
Thank you! I already thought about providing a webpack wrapper around ts-runtime. However, at this point I don't really have time to investigate, but I'm open to any discussions, suggestions and pull requests. By the way, the import But I definitely see the benefits of your proposal! |
Awesome. I don't need it super urgently, but may take you up on submitting a PR at some point in the future if it hasn't already been done by someone else and/or been implemented in the Angular CLI by then.
Ah, yeah, thanks for clarifying that. I'd misunderstood "On top of every source file, the runtime type checking library is imported" as "... must be imported by you", not "... is imported as one of the following list of transformations applied by |
Okay, great! Just to make it clearer, when transforming a file with the following code: let a: string; this is what you get by default: import './tsr-declarations';
import _t from 'ts-runtime/lib';
let _aType = _t.string(), a; What I'll have to change is, that Just noticed, that I will also have to align the CLI options with the API options. The project is still work in progress, but I'm glad for any feedback or issues spotted :) |
any update on this? I would love to see a webpack integration (or even advice on how to integrate the existing |
I will try to provide a guide on how to integrate this project in a webpack process soon. Please also see #19. |
I just cam across ts-runtime-loader, didn't check it, but does it work for you? |
so this does work to a degree, however its doing some really weird things behind the scenes (partially because of how ts-runtime works). It actually patches node's This would allow me (or anyone) to create a loader more in line with webpacks design principles of modularity and simplicity. This would keep different runtime files separated, and allow webpack to handle de-duping modules on its own. As another bonus, it could work with |
I see — as mentioned before, I didn't check the code or try it out. The thing with string in/string out is a bit tricky with ts-runtime, since it relies on resolving types, to be able to create the correct runtime type checks. This means that we have to provide the full project, either via an entry file from the file system, or via a representation of it via The latter could probably be used with webpack. |
By the way, both transform functions, |
The latter does sound like a possible solution with webpack. Loaders are supposed to be stateless, but it would be possible to read all files once, then reuse that for the remainder of the build. I dont believe there is a way around this, since webpack only expects one file in and one file out. This leads to another issue though, which is, how will webpack know about the other files that are created by transform? Depending on what is required by a transformed file, it could have created
// index.js
const path = require('path')
const helperLoader = require('./helper-loader')
const wrapRequires = (source, currentResourceFilename) => {
source.replace(
LOCAL_REQUIRE_REGEXP,
moduleImport => `./${currentResourceFilename}.${moduleImport}!=!${helperLoader}!${currentResourceFilename}?{content:"${source}",isJSON5:true}`
)
}
module.exports = function(source) {
const transformed = transformStrings(source)
const wrappedRequires = transformed.map(tr => {
tr.text.replace(
LOCAL_REQUIRE_REGEXP,
moduleImport => {
const moduleFile = transformed.find(tr => tr.name === moduleImport)
if (moduleFile) {
const escapedSource = escape(source)
return `require('./${currentResourceFilename}.${moduleImport}!=!${helperLoader}!${currentResourceFilename}?{content:"${escapedSource}",isJSON5:true}')`
} else {
return moduleImport
}
}
})
const currentResource = wrappedRequires.find(tr => tr.name === this.resourcePath)
return currentResource.text
}
// helper-loader.js
const { parseQuery } = require("loader-utils")
module.exports = function(source) {
const { content } = parseQuery(this.resourceQuery)
return content
} the implementation isnt perfect here, but the jist of it is that we can transform each require statement that matches one of the other transformed files into a webpack loader request which holds the files content inside it as a query. Then, when webpack uses the There are still a few small problems, I have a circular issue, I need to transform files before I set them as query parameters, but I need to put them as query parameters in order to transform them. The second issue being I dont know if I can properly escape all valid javascript into a query parameter. The good news is though, webpack can still resolve which requires are common, and this requires no extra access to the filesystem. |
This sounds like a way to go, thanks for putting effort into exploring that! If you make further progress I'd be happy about any contribution to this project. |
I'm considering working on this some myself, though am not sure yet whether the complexity level makes it feasible for me to complete with the motivation level I have. In any case, I thought I'd join the conversation, and try to get a picture on what challenges are left to solve for a proper webpack loader to be created. Regarding:
Couldn't you simply encode each character in the Javascript code into its number/character-code, separated by some known-safe character? For example:
And to decode:
EDIT: You should also be able to use base64 encoding, which is more compact: https://stackoverflow.com/a/2820329/2441655 |
Nice work @fabiandev; I'm really excited to test this out at some point!
Anyway, just throwing this idea out there in case you or anyone else have time for it at some point: a webpack plugin to automatically handle inserting the import into each source file and building with
tsr
.In addition to making it a lot simpler to get started by piggybacking on existing tooling, this would abstract away most of the work required for integrations like angular/angular-cli#6763 (at least for non-prod builds; some extra work might be needed to integrate
tsr
into Angular's AOT compiler).The text was updated successfully, but these errors were encountered: