Browse Source

[js/python] always generate requires/imports in dce=no, as we can't know if the type is used directly or not without DCE (closes #3873)

Dan Korostelev 10 years ago
parent
commit
ac002c6f7f
3 changed files with 13 additions and 2 deletions
  1. 11 0
      common.ml
  2. 1 1
      genjs.ml
  3. 1 1
      genpy.ml

+ 11 - 0
common.ml

@@ -871,6 +871,17 @@ let add_feature com f =
 let has_dce com =
 	(try defined_value com Define.Dce <> "no" with Not_found -> false)
 
+(*
+	TODO: The has_dce check is there because we mark types with @:directlyUsed in the DCE filter,
+	which is not run in dce=no and thus we can't know if a type is used directly or not,
+	so we just assume that they are.
+
+	If we had dce filter always running (even with dce=no), we would have types marked with @:directlyUsed
+	and we wouldn't need to generate unnecessary imports in dce=no, but that's good enough for now.
+*)
+let is_directly_used_class com c =
+	not (has_dce com) || Ast.Meta.has Ast.Meta.DirectlyUsed c.cl_meta
+
 let rec has_feature com f =
 	try
 		Hashtbl.find com.features f

+ 1 - 1
genjs.ml

@@ -1160,7 +1160,7 @@ let generate_type ctx = function
 			()
 		else if not c.cl_extern then
 			generate_class ctx c
-		else if (Meta.has Meta.JsRequire c.cl_meta) && (Meta.has Meta.DirectlyUsed c.cl_meta) then
+		else if Meta.has Meta.JsRequire c.cl_meta && is_directly_used_class ctx.com c then
 			generate_require ctx c
 		else if not ctx.js_flatten && Meta.has Meta.InitPackage c.cl_meta then
 			(match c.cl_path with

+ 1 - 1
genpy.ml

@@ -1920,7 +1920,7 @@ module Generator = struct
 	let gen_import ctx c =
 		gen_pre_code_meta ctx c.cl_meta;
 
-		if (Meta.has Meta.PythonImport c.cl_meta) && (Meta.has Meta.DirectlyUsed c.cl_meta) then begin
+		if Meta.has Meta.PythonImport c.cl_meta && is_directly_used_class ctx.com c then begin
 			let _, args, mp = Meta.get Meta.PythonImport c.cl_meta in
 
 			let class_name = match c.cl_path with