-
Notifications
You must be signed in to change notification settings - Fork 109
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
Add browser support via babel #440
base: master
Are you sure you want to change the base?
Conversation
bfbe71b
to
ee2aec9
Compare
ee2aec9
to
900720a
Compare
Thank you for the pull request. I'm looking forward to reviewing it. You touch on a point I think may have contributed to the stalling of this project. We did work a few years ago that should (if implemented correctly) eliminate the need for import { ActionTransition } from "antlr4ts/atn/ActionTransition"; can and should be eliminated. The Façade Pattern implemented in the index.ts files means that import { ActionTransition } from "antlr4ts"; should work, and is less fragile. Of course, as you correctly state, that would be a breaking change, but that's what major versions in semantic versioning are for. We're well overdue for a V1 release, and I think some cleanup of the way clients reach into the run-time code is would be appropriate. Bundlers like rollup and webpack are an important part of the modern javascript ecosystem, but neither @sharwell nor I had any experience with them when we started the port. |
Do you have any specific reason to say that Typescript's ES5 support is buggy? |
On the one hand it would be better to be able to rearrange library directory structure without impacting clients in future versions, on the other it forces clients to have to have a tree shaking bundler if they want to minimize their browser bundles. It is not my call to make.
If you enable these options in tsconfig
you will quickly run into problems #413 was facing, first of which is:
Looking at TS's issue you will see that they don't really care to support this compilation to ES5. That is a specific example but in general it is very telling of their support for ES5 compilation. Even though babel can compile the code just fine from ES6 to ES5, Typescript forces you to think about ES5 compilation quirks even with valid Typescript. If TS only fully supports compilation to ES6 then we should use babel to compile to ES5. There are many more of these sorts of problems that were addressed in #413. One perk of using babel is that with |
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.
Thanks for the PR. I've got some questions.
@@ -0,0 +1 @@ | |||
10.12.0 |
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.
Our convention is to always have a newline at the end of all files. The file '.editorconfig' generally fixes this, but your editor may not be using it. There are plugins, e.g. I use an editorconfig add-in for vscode.
Can you explain the effect of adding this .nvmrc file? Does it work with the original nvm, nvm-windows, or both?
.pipe(gulp.dest('target/src')); | ||
}; | ||
|
||
exports.default = build; |
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.
newline at end
function build() { | ||
return gulp.src('src/**/*.ts') | ||
.pipe(sourcemaps.init()) | ||
.pipe(tsProj()) |
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.
I've seen projects where they translate the .ts directly into .js using babel with @babel/preset-typescript @babel/preset-env @babel/plugin-proposal-class-properties @babel/plugin-proposal-object-rest-spread. That approach is discussed in a blog entry from the typescript team. The tsc command in that approach is only used for error checking.
The approach you have here seems to run tsc and babel differently with the tsc output feeding into babel. Can you explain why you chose that approach?
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.
Webpack has the same problem as rollup: it was mainly designed to bundle JS into one file. Good for shipping to browser only for HTTP1 concerns, but for libraries it is best to ship in multiple formats and let the user decide how they would like to consume it. Gulp is the easiest way to allow that.
Let me know if you would like me to keep updating this PR.
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.
Ah I see your question was not about webpack (which recommends uses this method for compiling TS) specifically. I still standby my point that it is better to ship in multiple formats, which gulp is better designed for.
@@ -185,8 +185,8 @@ Licensed under the BSD-3-Clause license. See LICENSE file in the project root fo | |||
<execution> | |||
<id>default-compile</id> | |||
<configuration> | |||
<source>1.6</source> | |||
<target>1.6</target> | |||
<source>1.8</source> |
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.
@sharwell, are you OK with this change in compiler version required?
@@ -7,6 +7,8 @@ | |||
"lib": [ | |||
"es6" | |||
], | |||
"inlineSourceMap": true, |
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.
As stands, there is already a "sourceMap" option specified at line 5. This "inlineSourceMap" at line 11 conflicts with the earlier one it and generates a compile error on my machine. I suggest you bring in the latest tsconfig.json from the project master branch.
@unsupervisednn @BurtHarris, it looks like the build is failing because the CI uses Node.js v6. Specifically, The README.md points to supporting Node.js 6.7.x. AppVeyor step that downgraded to v6 |
What's blocking this from being merged? |
Any updates on this? I took a shot at running this code in the browser, but unfortunately ran into errors with various Nodejs modules that are needed, like |
Using Rollup I was able to get this to work in the browser, shimming |
This takes a different approach from #413. Instead of modifying dozens of typescript files in the fragile hopes of getting it to compile nicely to es5, I took the es2015 generated by TS and gave it to babel to transform. This seems simpler and easier because instead of relying on buggy TS es5 support, we can use industry standard babel which will hardly ever fail at transforming es2015+ to es5. I do not think the responsibility of getting working es5 should fall on the TS programmer. I used gulp to take the output of TS and stream it to babel without the need to hit the filesystem twice while compiling.
I had tried to use rollup before (#422) but rollup is not suited to compiling individual files to es5. It is a bundler and designed to output the whole library into a single file. This breaks clients which import/require the individual files such as "antlr4ts/atn/ActionTransition". Rollup would only help in making a bundle file meant to be added directly to client HTML to reduce script requests, perhaps as a UMD file. However, most client projects will have their own bundler that statically includes all necessary library code.