Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide UMD compatibility. #50

Merged
merged 1 commit into from
Aug 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,11 @@ def write_variables(ninja):
ninja.variable('license_js', 'src/license.js')
ninja.variable('license', 'cat $license_js')
ninja.variable('preamble', 'true')
ninja.variable('wrapper_js', 'src/wrapper.js')

common_jsflags = [
'--compilation_level ADVANCED_OPTIMIZATIONS',
'--define "COMPILED=true"',
'--define "SPF_COMPILED=true"',
'--output_wrapper "(function(){%output%})();"',
'--manage_closure_dependencies true',
'--process_closure_primitives true',
]
Expand All @@ -208,9 +207,11 @@ def write_variables(ninja):
]
main_jsflags = [
'--closure_entry_point spf.main',
'--output_wrapper_file $wrapper_js',
]
bootloader_jsflags = [
'--closure_entry_point spf.bootloader',
'--output_wrapper "(function(){%output%})();"',
]
ninja.variable('prod_jsflags', ' '.join(prod_jsflags))
ninja.variable('debug_jsflags', ' '.join(debug_jsflags))
Expand Down Expand Up @@ -296,6 +297,7 @@ def write_rules(ninja):

def write_targets(ninja):
license_js = '$license_js'
wrapper_js = '$wrapper_js'

ninja.newline()
ninja.comment('Libraries.')
Expand Down Expand Up @@ -477,14 +479,14 @@ def write_targets(ninja):
ninja.comment('Main.')
ninja.build('$builddir/spf.js', 'jscompile', js_srcs,
variables=[('flags', '$prod_jsflags $main_jsflags')],
implicit=[jscompiler_jar, license_js])
implicit=[jscompiler_jar, license_js, wrapper_js])
ninja.build('$builddir/debug-spf.js', 'jscompile', js_srcs,
variables=[('flags', '$debug_jsflags $main_jsflags')],
implicit=[jscompiler_jar, license_js])
implicit=[jscompiler_jar, license_js, wrapper_js])
ninja.build('$builddir/tracing-spf.js', 'jscompile', js_srcs,
variables=[('flags', '$trace_jsflags $main_jsflags'),
('preamble', 'head -n 6 ' + wtf_shim)],
implicit=[jscompiler_jar, license_js])
implicit=[jscompiler_jar, license_js, wrapper_js])

ninja.newline()
ninja.comment('Bootloader.')
Expand Down
4 changes: 0 additions & 4 deletions src/client/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ goog.provide('spf');
var SPF_BOOTLOADER = false;


/** @define {boolean} Compiler flag to remove development code. */
var SPF_COMPILED = false;


/** @define {boolean} Compiler flag to include debugging code. */
var SPF_DEBUG = true;

Expand Down
18 changes: 8 additions & 10 deletions src/client/bootloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ goog.require('spf.net.script');


// Create the bootloader API by exporting aliased functions.
/** @private {Object} */
/** @private {!Object} */
spf.bootloader.api_ = {
'script': {
// The bootloader API.
Expand All @@ -38,13 +38,11 @@ spf.bootloader.api_ = {
'path': spf.net.script.path
}
};
if (!SPF_COMPILED) {
// When not compiled, mixin the API to the existing namespace for development.
for (var key in spf.bootloader.api_) {
// Work around the "incomplete alias" warning.
eval('spf[key] = spf.bootloader.api_[key]');
}
} else {
// When compiled for a production/debug build, isolate access to the API.
window['spf'] = spf.bootloader.api_;
// For a production/debug build, isolate access to the API.
// For a development build, mixin the API to the existing namespace.
var global = this;
global['spf'] = global['spf'] || {};
var api = global['spf'];
for (var fn in spf.bootloader.api_) {
api[fn] = spf.bootloader.api_[fn];
}
45 changes: 16 additions & 29 deletions src/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ spf.main.discover_();
// Create the API by exporting aliased functions.
// Core API functions are available on the top-level namespace.
// Extra API functions are available on second-level namespaces.
/** @private {Object} */
/** @private {!Object} */
spf.main.api_ = {
'init': spf.main.init,
'dispose': spf.main.dispose,
Expand All @@ -107,7 +107,7 @@ spf.main.api_ = {
'process': spf.nav.response.process, // TODO: Remove after deprecation.
'prefetch': spf.nav.prefetch
};
/** @private {Object} */
/** @private {!Object} */
spf.main.extra_ = {
'script': {
// The bootloader API.
Expand Down Expand Up @@ -145,34 +145,21 @@ spf.main.extra_ = {
'prefetch': spf.net.style.prefetch
}
};
if (!SPF_COMPILED) {
// When not compiled, mixin the API to the existing namespace for development.
// Use eval to work around the "incomplete alias" warning.
for (var fn1 in spf.main.api_) {
eval('spf[fn1] = spf.main.api_[fn1]');
}
for (var ns in spf.main.extra_) {
for (var fn2 in spf.main.extra_[ns]) {
eval('spf[ns] = spf[ns] || {};');
eval('spf[ns][fn2] = spf.main.extra_[ns][fn2]');
}
// For a production/debug build, isolate access to the API.
// For a development build, mixin the API to the existing namespace.
var global = this;
global['spf'] = global['spf'] || {};
var api = global['spf'];
for (var fn1 in spf.main.api_) {
api[fn1] = spf.main.api_[fn1];
}
// Use two-stage exporting to allow aliasing the intermediate namespaces
// created by the bootloader (e.g. s = spf.script; s.load(...)).
for (var ns in spf.main.extra_) {
for (var fn2 in spf.main.extra_[ns]) {
api[ns] = api[ns] || {};
api[ns][fn2] = spf.main.extra_[ns][fn2];
}
} else {
// When compiled for a production/debug build, isolate access to the API.
(function() {
window['spf'] = window['spf'] || {};
for (var fn1 in spf.main.api_) {
window['spf'][fn1] = spf.main.api_[fn1];
}
// Use two-stage exporting to allow aliasing the intermediate namespaces
// created by the bootloader (e.g. s = spf.script; s.load(...)).
for (var ns in spf.main.extra_) {
for (var fn2 in spf.main.extra_[ns]) {
window['spf'][ns] = window['spf'][ns] || {};
window['spf'][ns][fn2] = spf.main.extra_[ns][fn2];
}
}
})();
}

// Signal that the API is ready with custom event. Only supported in IE 9+.
Expand Down
1 change: 1 addition & 0 deletions src/wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(){%output%if(typeof define=='function'&&define.amd)define(spf);else if(typeof exports=='object')for(var f in spf)exports[f]=spf[f];})();