Skip to content

Commit

Permalink
Merge 6567662 into 7d35d6d
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeyer2115 committed Aug 18, 2021
2 parents 7d35d6d + 6567662 commit 8bb76d1
Show file tree
Hide file tree
Showing 18 changed files with 796 additions and 75 deletions.
28 changes: 22 additions & 6 deletions conf/gulp-tasks/bundle/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const {
const TranslateCallParser = require('../../i18n/translatecallparser');

/**
* The Gulp task for producing the modern version of the SDK bundle.
* The Gulp task for producing the modern bundle of an SDK asset.
*
* @param {Function} callback
* @param {string} entryPoint The entry point for the asset.
* @param {Object<string, ?>} outputConfig Any variant-specific configuration
* for the modern bundle.
* @param {string} bundleName The name of the created bundle.
Expand All @@ -28,9 +29,16 @@ const TranslateCallParser = require('../../i18n/translatecallparser');
* @returns {stream.Writable} A {@link Writable} stream containing the modern
* SDK bundle.
*/
exports.modernBundle = function (callback, outputConfig, bundleName, locale, libVersion, translationResolver) {
exports.modernBundle = function (
callback,
entryPoint,
outputConfig,
bundleName,
locale,
libVersion,
translationResolver) {
const rollupConfig = {
input: './src/answers-umd.js',
input: entryPoint,
output: outputConfig,
plugins: [
resolve({ browser: true }),
Expand All @@ -49,9 +57,10 @@ exports.modernBundle = function (callback, outputConfig, bundleName, locale, lib
};

/**
* The Gulp task for producing either variant of the legacy SDK bundle.
* The Gulp task for producing any variant of a legacy SDK asset.
*
* @param {Function} callback
* @param {string} entryPoint The entry point for the asset.
* @param {Object<string, ?>} outputConfig Any variant-specific configuration
* for the legacy bundle.
* @param {string} bundleName The name of the created bundle.
Expand All @@ -61,9 +70,16 @@ exports.modernBundle = function (callback, outputConfig, bundleName, locale, lib
* @returns {stream.Writable} A {@link Writable} stream containing the legacy
* SDK bundle.
*/
exports.legacyBundle = function (callback, outputConfig, bundleName, locale, libVersion, translationResolver) {
exports.legacyBundle = function (
callback,
entryPoint,
outputConfig,
bundleName,
locale,
libVersion,
translationResolver) {
const rollupConfig = {
input: './src/answers-umd.js',
input: entryPoint,
output: outputConfig,
plugins: [
resolve({ browser: true }),
Expand Down
20 changes: 13 additions & 7 deletions conf/gulp-tasks/bundle/bundletaskfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ class BundleTaskFactory {
* @param {BundleType} bundleType The type of SDK bundle to build.
* @returns {Function} Gulp task for producing the requested SDK bundle.
*/
create (bundleType) {
create (bundleType, entryPoint) {
let bundleFunction;
switch (bundleType) {
case BundleType.MODERN:
bundleFunction = (callback) => this._modernBundle(callback);
bundleFunction = (callback) => this._modernBundle(callback, entryPoint);
break;
case BundleType.LEGACY_IIFE:
bundleFunction = (callback) => this._legacyBundleIIFE(callback);
bundleFunction = (callback) => this._legacyBundleIIFE(callback, entryPoint);
break;
case BundleType.LEGACY_UMD:
bundleFunction = (callback) => this._legacyBundleUMD(callback);
bundleFunction = (callback) => this._legacyBundleUMD(callback, entryPoint);
break;
default:
throw new Error('Unrecognized BundleType');
Expand All @@ -51,10 +51,11 @@ class BundleTaskFactory {
* The Gulp task for producing the modern version of the SDK bundle.
*
* @param {function} callback function that will run after the Gulp task
* @param {string} entryPoint
* @returns {stream.Writable} A {@link Writable} stream containing the modern
* SDK bundle.
*/
_modernBundle (callback) {
_modernBundle (callback, entryPoint) {
const modernBundleConfig = {
format: 'umd',
name: this._namespace,
Expand All @@ -63,6 +64,7 @@ class BundleTaskFactory {
};
return modernBundle(
callback,
entryPoint,
modernBundleConfig,
getBundleName(BundleType.MODERN, this._locale),
this._locale,
Expand All @@ -75,17 +77,19 @@ class BundleTaskFactory {
* The Gulp task for producing the legacy, IIFE-style SDK bundle.
*
* @param {function} callback function that will run after the Gulp task
* @param {string} entryPoint
* @returns {stream.Writable} A {@link Writable} stream containing the legacy,
* IIFE-style SDK bundle.
*/
_legacyBundleIIFE (callback) {
_legacyBundleIIFE (callback, entryPoint) {
const legacyBundleConfig = {
format: 'iife',
name: this._namespace,
sourcemap: true
};
return legacyBundle(
callback,
entryPoint,
legacyBundleConfig,
getBundleName(BundleType.LEGACY_IIFE, this._locale),
this._locale,
Expand All @@ -98,10 +102,11 @@ class BundleTaskFactory {
* The Gulp task for producing the legacy, UMD-style SDK bundle.
*
* @param {function} callback function that will run after the Gulp task
* @param {string} entryPoint
* @returns {stream.Writable} A {@link Writable} stream containing the legacy,
* UMD-style SDK bundle.
*/
_legacyBundleUMD (callback) {
_legacyBundleUMD (callback, entryPoint) {
const legacyBundleConfig = {
format: 'umd',
name: this._namespace,
Expand All @@ -110,6 +115,7 @@ class BundleTaskFactory {
};
return legacyBundle(
callback,
entryPoint,
legacyBundleConfig,
getBundleName(BundleType.LEGACY_UMD, this._locale),
this._locale,
Expand Down
51 changes: 33 additions & 18 deletions conf/gulp-tasks/library.gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.dev = function devJSBundle () {
return createBundleTaskFactory(DEFAULT_LOCALE).then(devTaskFactory => {
return new Promise(resolve => {
return parallel(
series(devTaskFactory.create(BundleType.LEGACY_IIFE), getWatchJSTask(devTaskFactory)),
series(devTaskFactory.create(BundleType.LEGACY_IIFE, './src/answers-umd.js'), getWatchJSTask(devTaskFactory)),
series(compileCSS, watchCSS)
)(resolve);
});
Expand All @@ -36,28 +36,28 @@ exports.dev = function devJSBundle () {
* This function also supports locales, but it is named to reflect the current use case
* of creating bundles for just languages.
* @param {Array<string>} languages
* @param {boolean} isSearchBarOnly
* @returns {Promise<Function>}
*/
function createJSBundlesForLanguages (languages) {
function createJSBundlesForLanguages (languages, isSearchBarOnly = false) {
const localizedTaskPromises = languages.map(language => {
const minifyTaskFactory = new MinifyTaskFactory(language);
return createBundleTaskFactory(language)
.then(bundleTaskFactory => createBundles(bundleTaskFactory, minifyTaskFactory));
.then(bundleTaskFactory => createBundles(bundleTaskFactory, minifyTaskFactory, isSearchBarOnly));
});
return Promise.all(localizedTaskPromises).then(localizedTasks => {
return new Promise(resolve => series(compileCSS, ...localizedTasks)(resolve));
});
}

exports.default = function defaultLanguageJSBundles () {
return createJSBundlesForLanguages([DEFAULT_LOCALE]);
};

exports.buildLanguages = function allLanguageJSBundles () {
return createJSBundlesForLanguages(ALL_LANGUAGES);
};

exports.buildLocales = function allLocaleJSBundles () {
/**
* Creates a build task for each provided language and combines them into a series task.
* This function also supports locales, but it is named to reflect the current use case
* of creating bundles for just languages.
* @param {boolean} isSearchBarOnly
* @returns {Promise<Function>}
*/
function allLocaleJSBundles (isSearchBarOnly = false) {
const assetNames = [
'answers.js',
'answers.min.js',
Expand All @@ -66,9 +66,23 @@ exports.buildLocales = function allLocaleJSBundles () {
'answers-umd.js',
'answers-umd.min.js'];

return createJSBundlesForLanguages(ALL_LANGUAGES).then(() => {
return createJSBundlesForLanguages(ALL_LANGUAGES, isSearchBarOnly).then(() => {
copyAssetsForLocales(assetNames);
});
}

exports.default = function defaultLanguageJSBundles () {
return createJSBundlesForLanguages([DEFAULT_LOCALE]);
};

exports.buildLanguages = function allLanguageJSBundles () {
return createJSBundlesForLanguages(ALL_LANGUAGES);
};

exports.buildLocales = allLocaleJSBundles;

exports.buildSearchBarOnlyAssets = function () {
return allLocaleJSBundles(true);
};

/**
Expand All @@ -77,18 +91,19 @@ exports.buildLocales = function allLocaleJSBundles () {
* @param {BundleTaskFactory} bundleTaskFactory
* @param {MinifyTaskFactory} minifyTaskFactory
*/
function createBundles (bundleTaskFactory, minifyTaskFactory) {
function createBundles (bundleTaskFactory, minifyTaskFactory, isSearchBarOnly) {
const entryPoint = isSearchBarOnly ? './src/answers-search-bar.js' : './src/answers-umd.js';
return parallel(
series(
bundleTaskFactory.create(BundleType.MODERN),
bundleTaskFactory.create(BundleType.MODERN, entryPoint),
minifyTaskFactory.minify(BundleType.MODERN)
),
series(
bundleTaskFactory.create(BundleType.LEGACY_IIFE),
bundleTaskFactory.create(BundleType.LEGACY_IIFE, entryPoint),
minifyTaskFactory.minify(BundleType.LEGACY_IIFE)
),
series(
bundleTaskFactory.create(BundleType.LEGACY_UMD),
bundleTaskFactory.create(BundleType.LEGACY_UMD, entryPoint),
minifyTaskFactory.minify(BundleType.LEGACY_UMD)
)
);
Expand Down Expand Up @@ -155,7 +170,7 @@ function getWatchJSTask (bundleTaskFactory) {
return function watchJS () {
return watch(['./src/**/*.js'], {
ignored: './dist/'
}, bundleTaskFactory.create(BundleType.LEGACY_IIFE));
}, bundleTaskFactory.create(BundleType.LEGACY_IIFE, './src/answers-umd.js'));
};
}

Expand Down
45 changes: 37 additions & 8 deletions conf/gulp-tasks/template/createprecompiletemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const handlebars = require('gulp-handlebars');
const concat = require('gulp-concat');
const declare = require('gulp-declare');
const wrap = require('gulp-wrap');
const filter = require('gulp-filter');

const TranslateHelperVisitor = require('../../i18n/translatehelpervisitor');
const { addLocalePrefix, getPrecompiledFileName } = require('./filenameutils');
Expand All @@ -12,10 +13,13 @@ const { addLocalePrefix, getPrecompiledFileName } = require('./filenameutils');
*
* @param {string} locale
* @param {Translator} translator
* @param {boolean} isSearchBarOnly If only templates related to the SearchBar
* should be included.
* @returns {Function}
*/
function createPrecompileTemplatesTask (locale, translator) {
const precompileTask = callback => _precompileTemplates(callback, locale, translator);
function createPrecompileTemplatesTask (locale, translator, isSearchBarOnly = false) {
const precompileTask = callback =>
_precompileTemplates(callback, locale, isSearchBarOnly, translator);
const taskName = addLocalePrefix('precompileTemplates', locale);
Object.defineProperty(precompileTask, 'name', {
value: taskName
Expand All @@ -28,25 +32,49 @@ function createPrecompileTemplatesTask (locale, translator) {
*
* @param {Function} callback called when the task is finished
* @param {string} locale
* @param {boolean} isSearchBarOnly If only the templates related to the SearchBar
* should be included.
* @param {Translator} translator
* @returns {stream.Readable}
*/
function _precompileTemplates (callback, locale, translator) {
function _precompileTemplates (callback, locale, isSearchBarOnly, translator) {
const precompiledFileName = getPrecompiledFileName(locale);
const processAST = ast => new TranslateHelperVisitor(translator).accept(ast);
return precompileTemplates(callback, precompiledFileName, processAST);

return precompileTemplates(
callback, isSearchBarOnly, precompiledFileName, processAST);
}

/**
* Precopmiles SDK templates to the given output file.
* Precopmiles the requested templates to the given output file.
*
* @param {Function} callback called when the task is finished
* @param {boolean} isSearchBarOnly A boolean indicating if only those templates for
* the search-bar integration should be included.
* @param {string} outputFile
* @param {Function} processAST a function that takes in and mutates a handlebars AST
* @returns {stream.Readable}
*/
function precompileTemplates (callback, outputFile, processAST) {
return src('./src/ui/templates/**/*.hbs')
function precompileTemplates (callback, isSearchBarOnly, outputFile, processAST) {
let templatesStream = src('./src/ui/templates/**/*.hbs');
let handlebarsWrapperData;

if (isSearchBarOnly) {
templatesStream =
templatesStream.pipe(filter(['**/search/**/*.hbs', '**/icons/**/*.hbs']));
handlebarsWrapperData = {
importStatements: 'import Handlebars from \'handlebars/dist/handlebars.runtime.min.js\';'
};
} else {
handlebarsWrapperData = {
importStatements: 'import Handlebars from \'handlebars/dist/handlebars.min.js\';\n' +
'import templateHelpers from \'template-helpers\';',
handlebarsHelpers: 'let handlebarsHelpers = templateHelpers();\n' +
'parseHelper(handlebarsHelpers);'
};
}

return templatesStream
.pipe(handlebars({ processAST: processAST }))
.pipe(wrap(`Handlebars.template(<%= contents %>);
Handlebars.registerPartial(<%= processPartialName(file.relative) %>, <%= customContext(file.relative) %> )`, {}, {
Expand Down Expand Up @@ -83,7 +111,8 @@ function precompileTemplates (callback, outputFile, processAST) {
}
}))
.pipe(concat(outputFile))
.pipe(wrap({ src: './conf/templates/handlebarswrapper.txt' }))
.pipe(wrap(
{ src: './conf/templates/handlebarswrapper.txt' }, handlebarsWrapperData, { variable: 'data' }))
.pipe(dest('dist'))
.on('end', callback);
}
Expand Down
30 changes: 18 additions & 12 deletions conf/gulp-tasks/templates.gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ exports.dev = devTemplates;
* @param {Translator} translator
* @returns {Function}
*/
function createDefaultTask (locale, translator) {
const precompileTemplates = createPrecompileTemplatesTask(locale, translator);
function createDefaultTask (locale, translator, isSearchBarOnly) {
const precompileTemplates = createPrecompileTemplatesTask(locale, translator, isSearchBarOnly);

const bundleFactory = new BundleTemplatesTaskFactory(locale);
const bundleTemplatesIIFE = bundleFactory.create(TemplateType.IIFE);
Expand Down Expand Up @@ -87,15 +87,25 @@ function createDefaultTask (locale, translator) {
* @param {Array<string>} languages
* @returns {Promise<Function>}
*/
async function createTemplatesForLanguages (languages) {
async function createTemplatesForLanguages (languages, isSearchBarOnly = false) {
const localizedTaskPromises = languages.map(async language => {
const translator = await createTranslator(language);
return createDefaultTask(language, translator);
return createDefaultTask(language, translator, isSearchBarOnly);
});
const localizedTasks = await Promise.all(localizedTaskPromises);
return new Promise(resolve => series(...localizedTasks)(resolve));
}

function allLocaleTemplates (isSearchBarOnly = false) {
const assetNames = [
'answerstemplates-iife.compiled.min.js',
'answerstemplates.compiled.min.js'];

return createTemplatesForLanguages(ALL_LANGUAGES, isSearchBarOnly).then(() => {
copyAssetsForLocales(assetNames);
});
}

exports.default = function defaultTemplates () {
return createTemplatesForLanguages([DEFAULT_LOCALE]);
};
Expand All @@ -104,12 +114,8 @@ exports.buildLanguages = function allLanguageTemplates () {
return createTemplatesForLanguages(ALL_LANGUAGES);
};

exports.buildLocales = function allLocaleTemplates () {
const assetNames = [
'answerstemplates-iife.compiled.min.js',
'answerstemplates.compiled.min.js'];

return createTemplatesForLanguages(ALL_LANGUAGES).then(() => {
copyAssetsForLocales(assetNames);
});
exports.buildSearchBarOnlyAssets = function () {
return allLocaleTemplates(true);
};

exports.buildLocales = allLocaleTemplates;

0 comments on commit 8bb76d1

Please sign in to comment.