-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
TypeScript and ES6+ support using esbuild. #3738
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b0486e4
feat: TypeScript and ES6+ support using esbuild.
szkiba c5b1c24
feat: Application of golden file pattern for testing breaking test er…
szkiba 2758044
test: increase test coverage
szkiba ecd287f
Merge branch 'master' into feature/3703-typescript-support
szkiba 70eec19
fix: Include sources in source map
szkiba f3dfb30
test: test script for source map testing
szkiba 61e193c
Add a magic inline sourcemap URL to the generated code to make the so…
szkiba 21712b4
test: Full breaking_test_errors-*.json file generation with -update flag
szkiba 9a5dc14
fix: Only use the magic source map URL if source map is enabled
szkiba e760a20
test: Add test for magic source map URL workaround
szkiba daf55ee
Merge branch 'master' into feature/3703-typescript-support
szkiba 93048e5
Merge branch 'master' into feature/3703-typescript-support
szkiba 0fc2729
refactor: rename "enhanced" compatibility mode to "experimental_enhan…
szkiba be63c34
refactor: rename "enhanced" compatibility mode to "experimental_enhan…
szkiba d3f8987
Set target to ESNext - which in practice disables esbuild doing any t…
szkiba 01c794d
test: Move tests related to enhanced mode to enhanced_test.go
szkiba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import exec from "k6/execution"; | ||
|
||
export default function () { | ||
exec.test.abort("failed"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { User, newUser } from "./user.ts"; | ||
|
||
export default () => { | ||
const user: User = newUser("John"); | ||
console.log(user); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface User { | ||
name: string; | ||
id: number; | ||
} | ||
|
||
class UserAccount implements User { | ||
name: string; | ||
id: number; | ||
|
||
constructor(name: string) { | ||
this.name = name; | ||
this.id = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); | ||
} | ||
} | ||
|
||
function newUser(name: string): User { | ||
return new UserAccount(name); | ||
} | ||
|
||
export { User, newUser }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package compiler | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/dop251/goja/file" | ||
"github.com/dop251/goja/parser" | ||
"github.com/evanw/esbuild/pkg/api" | ||
) | ||
|
||
func esbuildTransform(src, filename string) (code string, srcMap []byte, err error) { | ||
opts := api.TransformOptions{ | ||
Sourcefile: filename, | ||
Loader: api.LoaderJS, | ||
Target: api.ES2017, | ||
Format: api.FormatCommonJS, | ||
Sourcemap: api.SourceMapExternal, | ||
SourcesContent: api.SourcesContentInclude, | ||
LegalComments: api.LegalCommentsNone, | ||
Platform: api.PlatformNeutral, | ||
LogLevel: api.LogLevelSilent, | ||
Charset: api.CharsetUTF8, | ||
} | ||
|
||
if filepath.Ext(filename) == ".ts" { | ||
opts.Loader = api.LoaderTS | ||
} | ||
|
||
result := api.Transform(src, opts) | ||
|
||
if hasError, err := esbuildCheckError(&result); hasError { | ||
return "", nil, err | ||
} | ||
|
||
return string(result.Code), result.Map, nil | ||
} | ||
|
||
func esbuildCheckError(result *api.TransformResult) (bool, error) { | ||
if len(result.Errors) == 0 { | ||
return false, nil | ||
} | ||
|
||
msg := result.Errors[0] | ||
err := &parser.Error{Message: msg.Text} | ||
|
||
if msg.Location != nil { | ||
err.Position = file.Position{ | ||
Filename: msg.Location.File, | ||
Line: msg.Location.Line, | ||
Column: msg.Location.Column, | ||
} | ||
} | ||
|
||
return true, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package compiler | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/dop251/goja/parser" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_esbuildTransform_js(t *testing.T) { | ||
t.Parallel() | ||
|
||
code, srcMap, err := esbuildTransform(`export default function(name) { return "Hello, " + name }`, "script.js") | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, srcMap) | ||
require.NotEmpty(t, code) | ||
} | ||
|
||
func Test_esbuildTransform_ts(t *testing.T) { | ||
t.Parallel() | ||
|
||
script := `export function hello(name:string) : string { return "Hello, " + name}` | ||
|
||
code, srcMap, err := esbuildTransform(script, "script.ts") | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, srcMap) | ||
require.NotEmpty(t, code) | ||
} | ||
|
||
func Test_esbuildTransform_error(t *testing.T) { | ||
t.Parallel() | ||
|
||
script := `export function hello(name:string) : string { return "Hello, " + name}` | ||
|
||
_, _, err := esbuildTransform(script, "script.js") | ||
|
||
require.Error(t, err) | ||
|
||
var perr *parser.Error | ||
|
||
require.True(t, errors.As(err, &perr)) | ||
require.NotNil(t, perr.Position) | ||
require.Equal(t, "script.js", perr.Position.Filename) | ||
require.Equal(t, 1, perr.Position.Line) | ||
require.Equal(t, 26, perr.Position.Column) | ||
require.Equal(t, "Expected \")\" but found \":\"", perr.Message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This makes esbuild unneedingly transform a whole bunch of code that goja supports.
In practice from the list of features on https://esbuild.github.io/content-types/#javascript
The only ones not supported are async generators, async iteration and
??=
. And in my test the async generator and async iteration doesn't work with the example from mdn.This also adds a bunch of esnext stuff which I haven't tested, but will argue are probably even less desired by even our less average users given that they aren't released yet.
If you will likely to enable any of those it will be nice to also enable their tc39 tests respectfully. I would recommend that this is done after the initial release of this instead of now.
So my strong recommendation is this to be set to ESNext - which in practice disables esbuild doing any transformation for features and leave just the TypeScript + commonJS transofmration. Some tc39 tests get slightly different results witht that.
You can use the
Supported
field to set which transformation you want to add. Again I will recommend doing this after this is merged, maybe even after we have had some feedback.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.
Done, I changed the target to ESNext (and then adjusted the golden file of the tc39 test)