Skip to content

Commit

Permalink
Simplify async loading scripts.
Browse files Browse the repository at this point in the history
For a more intuitive installation, remove async.coffee and stop
compiling/minifying zxcvbn-async.js. Instead, keep zxcvbn-async.js
readable and edit directly. Also add a zxcvbn-async-bower.js variant to
fix bower setup.
  • Loading branch information
Dan Wheeler committed Jul 12, 2015
1 parent 61cc72a commit d1ceb2d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,5 +1,7 @@
*~
*.js
!zxcvbn.js
!zxcvbn-async.js
!zxcvbn-async-bower.js
test/index_dev.html
node_modules
16 changes: 0 additions & 16 deletions async.coffee

This file was deleted.

10 changes: 7 additions & 3 deletions compile_and_minify.sh
Expand Up @@ -9,7 +9,12 @@ function_wrap () {

echo 'compiling cs -> js'
coffee --compile --bare {matching,scoring,init}.coffee
coffee --compile async.coffee

# bower's async loading uses a different relative path
cp zxcvbn-async.js zxcvbn-async-bower.js
sed -i '' 's/zxcvbn.js/bower_components\/zxcvbn\/zxcvbn.js/' zxcvbn-async-bower.js
sed -i '' '/^\/\//d' zxcvbn-async-bower.js
sed -i '' '/^$/d' zxcvbn-async-bower.js

echo 'compiling js -> js'
# closure's simple optimizations ended up being about 200k better than whitespace-only.
Expand All @@ -18,6 +23,5 @@ echo 'compiling js -> js'
COMPILATION_LEVEL=SIMPLE_OPTIMIZATIONS
cat {matching,scoring,adjacency_graphs,frequency_lists,init}.js | function_wrap > compiled.js
java -jar tools/closure.jar --compilation_level $COMPILATION_LEVEL --js compiled.js --js_output_file zxcvbn.js
java -jar tools/closure.jar --compilation_level $COMPILATION_LEVEL --js async.js --js_output_file zxcvbn-async.js
rm -f compiled.js
echo 'done. produced zxcvbn.js and zxcvbn-async.js'
echo 'done'
17 changes: 17 additions & 0 deletions zxcvbn-async-bower.js
@@ -0,0 +1,17 @@
(function() {
var ZXCVBN_SRC = 'bower_components/zxcvbn/zxcvbn.js';
var async_load = function() {
var first, s;
s = document.createElement('script');
s.src = ZXCVBN_SRC;
s.type = 'text/javascript';
s.async = true;
first = document.getElementsByTagName('script')[0];
return first.parentNode.insertBefore(s, first);
};
if (window.attachEvent != null) {
window.attachEvent('onload', async_load);
} else {
window.addEventListener('load', async_load, false);
}
}).call(this);
32 changes: 31 additions & 1 deletion zxcvbn-async.js
@@ -1 +1,31 @@
(function(){var a;a=function(){var a,b;b=document.createElement("script");b.src="zxcvbn.js";b.type="text/javascript";b.async=!0;a=document.getElementsByTagName("script")[0];return a.parentNode.insertBefore(b,a)};null!=window.attachEvent?window.attachEvent("onload",a):window.addEventListener("load",a,!1)}).call(this);
// cross-browser asynchronous script loading for zxcvbn.
// adapted from http://friendlybit.com/js/lazy-loading-asyncronous-javascript/

// Suggestion: instead of manually configuring this script,
// follow the bower setup instructions (see README) for easier installation and
// updating.

// If you do want to manually include zxcvbn, you'll likely only need to change
// ZXCVBN_SRC to point to the correct relative path from your index.html.
// (this script assumes index.html and zxcvbn.js sit next to each other.)

(function() {
var ZXCVBN_SRC = 'zxcvbn.js';

var async_load = function() {
var first, s;
s = document.createElement('script');
s.src = ZXCVBN_SRC;
s.type = 'text/javascript';
s.async = true;
first = document.getElementsByTagName('script')[0];
return first.parentNode.insertBefore(s, first);
};

if (window.attachEvent != null) {
window.attachEvent('onload', async_load);
} else {
window.addEventListener('load', async_load, false);
}

}).call(this);

0 comments on commit d1ceb2d

Please sign in to comment.