From 451ddf6003d89fc69b23d5c4a2c362e1acf11a25 Mon Sep 17 00:00:00 2001
From: pyrsmk
Date: Wed, 4 Jan 2017 16:17:16 +0100
Subject: [PATCH] Update build system; fix (probably) a several loading bug
---
README.md | 8 +--
bower.json | 4 +-
gulpfile.js | 108 ++++++++++++++++++++++++---------------
toast.js => lib/toast.js | 16 +++---
lib/toast.min.js | 1 +
package.json | 20 +++++---
src/toast.js | 7 ++-
tests/index.html | 2 +-
tests/tests.js | 2 +-
toast.min.js | 1 -
10 files changed, 97 insertions(+), 72 deletions(-)
rename toast.js => lib/toast.js (91%)
create mode 100644 lib/toast.min.js
delete mode 100644 toast.min.js
diff --git a/README.md b/README.md
index a3c300a..c5522b8 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-toast 2.1.1
+toast 2.2.0
===========
Toast is a tiny resource loader for JS and CSS files.
@@ -11,14 +11,8 @@ You can pick the minified library or install it with :
```
npm install pyrsmk-toast
bower install toast
-jam install pyrsmk-toast
```
-Changes from v1
----------------
-
-Be careful! The loading state detector has been redesigned and additional loading verification callbacks have been dropped.
-
Syntax
------
diff --git a/bower.json b/bower.json
index 47b9625..e652299 100644
--- a/bower.json
+++ b/bower.json
@@ -1,7 +1,7 @@
{
"name": "toast",
"description": "A simple CSS/JS resource loader",
- "main": "toast.js",
+ "main": "lib/toast.js",
"repository": {
"type": "git",
"url": "git://github.com/pyrsmk/toast.git"
@@ -22,7 +22,7 @@
".gitignore",
"nodes_modules/",
"tests/",
- "Gruntfile.js",
+ "gulpfile.js",
"package.json"
]
}
diff --git a/gulpfile.js b/gulpfile.js
index bd7a203..90c04f3 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1,59 +1,85 @@
-var fs = require('fs'),
- gulp = require('gulp'),
- jshint = require('gulp-jshint'),
- uglify = require('gulp-uglify'),
- replace = require('gulp-replace'),
- rename = require('gulp-rename'),
- merge = require('merge2'),
+var gulp = require('gulp'),
shell = require('gulp-shell'),
- umd = require('gulp-umd');
-
-var name = 'toast',
- version = fs.readFileSync('src/'+name+'.js', {encoding:'utf8'}).match(/^\/\*\! [\w-]+ ([0-9.]+)/)[1];
+ name = __dirname.match(/\\([^\\]*)$/)[1],
+ version = require('./package.json').version;
// ======================================== gulp version
gulp.task('version', function() {
- var streams = merge();
- streams.add(
- gulp.src( 'package.json' )
- .pipe( replace(/"version": "[0-9.]+",/, '"version": "'+version+'",') )
- .pipe( gulp.dest('.') )
- );
- streams.add(
- gulp.src( 'README.md' )
- .pipe( replace(/^(\w+) [0-9.]+/, '$1 '+version) )
- .pipe( gulp.dest('.') )
- );
- return streams;
+ var replace = require('gulp-replace');
+
+ return gulp.src( './README.md' )
+ .pipe( replace(/^([\w-]+) [0-9.]+/, '$1 ' + version) )
+ .pipe( gulp.dest('.') );
+
+});
+
+// ======================================== gulp lint
+
+gulp.task('lint', function() {
+ var lint = require('gulp-eslint'),
+ flow = require('gulp-flowtype');
+
+ return gulp.src( './src/' + name + '.js' )
+ .pipe( flow() )
+ .pipe( lint({
+ rules: {
+ "array-bracket-spacing": [2, "never"],
+ "block-scoped-var": 2,
+ "camelcase": 1,
+ "computed-property-spacing": [2, "never"],
+ "curly": 2,
+ "max-depth": [1, 3],
+ "max-statements": [1, 30],
+ "new-cap": 1,
+ "no-extend-native": 2,
+ "no-use-before-define": [2, "nofunc"],
+ "quotes": [2, "single", "avoid-escape"],
+ "semi": [2, "always"],
+ "space-unary-ops": 2
+ }
+ }) )
+ .pipe( lint.format() );
+
});
// ======================================== gulp build
-gulp.task('build', ['version'], function() {
- return gulp.src( './src/*.js' )
- .pipe( jshint({
- loopfunc: true,
- boss: true
- }) )
- .pipe( jshint.reporter('jshint-stylish') )
- .pipe( umd({
- exports: function() { return name; },
- namespace: function() { return name; }
- }) )
- .pipe( gulp.dest('.') )
- .pipe( uglify() )
- .pipe( rename({suffix: '.min'}) )
- .pipe( gulp.dest('.') );
+gulp.task('build', ['version', 'lint'], function() {
+ var concat = require('gulp-concat'),
+ uglify = require('gulp-uglify'),
+ rename = require('gulp-rename'),
+ umd = require('gulp-umd'),
+ add = require('gulp-add-src'),
+ resolve = require('resolve'),
+ _ = require('lodash'),
+ dependencies = [];
+
+ (_.keys(require('./package.json').dependencies) || []).forEach(function(dep) {
+ dependencies.unshift(resolve.sync(dep));
+ });
+
+ return gulp.src( './src/**' )
+ .pipe( umd({
+ template: './node_modules/umd-templates/patterns/returnExportsGlobal.js',
+ namespace: function() {
+ return name;
+ }
+ }) )
+ .pipe( add.prepend(dependencies) )
+ .pipe( concat(name + '.js') )
+ .pipe( gulp.dest('./lib/') )
+ .pipe( uglify() )
+ .pipe( rename(name + '.min.js') )
+ .pipe( gulp.dest('./lib/') );
});
// ======================================== gulp publish
gulp.task('publish', shell.task([
- "git tag -a "+version+" -m '"+version+"'",
+ "git tag -a " + version + " -m '" + version + "'",
'git push --tags',
- 'npm publish',
- 'jam publish'
+ 'npm publish'
]));
// ======================================== gulp
diff --git a/toast.js b/lib/toast.js
similarity index 91%
rename from toast.js
rename to lib/toast.js
index 59458e7..5a24443 100644
--- a/toast.js
+++ b/lib/toast.js
@@ -1,17 +1,17 @@
-;(function(root, factory) {
+(function(root, factory) {
if (typeof define === 'function' && define.amd) {
- define([], factory);
+ define([], function() {
+ return (root.toast = factory());
+ });
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.toast = factory();
}
}(this, function() {
-/*! toast 2.1.1 (https://github.com/pyrsmk/toast) */
-
var handled_resources = {};
-function toast() {
+function Toast() {
var head = document.getElementsByTagName('head')[0],
// Load as much resources as we can
@@ -70,7 +70,7 @@ function toast() {
// Create SCRIPT element
node = document.createElement('script');
node.src = resource;
- node.type = 'text/javascript';
+ node.async = false;
head.appendChild(node);
// Watch loading state
var version = navigator.appVersion.match(/MSIE (\d)/);
@@ -79,6 +79,7 @@ function toast() {
node.onreadystatechange = function() {
if(/ded|co/.test(this.readyState)) {
handled_resources[resource] = true;
+ node.onreadystatechange = null;
}
};
}
@@ -86,6 +87,7 @@ function toast() {
// Other browsers
node.onload = function() {
handled_resources[resource] = true;
+ node.onload = null;
};
}
break;
@@ -135,5 +137,5 @@ function toast() {
// Load resources
loadResources(arguments);
}
-return toast;
+return Toast;
}));
diff --git a/lib/toast.min.js b/lib/toast.min.js
new file mode 100644
index 0000000..c4ce261
--- /dev/null
+++ b/lib/toast.min.js
@@ -0,0 +1 @@
+!function(e,t){"function"==typeof define&&define.amd?define([],function(){return e.toast=t()}):"object"==typeof exports?module.exports=t():e.toast=t()}(this,function(){function e(){var e=document.getElementsByTagName("head")[0],n=function(t){if(e){if(t.length){for(var a,r,c=-1;a=t[++c];)if("string"==typeof a)o(a);else if("function"==typeof a){r=a;break}i(r,Array.prototype.slice.call(t,c+1))}}else setTimeout(function(){n(t)},50)},o=function(n){var o,i,r=/\.(\w+)$/.exec(n),c=/^\[(\w+)\](.+)/.exec(n);if(null!==c)o=c[1],n=c[2];else{if(null===r)return;o=r[1]}if(!(n in t))switch(t[n]=!1,o){case"js":i=document.createElement("script"),i.src=n,i.async=!1,e.appendChild(i);var f=navigator.appVersion.match(/MSIE (\d)/);null!==f&&parseInt(f[1],10)<9?i.onreadystatechange=function(){/ded|co/.test(this.readyState)&&(t[n]=!0,i.onreadystatechange=null)}:i.onload=function(){t[n]=!0,i.onload=null};break;case"css":i=document.createElement("link"),i.rel="styleSheet",i.href=n,e.appendChild(i),a(i,n);break;default:return void delete t[n]}},i=function(e,o){for(var a in t)if(!t[a])return void setTimeout(function(){i(e,o)},50);"function"==typeof e&&e(),n(o)},a=function(e,n){e.sheet||e.styleSheet?t[n]=!0:setTimeout(function(){a(e,n)},50)};n(arguments)}var t={};return e});
\ No newline at end of file
diff --git a/package.json b/package.json
index f2cb48e..72eb0e6 100644
--- a/package.json
+++ b/package.json
@@ -1,13 +1,13 @@
{
"name": "pyrsmk-toast",
"description": "A simple CSS/JS resource loader",
- "version": "2.1.1",
+ "version": "2.2.0",
"author": "Aurélien Delogu (http://dreamysource.fr)",
"repository": {
"type": "git",
"url": "https://github.com/pyrsmk/toast.git"
},
- "main": "toast.js",
+ "main": "lib/toast.js",
"keywords": [
"css",
"js",
@@ -20,14 +20,18 @@
},
"homepage": "http://dreamysource.io/project/toast",
"devDependencies": {
- "gulp": "^3.8.11",
- "gulp-jshint": "^1.11.0",
+ "gulp": "^3.9.0",
+ "gulp-add-src": "^0.2.0",
+ "gulp-concat": "^2.6.0",
+ "gulp-eslint": "^3.0.1",
+ "gulp-flowtype": "^1.0.0",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.3",
- "gulp-shell": "^0.4.1",
- "gulp-uglify": "^1.2.0",
+ "gulp-shell": "^0.4.2",
+ "gulp-uglify": "^2.0.0",
"gulp-umd": "^0.2.0",
- "jshint-stylish": "^2.0.0",
- "merge2": "^0.3.5"
+ "lodash": "^4.16.4",
+ "resolve": "^1.1.7",
+ "umd-templates": "0.0.3"
}
}
diff --git a/src/toast.js b/src/toast.js
index 290c3fd..24e1c5e 100644
--- a/src/toast.js
+++ b/src/toast.js
@@ -1,8 +1,6 @@
-/*! toast 2.1.1 (https://github.com/pyrsmk/toast) */
-
var handled_resources = {};
-function toast() {
+function Toast() {
var head = document.getElementsByTagName('head')[0],
// Load as much resources as we can
@@ -61,7 +59,6 @@ function toast() {
// Create SCRIPT element
node = document.createElement('script');
node.src = resource;
- node.type = 'text/javascript';
head.appendChild(node);
// Watch loading state
var version = navigator.appVersion.match(/MSIE (\d)/);
@@ -70,6 +67,7 @@ function toast() {
node.onreadystatechange = function() {
if(/ded|co/.test(this.readyState)) {
handled_resources[resource] = true;
+ node.onreadystatechange = null;
}
};
}
@@ -77,6 +75,7 @@ function toast() {
// Other browsers
node.onload = function() {
handled_resources[resource] = true;
+ node.onload = null;
};
}
break;
diff --git a/tests/index.html b/tests/index.html
index 29f2b87..9917936 100644
--- a/tests/index.html
+++ b/tests/index.html
@@ -9,7 +9,7 @@
-
+
diff --git a/tests/tests.js b/tests/tests.js
index fc81a7e..a8c436c 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -37,7 +37,7 @@ QUnit.test('Several resources', function(assert) {
function() {
assert.ok(typeof jQuery == 'function', 'jQuery loaded');
assert.ok(typeof qwest == 'object', 'qwest loaded [explicit]');
- assert.ok(typeof Horizon == 'object', 'Horizon loaded');
+ assert.ok(typeof Horizon == 'function', 'Horizon loaded');
assert.ok(document.styleSheets.length == 3, 'Google font loaded [explicit]');
done1();
diff --git a/toast.min.js b/toast.min.js
deleted file mode 100644
index db05163..0000000
--- a/toast.min.js
+++ /dev/null
@@ -1 +0,0 @@
-!function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():e.toast=t()}(this,function(){function e(){var e=document.getElementsByTagName("head")[0],n=function(t){if(e){if(t.length){for(var r,a,c=-1;r=t[++c];)if("string"==typeof r)i(r);else if("function"==typeof r){a=r;break}o(a,Array.prototype.slice.call(t,c+1))}}else setTimeout(function(){n(t)},50)},i=function(n){var i,o,a=/\.(\w+)$/.exec(n),c=/^\[(\w+)\](.+)/.exec(n);if(null!==c)i=c[1],n=c[2];else{if(null===a)return;i=a[1]}if(!(n in t))switch(t[n]=!1,i){case"js":o=document.createElement("script"),o.src=n,o.type="text/javascript",e.appendChild(o);var f=navigator.appVersion.match(/MSIE (\d)/);null!==f&&parseInt(f[1],10)<9?o.onreadystatechange=function(){/ded|co/.test(this.readyState)&&(t[n]=!0)}:o.onload=function(){t[n]=!0};break;case"css":o=document.createElement("link"),o.rel="styleSheet",o.href=n,e.appendChild(o),r(o,n);break;default:return void delete t[n]}},o=function(e,i){for(var r in t)if(!t[r])return void setTimeout(function(){o(e,i)},50);"function"==typeof e&&e(),n(i)},r=function(e,n){e.sheet||e.styleSheet?t[n]=!0:setTimeout(function(){r(e,n)},50)};n(arguments)}var t={};return e});
\ No newline at end of file