Commit bf9521dc authored by John Williams's avatar John Williams Committed by Commit Bot

Added fixes for MR module loading.

Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I9b006ee2b566712ac2f62f8c0599054176f9bafc
Reviewed-on: https://chromium-review.googlesource.com/917307Reviewed-by: default avatarmark a. foltz <mfoltz@chromium.org>
Commit-Queue: John Williams <jrw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#538945}
parent 25072f82
......@@ -14,11 +14,12 @@ JSCompiler's --module flag.)
import argparse
import os.path
import re
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--module-specs", nargs='+',
"--module-specs", nargs="+",
help="List of module specifiations.")
parser.add_argument(
"--output-dir",
......@@ -33,7 +34,7 @@ def main():
# Read the prelude file, which contains code to be placed at the
# start of each module.
with open(args.prelude_file, 'r') as prelude_in:
with open(args.prelude_file, "r") as prelude_in:
prelude = prelude_in.read()
# Loop over all specified modules, and simulaneously traverse the
......@@ -42,10 +43,10 @@ def main():
for spec in args.module_specs:
# Split the module spec into a module name and a count of source
# files to include in the module.
pre, sep, post = spec.partition('module=')
assert pre == ''
pre, sep, post = spec.partition("module=")
assert pre == ""
assert sep
parts = post.split(':')
parts = post.split(":")
module_name = parts[0]
input_count = int(parts[1])
......@@ -58,8 +59,26 @@ def main():
for i in range(input_count):
source_file = args.sources[source_index]
source_index += 1
module_name = None
with open(source_file, "r") as source_in:
module_out.write(source_in.read())
module_out.write("goog.scope(() => {\n")
# Copy input line by line.
for line in source_in:
m = re.match(r"goog\.module\('([^']+)'\);\n", line)
if m:
# Handle goog.module statements specially.
module_name = m.group(1)
module_out.write("let exports = {};\n")
else:
# Typical case: just copy the line verbatim.
module_out.write(line)
if module_name:
# Put exported names into the global namespace. That's
# not now goog.module is supposed to work, but it's close
# enough.
module_out.write(
"__setGlobal('{}', exports);\n".format(module_name));
module_out.write("});\n")
# Check that every source file has been appended to a module.
if source_index != len(args.sources):
......
......@@ -2,11 +2,49 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
goog = {};
goog.require = () => undefined;
goog.provide = goog.require;
goog.module = goog.require;
goog.forwardDeclare = goog.require;
goog.scope = body => {
body.call(window);
// Implementation of JSCompiler intrinsics for use when JSCompiler is
// not available.
/**
* Sets the values of a global expression consisting of a
* dot-delimited list of identifiers.
*/
const __setGlobal = (name, value) => {
let parent = window;
const parts = name.split('.');
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (i == parts.length - 1) {
parent[part] = value;
} else {
if (!parent[part]) {
parent[part] = {};
}
parent = parent[part];
}
}
};
const goog = {
provide(name) {
__setGlobal(name, {});
},
require(name) {
let parent = window;
name.split('.').forEach(part => {
parent = parent[part];
});
return parent;
},
module: {
declareLegacyNamespace() {},
},
forwardDeclare() {},
scope(body) {
body.call(window);
},
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment