Skip to content

Commit

Permalink
Defer initialisation of ExecJS context
Browse files Browse the repository at this point in the history
Wait until context is used before initialising context. This avoids memory leaks with MiniRacer. Fixes #165.
  • Loading branch information
lautis committed Sep 25, 2019
1 parent 990a6eb commit 9de67eb
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/uglifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ def initialize(options = {})
raise ArgumentError, "Invalid option: #{missing}"
end
@options = options

source = harmony? ? source_with(HarmonySourcePath) : source_with(SourcePath)
@context = ExecJS.compile(source)
end

# Minifies JavaScript code
Expand Down Expand Up @@ -181,6 +178,13 @@ def compile_with_map(source)

private

def context
@context ||= begin
source = harmony? ? source_with(HarmonySourcePath) : source_with(SourcePath)
ExecJS.compile(source)
end
end

def source_map_comments
return '' unless @options[:source_map].respond_to?(:[])

Expand Down Expand Up @@ -214,7 +218,7 @@ def run_uglifyjs(input, generate_map)
:ie8 => ie8?
}

parse_result(@context.call("uglifier", options), generate_map, options)
parse_result(context.call("uglifier", options), generate_map, options)
end

def harmony?
Expand Down

0 comments on commit 9de67eb

Please sign in to comment.