Skip to content

Commit

Permalink
add option to disable legacy templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo-devis-agullo committed Sep 8, 2024
1 parent df43b24 commit 4d403a3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface CompileOptions {
retryInterval?: number;
retryLimit?: number;
disableLoader?: boolean;
disableLegacyTemplates?: boolean;
externals?: External[];
}
type Compiled = { code: string; map: string; dev: string };
Expand Down
11 changes: 7 additions & 4 deletions src/oc-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals define, exports, require, globalThis, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __DEFAULT_DISABLE_LOADER__, __EXTERNALS__ */
/* globals define, exports, require, globalThis, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __DEFAULT_DISABLE_LOADER__, __DISABLE_LEGACY_TEMPLATES__, __EXTERNALS__ */
/* eslint no-var: 'off' */
/* eslint prefer-arrow-callback: 'off' */

Expand Down Expand Up @@ -405,8 +405,10 @@ var oc = oc || {};
}

var type = compiledViewInfo.type;
if (type == 'jade' || type == 'handlebars') {
type = 'oc-template-' + type;
if (!__DISABLE_LEGACY_TEMPLATES__) {
if (type == 'jade' || type == 'handlebars') {
type = 'oc-template-' + type;
}
}
var template = registeredTemplates[type];

Expand All @@ -427,7 +429,8 @@ var oc = oc || {};
try {
callback(
null,
type == 'oc-template-handlebars'
!__DISABLE_LEGACY_TEMPLATES__ &&
type == 'oc-template-handlebars'
? $window.Handlebars.template(compiledView, [])(model)
: compiledView(model)
);
Expand Down
48 changes: 26 additions & 22 deletions tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,52 +53,46 @@ function parseConf(conf) {
global: 'jQuery',
url: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js'
};
const disableLegacyTemplates = Boolean(conf.disableLegacyTemplates ?? true);
const transformedTemplates = transformTemplates(conf.templates);
const templates = disableLegacyTemplates
? {
'oc-template-es6': baseTemplates['oc-template-es6'],
...transformedTemplates
}
: { ...baseTemplates, ...transformedTemplates };

return {
externals: [jQueryExternal].concat(conf.externals || []),
retryLimit: conf.retryLimit || 30,
retryInterval: conf.retryInterval || 5000,
disableLegacyTemplates: disableLegacyTemplates,
disableLoader: Boolean(conf.disableLoader ?? false),
templates: {
...baseTemplates,
...transformTemplates(conf.templates)
}
templates
};
}

function getFiles({ sync = false, conf }) {
function getFiles({ sync = false }) {
const srcPath = '../src/';
const vendorPath = '../vendor/';

const lPath = path.join(__dirname, vendorPath, 'l.js');
const ocClientPath = path.join(__dirname, srcPath, 'oc-client.js');
const replaceTemplates = x =>
x
.replaceAll(
'__REGISTERED_TEMPLATES_PLACEHOLDER__',
JSON.stringify(conf.templates)
)
.replaceAll('__EXTERNALS__', JSON.stringify(conf.externals))
.replaceAll('__DEFAULT_RETRY_LIMIT__', conf.retryLimit)
.replaceAll('__DEFAULT_RETRY_INTERVAL__', conf.retryInterval)
.replaceAll('__DEFAULT_DISABLE_LOADER__', conf.disableLoader);

if (sync) {
const l = fs.readFileSync(lPath, 'utf-8');
const ocClient = replaceTemplates(fs.readFileSync(ocClientPath, 'utf-8'));
const ocClient = fs.readFileSync(ocClientPath, 'utf-8');

return [l, ocClient];
} else {
const lPromise = readFile(lPath, 'utf-8');
const ocClientPromise = readFile(ocClientPath, 'utf-8').then(
replaceTemplates
);
const ocClientPromise = readFile(ocClientPath, 'utf-8');

return Promise.all([lPromise, ocClientPromise]);
}
}

function compileFiles(l, ocClient) {
function compileFiles(l, ocClient, conf) {
const version = packageJson.version;
const licenseLink =
'https://github.com/opencomponents/oc-client-browser/tree/master/LICENSES';
Expand All @@ -109,6 +103,16 @@ function compileFiles(l, ocClient) {
sourceMap: {
filename: 'oc-client.min.js',
url: 'oc-client.min.map'
},
compress: {
global_defs: {
__DISABLE_LEGACY_TEMPLATES__: conf.disableLegacyTemplates,
__DEFAULT_DISABLE_LOADER__: conf.disableLoader,
__DEFAULT_RETRY_INTERVAL__: conf.retryInterval,
__DEFAULT_RETRY_LIMIT__: conf.retryLimit,
__EXTERNALS__: conf.externals,
__REGISTERED_TEMPLATES_PLACEHOLDER__: conf.templates
}
}
});

Expand All @@ -119,13 +123,13 @@ function compileFiles(l, ocClient) {

async function compile(conf = {}) {
const parsedConf = parseConf(conf);
const [l, ocClient] = await getFiles({ sync: false, conf: parsedConf });
const [l, ocClient] = await getFiles({ sync: false });
return compileFiles(l, ocClient, parsedConf);
}

function compileSync(conf = {}) {
const parsedConf = parseConf(conf);
const [l, ocClient] = getFiles({ sync: true, conf: parsedConf });
const [l, ocClient] = getFiles({ sync: true });
return compileFiles(l, ocClient, parsedConf);
}

Expand Down

0 comments on commit 4d403a3

Please sign in to comment.