This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
loader metadata per lang #616
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ad69c2
fully functional yui configuration for debugging, filters, fetchCSS, …
caridy 48b70c9
adding support for logLevel under applicatoin.json->yui->config->logL…
caridy 038ef1e
Merge branch 'develop-perf' of git://github.com/yahoo/mojito into loa…
caridy 4deb1b0
splitting meta per lang. supporting default langs. removing unnecesar…
caridy 50f7004
wildcard lang * should include all available langs, this is useful fo…
caridy a64623f
adding yui-sandbox module to isolate YUI objects. using yui-sandbox a…
caridy dc674e2
blessing the readFileSync since it happens during the boot time.
caridy 000013f
review. adding support for simple locale like [en] instead of just [e…
caridy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ dependencies computations efficiently. | |
var libfs = require('fs'), | ||
mime = require('mime'), | ||
libpath = require('path'), | ||
YUI = require(libpath.join(__dirname, '..', '..', 'yui-sandbox.js')).getYUI(), | ||
parseUrl = require('url').parse, | ||
logger, | ||
NAME = 'ComboHandler', | ||
|
@@ -46,6 +47,10 @@ var libfs = require('fs'), | |
MODULE_META_PRIVATE_ENTRIES = ['after', 'expanded', 'supersedes', 'ext', '_parsed', '_inspected', | ||
'skinCache', 'langCache'], | ||
|
||
REGEX_LANG_TOKEN = /\"\{langToken\}\"/g, | ||
REGEX_LANG_PATH = /\{langPath\}/g, | ||
REGEX_LOCALE = /\_([a-z]{2}(-[A-Z]{2})?)$/, | ||
|
||
DEFAULT_HEADERS = { | ||
'.js': { | ||
'Content-Type': 'application/javascript; charset=utf-8' | ||
|
@@ -177,23 +182,54 @@ function clearCache(key) { | |
} | ||
} | ||
|
||
function processMeta(resolvedMods, modules, expanded_modules, conditions) { | ||
function processMeta(resolvedMods, modules, expanded_modules, langs, conditions) { | ||
var m, | ||
l, | ||
i, | ||
module; | ||
module, | ||
name, | ||
mod, | ||
lang, | ||
bundle; | ||
|
||
for (m in resolvedMods) { | ||
if (resolvedMods.hasOwnProperty(m)) { | ||
module = resolvedMods[m]; | ||
|
||
mod = name = module.name; | ||
bundle = name.indexOf('lang/') === 0; | ||
lang = bundle && REGEX_LOCALE.exec(name); | ||
|
||
if (lang) { | ||
mod = mod.slice(0, lang.index); // eg. lang/foo_en-US -> lang/foo | ||
lang = lang[1]; | ||
// TODO: validate lang | ||
langs.push(lang); // eg. en-US | ||
} | ||
mod = bundle ? mod.slice(5) : mod; // eg. lang/foo -> foo | ||
|
||
// language manipulation | ||
// TODO: this routine is very restrictive, and we might want to | ||
// make it optional later on. | ||
if (module.lang) { | ||
module.lang = ['{langToken}']; | ||
} | ||
if (bundle) { | ||
module.owner = mod; | ||
// applying some extra optimizations | ||
module.langPack = lang || '*'; | ||
module.intl = true; | ||
delete module.expanded_map; | ||
} | ||
|
||
if (module.condition && module.condition.test) { | ||
conditions[module.name] = module.condition.test.toString(); | ||
module.condition.test = "{" + module.name + "}"; | ||
} | ||
|
||
modules[module.name] = {}; | ||
if (module.type === 'css') { | ||
modules[module.name] = 'css'; | ||
modules[module.name].type = 'css'; | ||
} | ||
for (i = 0; i < MODULE_META_ENTRIES.length; i += 1) { | ||
if (module[MODULE_META_ENTRIES[i]]) { | ||
|
@@ -211,6 +247,25 @@ function processMeta(resolvedMods, modules, expanded_modules, conditions) { | |
} | ||
|
||
|
||
function produceMeta(meta, name) { | ||
var token = '', | ||
path = '', | ||
lang = REGEX_LOCALE.exec(name); | ||
|
||
if (lang && lang[1] && meta[lang[1]]) { | ||
lang = lang[1]; // eg. en-US | ||
token = '"' + lang + '"'; | ||
path = '_' + lang; | ||
meta = meta[lang]; | ||
} else { | ||
meta = meta['*']; // default in case they use invalid lang | ||
} | ||
return LOADER_MODULE_TEMPLATE | ||
.replace('{metadata}', meta) | ||
.replace(REGEX_LANG_TOKEN, token) | ||
.replace(REGEX_LANG_PATH, path); | ||
} | ||
|
||
/* | ||
* Static file server. | ||
* | ||
|
@@ -226,59 +281,103 @@ function processMeta(resolvedMods, modules, expanded_modules, conditions) { | |
* @return {Function} | ||
* @api public | ||
*/ | ||
function staticProvider(store, globalLogger, Y) { | ||
logger = globalLogger; | ||
var appConfig = store.getStaticAppConfig(), | ||
function staticProvider(store, globalLogger) { | ||
var appConfig = store.getAppConfig(store.getStaticContext()), | ||
options = appConfig.staticHandling || {}, | ||
cache = options.cache, | ||
maxAge = options.maxAge, | ||
urls = store.getAllModulesURLs(), | ||
lang, | ||
|
||
// collecting client side metadata | ||
mojits = store.yui.getConfigAllMojits('client', {}), | ||
shared = store.yui.getConfigShared('client', {}, false), | ||
modules_config = Y.merge((mojits.modules || {}), (shared.modules || {})), | ||
modules_config, | ||
Y, | ||
loader, | ||
resolved, | ||
appMetaData, | ||
appResolvedMetaData, | ||
appMetaData = {}, | ||
appResolvedMetaData = {}, | ||
|
||
// other structures | ||
langs = ['*'], // language wildcard | ||
expanded_modules = {}, // expanded meta (including fullpaths) | ||
modules = {}, // regular meta (a la loader-yui3) | ||
conditions = {}, // hash to store conditional functions | ||
name; | ||
name, | ||
i; | ||
|
||
logger = globalLogger; | ||
|
||
Y = YUI({ | ||
fetchCSS: true, | ||
combine: true, | ||
base: "/combo?", | ||
comboBase: "/combo?", | ||
root: "" | ||
}, ((appConfig.yui && appConfig.yui.config && appConfig.yui.config.config) || {})); | ||
|
||
modules_config = Y.merge((mojits.modules || {}), (shared.modules || {})); | ||
Y.applyConfig({ | ||
modules: modules_config, | ||
useSync: true | ||
}); | ||
Y.use('loader'); | ||
|
||
// using the loader at the server side to compute the loader metadata | ||
// to avoid loading the whole thing on demand. | ||
loader = new Y.Loader(Y.merge({ | ||
ignoreRegistered: true, | ||
modules: modules_config | ||
}, { | ||
loader = new Y.Loader({ | ||
require: Y.Object.keys(modules_config) | ||
})); | ||
}); | ||
resolved = loader.resolve(true); | ||
|
||
// Need to make a copy otherwise the changes we make deep in this structure | ||
// will bleed into the loader, especially causing condition.test to fail. | ||
resolved = Y.mojito.util.copy(resolved); | ||
|
||
if (cache && !maxAge) { | ||
maxAge = cache; | ||
} | ||
maxAge = maxAge || 0; | ||
|
||
processMeta(resolved.jsMods, modules, expanded_modules, conditions); | ||
processMeta(resolved.cssMods, modules, expanded_modules, conditions); | ||
processMeta(resolved.jsMods, modules, expanded_modules, langs, conditions); | ||
processMeta(resolved.cssMods, modules, expanded_modules, langs, conditions); | ||
|
||
for (i = 0; i < langs.length; i += 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slight performance gain if we cache length before the loop. |
||
lang = langs[i]; | ||
|
||
appMetaData[lang] = {}; | ||
appResolvedMetaData[lang] = {}; | ||
|
||
for (name in expanded_modules) { | ||
if (expanded_modules.hasOwnProperty(name)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slight performance gain to cache expanded_modules[name] as 'slot' or whatever rather than reaccessing it repeatedly through the [] operator. |
||
if (expanded_modules[name].owner && | ||
!expanded_modules[expanded_modules[name].owner]) { | ||
// if there is not a module corresponding with the lang pack | ||
// that means the controller doesn't have client affinity, | ||
// in that case, we don't need to ship it. | ||
continue; | ||
} | ||
if ((lang === '*') || | ||
(expanded_modules[name].langPack === '*') || | ||
(!expanded_modules[name].langPack) || | ||
(lang === expanded_modules[name].langPack)) { | ||
|
||
appMetaData[lang][name] = modules[name]; | ||
appResolvedMetaData[lang][name] = expanded_modules[name]; | ||
|
||
} | ||
} | ||
} | ||
|
||
appMetaData = JSON.stringify(modules); | ||
appResolvedMetaData = JSON.stringify(expanded_modules); | ||
appMetaData[lang] = JSON.stringify(appMetaData[lang]); | ||
appResolvedMetaData[lang] = JSON.stringify(appResolvedMetaData[lang]); | ||
|
||
for (name in conditions) { | ||
if (conditions.hasOwnProperty(name)) { | ||
appMetaData = appMetaData.replace('"{' + name + '}"', conditions[name]); | ||
appResolvedMetaData = appResolvedMetaData.replace('"{' + name + '}"', conditions[name]); | ||
for (name in conditions) { | ||
if (conditions.hasOwnProperty(name)) { | ||
appMetaData[lang] = appMetaData[lang] | ||
.replace('"{' + name + '}"', conditions[name]); | ||
appResolvedMetaData[lang] = appResolvedMetaData[lang] | ||
.replace('"{' + name + '}"', conditions[name]); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
@@ -303,7 +402,7 @@ function staticProvider(store, globalLogger, Y) { | |
return next(); | ||
} | ||
|
||
logger.log('serving static path: ' + url.pathname, 'debug', 'static-handler'); | ||
logger.log('serving combo url: ' + url.query, 'debug', NAME); | ||
|
||
// YIV might be messing around with the querystring params | ||
// trying to formalize them by adding = and transforming / | ||
|
@@ -363,19 +462,21 @@ function staticProvider(store, globalLogger, Y) { | |
// so errors can be found early on. | ||
for (i = 0; i < files.length; i += 1) { | ||
|
||
// something like foo/bar-min.js should become just "bar" | ||
module = libpath.basename(files[i], ext). | ||
replace(/\-(min|debug)$/, ''); | ||
// something like: | ||
// - foo/bar-min.js becomes "bar" | ||
// - foo/lang/bar_en-US.js becomes "lang/bar_en-US" | ||
module = (files[i].indexOf('/lang/') >= 0 ? 'lang/' : '') + | ||
libpath.basename(files[i], ext).replace(/\-(min|debug)$/, ''); | ||
|
||
if (module === 'loader-app-base') { | ||
if (module.indexOf('loader-app-base') === 0) { | ||
result[i] = { | ||
fullpath: module, | ||
content: LOADER_MODULE_TEMPLATE.replace('{metadata}', appMetaData) | ||
content: produceMeta(appMetaData, module) | ||
}; | ||
} else if (module === 'loader-app-full') { | ||
} else if (module.indexOf('loader-app-full') === 0) { | ||
result[i] = { | ||
fullpath: module, | ||
content: LOADER_MODULE_TEMPLATE.replace('{metadata}', appResolvedMetaData) | ||
content: produceMeta(appResolvedMetaData, module) | ||
}; | ||
} else if (urls[module]) { | ||
result[i] = { | ||
|
@@ -418,13 +519,6 @@ function staticProvider(store, globalLogger, Y) { | |
} | ||
} | ||
} | ||
} else { | ||
logger.log('Error producing combo: ' + files, 'error', NAME); | ||
// this should never happen, because an invalid | ||
// module error should happen before reaching this. | ||
res.writeHead(400); | ||
res.end(undefined); | ||
return; | ||
} | ||
|
||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is fine for now, but i'd like to see us adopt a pattern that doesn't create objects that might not be necessary. so anywhere we could check for null/undefined instead of creating objects which affect memory consumption we should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed.