Skip to content

Commit

Permalink
Apply patch on build for supported formats
Browse files Browse the repository at this point in the history
  • Loading branch information
caroqliu committed Feb 23, 2021
1 parent 6e81602 commit 5032f5a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
4 changes: 2 additions & 2 deletions build-system/compile/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const COMMON_GLOBS = [
'node_modules/promise-pjs/package.json',
'node_modules/promise-pjs/promise.mjs',
'node_modules/rrule/dist/es5/rrule.js',
'node_modules/timeago.js/dist/timeago.full.min.js',
'node_modules/timeago.js/lib/lang/*.js',
'node_modules/timeago.js/package.json',
'node_modules/timeago.js/lib/full.patched.js',
'node_modules/timeago.js/lib/lang/*.js',
'node_modules/web-animations-js/package.json',
'node_modules/web-animations-js/web-animations.install.js',
'node_modules/web-activities/package.json',
Expand Down
40 changes: 40 additions & 0 deletions build-system/tasks/update-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,45 @@ function patchResizeObserver() {
writeIfUpdated(patchedName, file);
}

/**
* Patches Timeago by renaming some locales in lang/ in the format supported by
* amp-timeago, as well as files which import these.
*/
function patchTimeago() {
// Copies full.js into a new file that imports updated formats.
const patchedFullName = 'node_modules/timeago.js/lib/full.patched.js';
let file = fs.readFileSync('node_modules/timeago.js/lib/full.js').toString();

// Wrap the contents inside the install function.
file = file
// For some reason Closure fails on this three lines. Babel is fine.
.replace(
`Object.keys(Languages).forEach(function (locale) {
_1.register(locale, Languages[locale]);
});`,
`var timeagoReplacements = {
'en_US': 'en',
'en_short': 'enshort',
'bn_IN': 'inbg',
'id_ID': 'inid',
'hi_IN': 'inhi',
'nb_NO': 'nbno',
'nn_NO': 'nnno',
'pt_BR': 'ptbr',
'zh_CN': 'zhcn',
'zh_TW': 'zhtw',
};
Object.keys(Languages).forEach(function (locale) {
var localeStr = locale;
if (timeagoReplacements[locale]) {
localeStr = timeagoReplacements[locale]
}
_1.register(localeStr, Languages[locale]);
});`
);
writeIfUpdated(patchedFullName, file);
}

/**
* Deletes the map file for rrule, which breaks closure compiler.
* TODO(rsimha): Remove this workaround after a fix is merged for
Expand Down Expand Up @@ -186,6 +225,7 @@ async function updatePackages() {
patchIntersectionObserver();
patchResizeObserver();
removeRruleSourcemap();
patchTimeago();
}

module.exports = {
Expand Down
10 changes: 5 additions & 5 deletions extensions/amp-timeago/0.1/amp-timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {format, getLocale} from './locales';
import {format} from './locales';
import {isLayoutSizeDefined} from '../../../src/layout';
import {
observeWithSharedInOb,
Expand Down Expand Up @@ -52,10 +52,10 @@ export class AmpTimeAgo extends AMP.BaseElement {
);

this.datetime_ = this.element.getAttribute('datetime');
this.locale_ = getLocale(
this.element.getAttribute('locale') ||
this.win.document.documentElement.lang
);
this.locale_ =
this.element.getAttribute('locale')?.toLocaleLowerCase() ||
this.win.document.documentElement.lang;
console.log(this.locale_);
this.title_ = this.element.textContent.trim();

this.element.textContent = '';
Expand Down
45 changes: 3 additions & 42 deletions extensions/amp-timeago/0.1/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
* This file registers all necessary locales supported by amp-timeago.
*/

import {format, register} from 'timeago.js/dist/timeago.full.min.js';
import * as timeago from 'timeago.js/lib/full.patched.js';
import cs from 'timeago.js/lib/lang/cs';
import da from 'timeago.js/lib/lang/da';
import ka from 'timeago.js/lib/lang/ka';
import oc from 'timeago.js/lib/lang/oc';

const {format, register} = timeago;
export {format};

/**
* timeago.full.min.js only contains the following 41 locales:
* full.js only contains the following 41 locales:
* ar, be, bg, bn_IN, ca, de, el, en_short, en_US, es, eu, fa, fi, fr, gl, he,
* hi_IN, hu, id_ID, it, ja, ko, ml, my, nb_NO, nl, nn_NO, pl, pt_BR, ro, ru,
* sq, sr, sv, ta, th, tr, uk, vi, zh_CN, zh_TW
Expand All @@ -42,43 +43,3 @@ register('cs', cs);
register('da', da);
register('ka', ka);
register('oc', oc);

/**
* timeago.full.min.js registers some locales in a different format than what
* is currently supported by amp-timeago. For backwards compatibility, the
* following additional locale formats are converted to their registered type.
* @param {string} locale
* @return {string}
*/
export function getLocale(locale) {
switch (locale) {
case 'en':
return 'en_US';
case 'enShort':
return 'en_short';
case 'inBG':
return 'bn_IN';
case 'inID':
return 'id_ID';
case 'inHI':
return 'hi_IN';
case 'nbNO':
return 'nb_NO';
case 'nnNO':
return 'nn_NO';
case 'ptBR':
return 'pt_BR';
case 'zhCN':
return 'zh_CN';
case 'zhTW':
return 'zh_TW';
default:
// Note: This line both supports en_Short -> en_short, and consequently
// invalidates default formatting provided by timeago.js. i.e. "zh_CN"
// locale value will not work as it becomes "zh_cn", and the
// registration is case sensitive. If we want to expand the set of
// supported formats to those that are already supported by the
// timeago.js library, we should remove `toLocaleLowerCase`.
return locale.toLocaleLowerCase();
}
}

0 comments on commit 5032f5a

Please sign in to comment.