summaryrefslogtreecommitdiff
path: root/chromium/ios/web/js_compile.gni
blob: 82e6ceb17d13c34b580ae0a1145b274c11bcd7a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/util/java_action.gni")

declare_args() {
  # Control whether the JavaScript files shipped with Chrome on iOS are
  # compiled with closure_compiler or not. Useful for debugging.
  compile_javascript = true
}

closure_compiler_path = "//third_party/closure_compiler/compiler/compiler.jar"

# Defines a target that create a JavaScript bundle using the closure compiler.
#
# Variables
#   sources:
#     List of JavaScript files to compile into the bundle.
#
#   closure_entry_point:
#     Name of the entry point closure module.
#
#   deps (optional)
#     List of targets required by this target.
#
#   visibility (optional)
#     Visibility restrictions.
#
# Generates a single JavaScript bundle file that can be put in the application
# bundle.
#
# TODO(crbug.com/487804): once all errors have been fixed, sync with the flags
# from third_party/closure_compiler/closure_args.gni.
template("js_compile_bundle") {
  assert(defined(invoker.sources),
         "Need sources in $target_name listing the js files.")

  assert(defined(invoker.closure_entry_point),
         "Need closure_entry_point in $target_name for generated js file.")

  _target_name = target_name
  compile_js_target_name = target_name + "_compile_js"

  java_action(compile_js_target_name) {
    visibility = [ ":$_target_name" ]
    forward_variables_from(invoker, [ "testonly" ])
    script = closure_compiler_path
    sources = invoker.sources
    outputs = [
      "$target_gen_dir/$_target_name.js",
    ]

    args = [
             "--compilation_level",
             "SIMPLE_OPTIMIZATIONS",
             "--js_output_file",
             rebase_path("$target_gen_dir/$_target_name.js", root_build_dir),
             "--only_closure_dependencies",
             "--closure_entry_point",
             invoker.closure_entry_point,
             "--js",
           ] + rebase_path(sources, root_build_dir)
  }

  bundle_data(target_name) {
    forward_variables_from(invoker,
                           [
                             "data_deps",
                             "deps",
                             "public_deps",
                             "testonly",
                             "visibility",
                           ])

    if (!defined(public_deps)) {
      public_deps = []
    }
    public_deps += [ ":$compile_js_target_name" ]
    sources = get_target_outputs(":$compile_js_target_name")
    outputs = [
      "{{bundle_resources_dir}}/{{source_file_part}}",
    ]
  }
}

