You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2014/03/27 15:22:27 UTC

[18/34] js commit: adding browserify require transform

adding browserify require transform


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/06449712
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/06449712
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/06449712

Branch: refs/heads/browserify
Commit: 064497125c63f826e08f71b1aafa74da7e5f5244
Parents: 78da184
Author: Anis Kadri <an...@apache.org>
Authored: Mon Feb 3 22:15:19 2014 -0800
Committer: Anis Kadri <an...@apache.org>
Committed: Thu Mar 27 15:20:21 2014 +0100

----------------------------------------------------------------------
 tasks/lib/require-tr.js | 81 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/06449712/tasks/lib/require-tr.js
----------------------------------------------------------------------
diff --git a/tasks/lib/require-tr.js b/tasks/lib/require-tr.js
new file mode 100644
index 0000000..4459fda
--- /dev/null
+++ b/tasks/lib/require-tr.js
@@ -0,0 +1,81 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+/*
+ * This probably should live in plugman/cli world
+ * Transoforms old require calls to new node-style require calls
+ */
+
+var fs = require('fs');
+var path = require('path');
+var through = require('through');
+var UglifyJS = require('uglify-js');
+var root = fs.realpathSync(path.join(__dirname, '..', '..'));
+
+function _updateRequires(code) {
+  
+  var ast = UglifyJS.parse(code);
+
+  var walker = new UglifyJS.TreeWalker(function(node) {
+    // check all function calls
+    if(node instanceof UglifyJS.AST_Call) {
+      // check if function call is a require('module') call
+      if(node.expression.name === "require") {
+        // make sure require only has one argument and that it starts with cordova (old style require.js) 
+        //fs.appendFileSync('/tmp/foo', JSON.stringify(node.args[0]) + "###\n");
+        if(node.args.length === 1 && 
+           node.args[0].value !== undefined &&
+           node.args[0].value.indexOf("cordova") === 0){
+          if(node.args[0].value === "cordova") {
+            node.args[0].value = path.join(root, "src", "cordova");
+          } else if(node.args[0].value.match(/cordova\/(.+)/)) {
+            node.args[0].value = node.args[0].value.replace(/cordova\/(.+)/, path.join(root, "src", "common", "$1"));
+          }
+        }
+      }
+    }
+  });
+
+  ast.walk(walker);
+
+  var stream = UglifyJS.OutputStream({beautify:true});
+
+  ast.print(stream);
+
+  return stream.toString();
+}
+
+
+module.exports = function(file) {
+  var data = '';
+
+  function write(buf) {
+    data += buf;
+  }
+
+  function end() {
+    //fs.appendFileSync('/tmp/foo', _updateRequires(data));
+    this.queue(_updateRequires(data));
+    this.queue(null);
+  }
+ 
+  return through(write, end);
+}