From 9de67eb346b2ecca7d1134412be753cfa5c492c2 Mon Sep 17 00:00:00 2001 From: Ville Lautanala Date: Wed, 25 Sep 2019 21:12:44 +0300 Subject: [PATCH] Defer initialisation of ExecJS context Wait until context is used before initialising context. This avoids memory leaks with MiniRacer. Fixes #165. --- lib/uglifier.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/uglifier.rb b/lib/uglifier.rb index acb6e32..f244c5e 100644 --- a/lib/uglifier.rb +++ b/lib/uglifier.rb @@ -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 @@ -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?(:[]) @@ -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?