Skip to content

Commit

Permalink
Improve compiler readability around commonjs wrapping parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jul 14, 2022
1 parent a614cfe commit 6b77bfc
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,26 @@ type compilationState struct {
// srcMap is the current full sourceMap that has been generated read so far
srcMap []byte
srcMapError error
main bool
wrapped bool // whether the original source is wrapped in a function to make it a commonjs module

compiler *Compiler
}

// Compile the program in the given CompatibilityMode, wrapping it between pre and post code
func (c *Compiler) Compile(src, filename string, main bool) (*goja.Program, string, error) {
return c.compileImpl(src, filename, main, c.Options.CompatibilityMode, nil)
// TODO isESM will be used once goja support ESM modules natively
func (c *Compiler) Compile(src, filename string, isESM bool) (*goja.Program, string, error) {
return c.compileImpl(src, filename, !isESM, c.Options.CompatibilityMode, nil)
}

// sourceMapLoader is to be used with goja's WithSourceMapLoader
// it not only gets the file from disk in the simple case, but also returns it if the map was generated from babel
// additioanlly it fixes off by one error in commonjs dependencies due to having to wrap them in a function.
func (c *compilationState) sourceMapLoader(path string) ([]byte, error) {
if path == sourceMapURLFromBabel {
return c.increaseMappingsByOne(c.srcMap)
if c.wrapped {
return c.increaseMappingsByOne(c.srcMap)
}
return c.srcMap, nil
}
c.srcMap, c.srcMapError = c.compiler.Options.SourceMapLoader(path)
if c.srcMapError != nil {
Expand All @@ -211,15 +215,18 @@ func (c *compilationState) sourceMapLoader(path string) ([]byte, error) {
c.srcMap = nil
return nil, c.srcMapError
}
return c.increaseMappingsByOne(c.srcMap)
if c.wrapped {
return c.increaseMappingsByOne(c.srcMap)
}
return c.srcMap, nil
}

func (c *Compiler) compileImpl(
src, filename string, main bool, compatibilityMode lib.CompatibilityMode, srcMap []byte,
src, filename string, wrap bool, compatibilityMode lib.CompatibilityMode, srcMap []byte,
) (*goja.Program, string, error) {
code := src
state := compilationState{srcMap: srcMap, compiler: c, main: main}
if !main { // the lines in the sourcemap (if available) will be fixed by increaseMappingsByOne
state := compilationState{srcMap: srcMap, compiler: c, wrapped: wrap}
if wrap { // the lines in the sourcemap (if available) will be fixed by increaseMappingsByOne
code = "(function(module, exports){\n" + code + "\n})\n"
}
opts := parser.WithDisableSourceMaps
Expand All @@ -242,7 +249,7 @@ func (c *Compiler) compileImpl(
return nil, code, err
}
// the compatibility mode "decreases" here as we shouldn't transform twice
return c.compileImpl(code, filename, main, lib.CompatibilityModeBase, state.srcMap)
return c.compileImpl(code, filename, wrap, lib.CompatibilityModeBase, state.srcMap)
}
return nil, code, err
}
Expand Down

0 comments on commit 6b77bfc

Please sign in to comment.