diff --git a/build.py b/build.py index ccbc6deee..2e2994790 100755 --- a/build.py +++ b/build.py @@ -75,62 +75,68 @@ class Gen_uncompressed(threading.Thread): self.search_paths = search_paths def run(self): - target_filename = "blockly_uncompressed.js" - add_dependency = [] - base_path = calcdeps.FindClosureBasePath(self.search_paths) - deps = calcdeps.BuildDependenciesFromFiles(self.search_paths) - filenames = calcdeps.CalculateDependencies(self.search_paths, - [os.path.join("core", "blockly.js")]) - - for dep in deps: - if dep.filename in filenames: - add_dependency.append(calcdeps.GetDepsLine(dep, base_path)) - add_dependency = "\n".join(add_dependency) - # Find the Blockly directory name and replace it with a JS variable. - # This allows blockly_uncompressed.js to be compiled on one computer and be - # used on another, even if the directory name differs. - m = re.search("[\\/]([^\\/]+)[\\/]core[\\/]blockly.js", add_dependency) - add_dependency = re.sub("([\\/])" + re.escape(m.group(1)) + - "([\\/]core[\\/])", '\\1" + dir + "\\2', add_dependency) - - provides = [] - for dep in deps: - if dep.filename in filenames: - if not dep.filename.startswith(os.pardir + os.sep): # "../" - provides.extend(dep.provides) - provides.sort() - - f = open(target_filename, "w") + target_filename = 'blockly_uncompressed.js' + f = open(target_filename, 'w') f.write(HEADER) f.write(""" -// 'this' is 'window' in a browser, or 'global' in node.js. -this.BLOCKLY_DIR = (function() { - // Find name of current directory. - var scripts = document.getElementsByTagName('script'); - var re = new RegExp('(.+)[\/]blockly_uncompressed\.js$'); - for (var x = 0, script; script = scripts[x]; x++) { - var match = re.exec(script.src); - if (match) { - return match[1]; +var isNodeJS = !!(typeof module !== 'undefined' && module.exports); + +if (isNodeJS) { + var window = {}; + require('../closure-library/closure/goog/bootstrap/nodejs') +} + +window.BLOCKLY_DIR = (function() { + if (!isNodeJS) + { + // Find name of current directory. + var scripts = document.getElementsByTagName('script'); + var re = new RegExp('(.+)[\/]blockly_uncompressed\.js$'); + for (var x = 0, script; script = scripts[x]; x++) { + var match = re.exec(script.src); + if (match) { + return match[1]; + } } + alert('Could not detect Blockly\\'s directory name.'); } - alert('Could not detect Blockly\\'s directory name.'); return ''; })(); -this.BLOCKLY_BOOT = function() { -// Execute after Closure has loaded. -if (!this.goog) { - alert('Error: Closure not found. Read this:\\n' + - 'developers.google.com/blockly/hacking/closure'); -} - -// Build map of all dependencies (used and unused). -var dir = this.BLOCKLY_DIR.match(/[^\\/]+$/)[0]; +window.BLOCKLY_BOOT = function() { + var dir = ''; + if (isNodeJS) { + require('../closure-library/closure/goog/bootstrap/nodejs') + dir = 'blockly'; + } else { + // Execute after Closure has loaded. + if (!window.goog) { + alert('Error: Closure not found. Read this:\\n' + + 'developers.google.com/blockly/hacking/closure'); + } + dir = window.BLOCKLY_DIR.match(/[^\\/]+$/)[0]; + } """) - f.write(add_dependency + "\n") - f.write("\n") - f.write("// Load Blockly.\n") + add_dependency = [] + base_path = calcdeps.FindClosureBasePath(self.search_paths) + for dep in calcdeps.BuildDependenciesFromFiles(self.search_paths): + add_dependency.append(calcdeps.GetDepsLine(dep, base_path)) + add_dependency = '\n'.join(add_dependency) + # Find the Blockly directory name and replace it with a JS variable. + # This allows blockly_uncompressed.js to be compiled on one computer and be + # used on another, even if the directory name differs. + m = re.search('[\\/]([^\\/]+)[\\/]core[\\/]blockly.js', add_dependency) + add_dependency = re.sub('([\\/])' + re.escape(m.group(1)) + + '([\\/]core[\\/])', '\\1" + dir + "\\2', add_dependency) + f.write(add_dependency + '\n') + + provides = [] + for dep in calcdeps.BuildDependenciesFromFiles(self.search_paths): + if not dep.filename.startswith(os.pardir + os.sep): # '../' + provides.extend(dep.provides) + provides.sort() + f.write('\n') + f.write('// Load Blockly.\n') for provide in provides: f.write("goog.require('%s');\n" % provide) @@ -139,17 +145,17 @@ delete this.BLOCKLY_DIR; delete this.BLOCKLY_BOOT; }; -if (typeof DOMParser == 'undefined' && typeof require == 'function') { - // Node.js needs DOMParser loaded separately. - var DOMParser = require('xmldom').DOMParser; +if (isNodeJS) { + window.BLOCKLY_BOOT() + module.exports = Blockly; +} else { + // Delete any existing Closure (e.g. Soy's nogoog_shim). + document.write(''); + // Load fresh Closure Library. + document.write(''); + document.write(''); } - -// Delete any existing Closure (e.g. Soy's nogoog_shim). -document.write(''); -// Load fresh Closure Library. -document.write(''); -document.write(''); """) f.close() print("SUCCESS: " + target_filename) @@ -437,9 +443,11 @@ if __name__ == "__main__": "Please rename this directory.") elif os.path.isdir(os.path.join(os.path.pardir, "google-closure-library")): # When Closure is installed by npm, it is named "google-closure-library". + #calcdeps = import_path(os.path.join( + # os.path.pardir, "google-closure-library", "closure", "bin", "calcdeps.py")) print("Error: Closure directory needs to be renamed from" - "'google-closure-library' to 'closure-library'.\n" - "Please rename this directory.") + "'google-closure-library' to 'closure-library'.\n" + "Please rename this directory.") else: print("""Error: Closure not found. Read this: https://developers.google.com/blockly/hacking/closure""") diff --git a/package.json b/package.json index 6eba03ab2..cc24f2a90 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "blockly-src", + "name": "blockly", "version": "1.0.0", "description": "Blockly is a library for building visual programming editors.", "keywords": ["blockly"],