|
@@ -15,23 +15,23 @@ def extract_require(s):
|
|
|
return None
|
|
|
return m.group(3)
|
|
|
|
|
|
-def resolve_require(req, out, resolved, resolving):
|
|
|
- if not os.path.exists(req):
|
|
|
+def resolve_require(req, out, resolved, resolving, dir):
|
|
|
+ if not os.path.exists(os.path.join(dir, req)):
|
|
|
raise Exception('cannot resolve "%s"' % req)
|
|
|
- process(req, out, resolved, resolving)
|
|
|
+ process(req, out, resolved, resolving, dir)
|
|
|
|
|
|
-def process(path, out, resolved, resolving):
|
|
|
+def process(path, out, resolved, resolving, dir):
|
|
|
module_name = os.path.splitext(os.path.basename(path))[0]
|
|
|
if module_name in resolving:
|
|
|
raise Exception('cyclic import detected: "%s"' % module_name)
|
|
|
result = 'imports["%s"] = {};\n' % path
|
|
|
result += '(function %s(exports){\n' % module_name
|
|
|
- with open(path) as f:
|
|
|
+ with open(os.path.join(dir, path)) as f:
|
|
|
for l in f:
|
|
|
req = extract_require(l)
|
|
|
if req and not req in resolved:
|
|
|
try:
|
|
|
- resolve_require(req, out, resolved, resolving + [module_name])
|
|
|
+ resolve_require(req, out, resolved, resolving + [module_name], dir)
|
|
|
except Exception:
|
|
|
print('while resolving "%s"...' % module_name)
|
|
|
raise sys.exc_info()[1]
|
|
@@ -40,15 +40,16 @@ def process(path, out, resolved, resolving):
|
|
|
out.write(result)
|
|
|
resolved += [path]
|
|
|
|
|
|
-if __name__ == '__main__':
|
|
|
- if len(sys.argv) != 3:
|
|
|
- raise Exception("Usage: linkjs.py <input js> <output js>")
|
|
|
-
|
|
|
- input_path = sys.argv[1]
|
|
|
- output_path = sys.argv[2]
|
|
|
+def link(input_path, output_path, dir):
|
|
|
with open(output_path, "w") as out:
|
|
|
prolog = "var imports = {};\n"
|
|
|
prolog += 'function require(module){return imports[module];}\n'
|
|
|
out.write(prolog)
|
|
|
- process(input_path, out, [], [])
|
|
|
+ process(input_path, out, [], [], dir)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ if len(sys.argv) != 3:
|
|
|
+ raise Exception("Usage: linkjs.py <input js> <output js>")
|
|
|
+
|
|
|
+ link(sys.argv[1], sys.argv[2], '.')
|
|
|
|