Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CLJS-772: Support :none & :whitespace optimizations under Node.js

Include nodejs.js from Closure Library. When :nodejs target and
:optimizations :none or :whitespace, emit nodejs.js to the expected
location in the output directory.
  • Loading branch information...
commit 0c7b31ada01237de33cef77b817ccef3f2b3576d 1 parent 71f781c
@swannodette swannodette authored
Showing with 100 additions and 5 deletions.
  1. +9 −5 src/clj/cljs/closure.clj
  2. +91 −0 src/cljs/cljs/nodejs.js
View
14 src/clj/cljs/closure.clj
@@ -909,10 +909,7 @@
(env/default-compiler-env opts))))
([source opts compiler-env]
(env/with-compiler-env compiler-env
- (let [opts (if (= :nodejs (:target opts))
- (merge {:optimizations :simple} opts)
- opts)
- ups-deps (get-upstream-deps)
+ (let [ups-deps (get-upstream-deps)
all-opts (assoc opts
:ups-libs (:libs ups-deps)
:ups-foreign-libs (:foreign-libs ups-deps)
@@ -966,7 +963,14 @@
(add-source-map-link all-opts)
(add-header all-opts)
(output-one-file all-opts)))
- (apply output-unoptimized all-opts js-sources))))))))
+ (apply output-unoptimized all-opts js-sources))
+ ;; emit Node.js bootstrap script for :none & :whitespace optimizations
+ (when (and (= (:target opts) :nodejs)
+ (#{:none :whitespace} (:optimizations opts)))
+ (let [outfile (io/file (io/file (output-directory opts))
+ "goog/bootstrap/nodejs.js")]
+ (comp/mkdirs outfile)
+ (spit outfile (slurp (io/resource "cljs/nodejs.js")))))))))))
(comment
View
91 src/cljs/cljs/nodejs.js
@@ -0,0 +1,91 @@
+// Copyright 2013 The Closure Library Authors.
+//
+// Licensed 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.
+
+/**
+ * @fileoverview A nodejs script for dynamically requiring Closure within
+ * nodejs.
+ *
+ * Example of usage:
+ * <code>
+ * require('./bootstrap/nodejs')
+ * goog.require('goog.ui.Component')
+ * </code>
+ *
+ * This loads goog.ui.Component in the global scope.
+ *
+ * If you want to load custom libraries, you can require the custom deps file
+ * directly. If your custom libraries introduce new globals, you may
+ * need to run goog.nodeGlobalRequire to get them to load correctly.
+ *
+ * <code>
+ * require('./path/to/my/deps.js')
+ * goog.bootstrap.nodeJs.nodeGlobalRequire('./path/to/my/base.js')
+ * goog.require('my.Class')
+ * </code>
+ *
+ * @author nick@medium.com (Nick Santos)
+ *
+ * @nocompile
+ */
+
+
+var fs = require('fs');
+var path = require('path');
+
+
+/**
+ * The goog namespace in the global scope.
+ */
+global.goog = {};
+
+
+/**
+ * Imports a script using Node's require() API.
+ *
+ * @param {string} src The script source.
+ * @return {boolean} True if the script was imported, false otherwise.
+ */
+global.CLOSURE_IMPORT_SCRIPT = function(src) {
+ // Sources are always expressed relative to closure's base.js, but
+ // require() is always relative to the current source.
+ require('./../' + src);
+ return true;
+};
+
+
+// Declared here so it can be used to require base.js
+function nodeGlobalRequire(file) {
+ process.binding('evals').NodeScript.runInThisContext.call(
+ global, fs.readFileSync(file), file);
+}
+
+
+// Load Closure's base.js into memory. It is assumed base.js is in the
+// directory above this directory given this script's location in
+// bootstrap/nodejs.js.
+nodeGlobalRequire(path.resolve(__dirname, '..', 'base.js'));
+
+
+/**
+ * Bootstraps a file into the global scope.
+ *
+ * This is strictly for cases where normal require() won't work,
+ * because the file declares global symbols with 'var' that need to
+ * be added to the global scope.
+ * @suppress {missingProvide}
+ *
+ * @param {string} file The path to the file.
+ */
+goog.nodeGlobalRequire = nodeGlobalRequire;
+
Please sign in to comment.
Something went wrong with that request. Please try again.