# Defines a target that compile JavaScript files with error checking using the
# closure compiler.
#
# Variables
#   sources:
#     List of JavaScript files to compile.
#
#   deps (optional)
#     List of targets required by this target.
#
#   js_modules
#     List of JS files defining modules that are required by the sources.
#     The list must be sorted in dependency order.
#     This is based on
#     https://github.com/google/closure-compiler/wiki/JS-Modules
#     https://stackoverflow.com/questions/10395810/how-do-i-split-my-javascript-into-modules-using-googles-closure-compiler/10401030#10401030
#
#   visibility (optional)
#     Visibility restrictions.
#
# TODO(crbug.com/487804): once all errors have been fixed, sync with the flags
# from third_party/closure_compiler/closure_args.gni.
template("js_compile_checked") {
  assert(defined(invoker.sources),
         "Need sources in $target_name listing the js files.")

  if (compile_javascript) {
    _target_name = target_name
    compile_js_target_name = target_name + "_compile_js"

    java_action_foreach(compile_js_target_name) {
      visibility = [ ":$_target_name" ]
      forward_variables_from(invoker, [ "testonly" ])
      script = closure_compiler_path
      sources = invoker.sources
      _js_modules = [
        "//ios/web/web_state/js/resources/base.js",
        "//ios/web/web_state/js/resources/common.js",
        "//ios/web/web_state/js/resources/message.js",
      ]
      if (defined(invoker.js_modules)) {
        _js_modules += invoker.js_modules
      }
      inputs = _js_modules
      outputs = [
        "$target_gen_dir/{{source_file_part}}",
      ]

      args = [
        "--compilation_level",
        "SIMPLE_OPTIMIZATIONS",
        "--jscomp_error=checkTypes",
        "--jscomp_error=checkVars",
        "--jscomp_error=missingProperties",
        "--jscomp_error=undefinedVars",
        "--jscomp_error=accessControls",
        "--jscomp_error=ambiguousFunctionDecl",
        "--jscomp_error=constantProperty",
        "--jscomp_error=deprecated",
        "--jscomp_error=externsValidation",
        "--jscomp_error=globalThis",
        "--jscomp_error=invalidCasts",
        "--jscomp_error=missingReturn",
        "--jscomp_error=nonStandardJsDocs",
        "--jscomp_error=suspiciousCode",
        "--jscomp_error=undefinedNames",
        "--jscomp_error=unknownDefines",
        "--jscomp_error=uselessCode",
        "--jscomp_error=visibility",
        "--module_output_path_prefix",
        rebase_path(target_gen_dir, root_build_dir) + "/",
      ]
      last_dependency = ""
      foreach(_js_module, _js_modules) {
        _js_module_name = get_path_info(_js_module, "name")
        args += [
          "--js",
          rebase_path(_js_module, root_build_dir),
          "--module",
          "$_js_module_name:1$last_dependency",
        ]
        last_dependency = ":$_js_module_name"
      }
      args += [
        "--js",
        "{{source}}",
        "--module",
        "{{source_name_part}}:1$last_dependency",
      ]
    }

    bundle_data(target_name) {
      forward_variables_from(invoker,
                             [
                               "data_deps",
                               "deps",
                               "public_deps",
                               "testonly",
                               "visibility",
                             ])

      if (!defined(public_deps)) {
        public_deps = []
      }
      public_deps += [ ":$compile_js_target_name" ]
      sources = get_target_outputs(":$compile_js_target_name")
      outputs = [
        "{{bundle_resources_dir}}/{{source_file_part}}",
      ]
    }
  } else {
    bundle_data(target_name) {
      forward_variables_from(invoker,
                             [
                               "data_deps",
                               "deps",
                               "public_deps",
                               "testonly",
                               "visibility",
                             ])

      sources = invoker.sources
      outputs = [
        "{{bundle_resources_dir}}/{{source_file_part}}",
      ]
    }
  }
}

# Defines a target that compile JavaScript files without error checking using
# the closure compiler.
#
# Variables
#   sources:
#     List of JavaScript files to compile.
#
#   deps (optional)
#     List of targets required by this target.
#
#   visibility (optional)
#     Visibility restrictions.
#
# TODO(crbug.com/487804): once all errors have been fixed, remove this template
# and port all code to use js_compile_checked instead.
template("js_compile_unchecked") {
  assert(defined(invoker.sources),
         "Need sources in $target_name listing the js files.")

  if (compile_javascript) {
    _target_name = target_name
    compile_js_target_name = target_name + "_compile_js"

    java_action_foreach(compile_js_target_name) {
      visibility = [ ":$_target_name" ]
      forward_variables_from(invoker, [ "testonly" ])
      script = closure_compiler_path
      sources = invoker.sources
      outputs = [
        "$target_gen_dir/{{source_file_part}}",
      ]

      args = [
        "--compilation_level",
        "SIMPLE_OPTIMIZATIONS",
        "--js",
        "{{source}}",
        "--js_output_file",
        rebase_path("$target_gen_dir/{{source_file_part}}", root_build_dir),
      ]
    }

    bundle_data(target_name) {
      forward_variables_from(invoker,
                             [
                               "data_deps",
                               "deps",
                               "public_deps",
                               "testonly",
                               "visibility",
                             ])

      if (!defined(public_deps)) {
        public_deps = []
      }
      public_deps += [ ":$compile_js_target_name" ]
      sources = get_target_outputs(":$compile_js_target_name")
      outputs = [
        "{{bundle_resources_dir}}/{{source_file_part}}",
      ]
    }
  } else {
    bundle_data(target_name) {
      forward_variables_from(invoker,
                             [
                               "data_deps",
                               "deps",
                               "public_deps",
                               "testonly",
                               "visibility",
                             ])

      sources = invoker.sources
      outputs = [
        "{{bundle_resources_dir}}/{{source_file_part}}",
      ]
    }
  }
}