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

Issue w/ head.load #522

Merged
merged 4 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ examples/**/_package/
*.min.js
client/src/renderers/support/jade-runtime.js
src/components/oc-client/src/head.load.js
src/components/oc-client/src/l.js
src/components/oc-client/_package/**/*
src/components/base-component-handlebars/_package/**/*
src/components/base-component-handlebars/server.js
Expand Down
8 changes: 8 additions & 0 deletions src/components/oc-client/index.head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script src="src/head.load.js"></script>
<script src="src/oc-client.js"></script>
<script>
oc.require('https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.js', () => {
console.log('angular is ready');
console.log('angular:', angular);
});
</script>
8 changes: 8 additions & 0 deletions src/components/oc-client/index.l.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script src="src/l.js"></script>
<script src="src/oc-client.js"></script>
<script>
oc.require('https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.js', () => {
console.log('angular is ready');
console.log('angular:', angular);
});
</script>
152 changes: 152 additions & 0 deletions src/components/oc-client/src/l.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//https://github.com/malko/l.js
; (function (window, undefined) {
/*
* script for js/css parallel loading with dependancies management
* @author Jonathan Gotti < jgotti at jgotti dot net >
* @licence dual licence mit / gpl
* @since 2012-04-12
* @todo add prefetching using text/cache for js files
* @changelog
* - 2016-08-22 - remove global eval and fix issue #13
* - 2014-06-26 - bugfix in css loaded check when hashbang is used
* - 2014-05-25 - fallback support rewrite + null id bug correction + minification work
* - 2014-05-21 - add cdn fallback support with hashbang url
* - 2014-05-22 - add support for relative paths for stylesheets in checkLoaded
* - 2014-05-21 - add support for relative paths for scripts in checkLoaded
* - 2013-01-25 - add parrallel loading inside single load call
* - 2012-06-29 - some minifier optimisations
* - 2012-04-20 - now sharp part of url will be used as tag id
* - add options for checking already loaded scripts at load time
* - 2012-04-19 - add addAliases method
* @note coding style is implied by the target usage of this script not my habbits
*/
var isA = function (a, b) { return a instanceof (b || Array); }
//-- some minifier optimisation
, D = document
, getElementsByTagName = 'getElementsByTagName'
, length = 'length'
, readyState = 'readyState'
, onreadystatechange = 'onreadystatechange'
//-- get the current script tag for further evaluation of it's eventual content
, scripts = D[getElementsByTagName]("script")
, scriptTag = scripts[scripts[length] - 1]
, script = scriptTag.innerHTML.replace(/^\s+|\s+$/g, '')
;
//avoid multiple inclusion to override current loader but allow tag content evaluation
if (!window.ljs) {
var checkLoaded = scriptTag.src.match(/checkLoaded/) ? 1 : 0
//-- keep trace of header as we will make multiple access to it
, header = D[getElementsByTagName]("head")[0] || D.documentElement
, urlParse = function (url) {
var parts = {}; // u => url, i => id, f = fallback
parts.u = url.replace(/#(=)?([^#]*)?/g, function (m, a, b) { parts[a ? 'f' : 'i'] = b; return ''; });
return parts;
}
, appendElmt = function (type, attrs, cb) {
var e = D.createElement(type), i;
if (cb) { //-- this is not intended to be used for link
if (e[readyState]) {
e[onreadystatechange] = function () {
if (e[readyState] === "loaded" || e[readyState] === "complete") {
e[onreadystatechange] = null;
cb();
}
};
} else {
e.onload = cb;
}
}
for (i in attrs) { attrs[i] && (e[i] = attrs[i]); }
header.appendChild(e);
// return e; // unused at this time so drop it
}
, load = function (url, cb) {
if (this.aliases && this.aliases[url]) {
var args = this.aliases[url].slice(0);
isA(args) || (args = [args]);
cb && args.push(cb);
return this.load.apply(this, args);
}
if (isA(url)) { // parallelized request
for (var l = url[length]; l--;) {
this.load(url[l]);
}
cb && url.push(cb); // relaunch the dependancie queue
return this.load.apply(this, url);
}
if (url.match(/\.css\b/)) {
return this.loadcss(url, cb);
}
return this.loadjs(url, cb);
}
, loaded = {} // will handle already loaded urls
, loader = {
aliases: {}
, loadjs: function (url, cb) {
var parts = urlParse(url);
url = parts.u;
if (loaded[url] === true) { // already loaded exec cb if any
cb && cb();
return this;
} else if (loaded[url] !== undefined) { // already asked for loading we append callback if any else return
if (cb) {
loaded[url] = (function (ocb, cb) { return function () { ocb && ocb(); cb && cb(); }; })(loaded[url], cb);
}
return this;
}
// first time we ask this script
loaded[url] = (function (cb) { return function () { loaded[url] = true; cb && cb(); }; })(cb);
cb = function () { loaded[url](); };
appendElmt('script', {
type: 'text/javascript', src: url, id: parts.i, onerror: function (error) {
if (parts.f) {
var c = error.currentTarget;
c.parentNode.removeChild(c);
appendElmt('script', { type: 'text/javascript', src: parts.f, id: parts.i }, cb);
}
}
}, cb);
return this;
}
, loadcss: function (url, cb) {
var parts = urlParse(url);
url = parts.u;
loaded[url] || appendElmt('link', { type: 'text/css', rel: 'stylesheet', href: url, id: parts.i });
loaded[url] = true;
cb && cb();
return this;
}
, load: function () {
var argv = arguments, argc = argv[length];
if (argc === 1 && isA(argv[0], Function)) {
argv[0]();
return this;
}
load.call(this, argv[0], argc <= 1 ? undefined : function () { loader.load.apply(loader, [].slice.call(argv, 1)); });
return this;
}
, addAliases: function (aliases) {
for (var i in aliases) {
this.aliases[i] = isA(aliases[i]) ? aliases[i].slice(0) : aliases[i];
}
return this;
}
}
;
if (checkLoaded) {
var i, l, links, url;
for (i = 0, l = scripts[length]; i < l; i++) {
(url = scripts[i].getAttribute('src')) && (loaded[url.replace(/#.*$/, '')] = true);
}
links = D[getElementsByTagName]('link');
for (i = 0, l = links[length]; i < l; i++) {
(links[i].rel === 'stylesheet' || links[i].type === 'text/css') && (loaded[links[i].getAttribute('href').replace(/#.*$/, '')] = true);
}
}
//export ljs (as `head`)
window.head = loader;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only workaround applied 😅

Copy link
Member

@matteofigus matteofigus Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this is temporary right? If we would want to go for it, we can change the library to make it use ljs instead and it should work I guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want me to use ljs instead of head as the name of the loader? sure I'll try to do that. I will also test CSS loading.

// eval inside tag code if any
}
// eval script tag content if needed
scriptTag.src && script && appendElmt('script', { innerHTML: script });
})(window);
119 changes: 77 additions & 42 deletions tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,81 @@ const uglifyJs = require('uglify-js');

const packageJson = require('../package');

module.exports = function(grunt){

grunt.registerTask('build', 'Builds and minifies the oc-client component', function(){

const done = this.async(),
version = packageJson.version,
clientComponentDir = '../src/components/oc-client/',
licenseRow = '/*! OpenComponents client v{0} | (c) 2015-{1} OpenTable, Inc. | {2} */',
licenseLink = 'https://github.com/opentable/oc/tree/master/src/components/oc-client/LICENSES',
license = format(licenseRow, version, new Date().getFullYear(), licenseLink),
headLoad = fs.readFileSync(path.join(__dirname, clientComponentDir, 'src/head.load.js')).toString(),
ocClient = fs.readFileSync(path.join(__dirname, clientComponentDir, 'src/oc-client.js')).toString(),
bundle = format('{0}\n;\n{1}\n;\noc.clientVersion=\'{2}\';', headLoad, ocClient, version),
ocClientPackageInfo = require(clientComponentDir + 'package.json'),
jsonConfig = {spaces: 2};

ocClientPackageInfo.version = version;
fs.writeJsonSync(path.join(__dirname, clientComponentDir, 'package.json'), ocClientPackageInfo, jsonConfig);

const compressed = uglifyJs.minify(bundle, {
fromString: true,
outSourceMap: 'oc-client.min.map'
});

const compressedCode = format('{0}\n{1}', license, compressed.code);

fs.writeFileSync(path.join(__dirname, clientComponentDir, 'src/oc-client.min.js'), compressedCode);
fs.writeFileSync(path.join(__dirname, clientComponentDir, 'src/oc-client.min.map'), compressed.map);
fs.writeFileSync(path.join(__dirname, '../client/src/oc-client.min.js'), compressedCode);

const Local = require('../src/cli/domain/local'),
local = new Local(),
packageOptions = {
componentPath: path.join(__dirname, clientComponentDir),
verbose: false
};

local.package(packageOptions, (err) => {
grunt.log[err ? 'error' : 'ok'](err ? err : 'Client has been built and packaged');
done();
});
});
module.exports = function(grunt) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has been reformatted because of prettier

grunt.registerTask(
'build',
'Builds and minifies the oc-client component',
function() {
const done = this.async(),
version = packageJson.version,
clientComponentDir = '../src/components/oc-client/',
licenseRow =
'/*! OpenComponents client v{0} | (c) 2015-{1} OpenTable, Inc. | {2} */',
licenseLink =
'https://github.com/opentable/oc/tree/master/src/components/oc-client/LICENSES',
license = format(
licenseRow,
version,
new Date().getFullYear(),
licenseLink
),
l = fs
.readFileSync(path.join(__dirname, clientComponentDir, 'src/l.js'))
.toString(),
ocClient = fs
.readFileSync(
path.join(__dirname, clientComponentDir, 'src/oc-client.js')
)
.toString(),
bundle = format(
"{0}\n;\n{1}\n;\noc.clientVersion='{2}';",
l,
ocClient,
version
),
ocClientPackageInfo = require(clientComponentDir + 'package.json'),
jsonConfig = { spaces: 2 };

ocClientPackageInfo.version = version;
fs.writeJsonSync(
path.join(__dirname, clientComponentDir, 'package.json'),
ocClientPackageInfo,
jsonConfig
);

const compressed = uglifyJs.minify(bundle, {
fromString: true,
outSourceMap: 'oc-client.min.map'
});

const compressedCode = format('{0}\n{1}', license, compressed.code);

fs.writeFileSync(
path.join(__dirname, clientComponentDir, 'src/oc-client.min.js'),
compressedCode
);
fs.writeFileSync(
path.join(__dirname, clientComponentDir, 'src/oc-client.min.map'),
compressed.map
);
fs.writeFileSync(
path.join(__dirname, '../client/src/oc-client.min.js'),
compressedCode
);

const Local = require('../src/cli/domain/local'),
local = new Local(),
packageOptions = {
componentPath: path.join(__dirname, clientComponentDir),
verbose: false
};

local.package(packageOptions, err => {
grunt.log[err ? 'error' : 'ok'](
err ? err : 'Client has been built and packaged'
);
done();
});
}
);
};
2 changes: 1 addition & 1 deletion test/configuration/karma-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
'test-settings.js',

// The oc-client bundle
'../../src/components/oc-client/src/head.load.js',
'../../src/components/oc-client/src/l.js',
'../../src/components/oc-client/src/oc-client.js',

// The tests
Expand Down