Skip to content

Commit 4d403a3

Browse files
add option to disable legacy templates
1 parent df43b24 commit 4d403a3

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface CompileOptions {
2525
retryInterval?: number;
2626
retryLimit?: number;
2727
disableLoader?: boolean;
28+
disableLegacyTemplates?: boolean;
2829
externals?: External[];
2930
}
3031
type Compiled = { code: string; map: string; dev: string };

src/oc-client.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* globals define, exports, require, globalThis, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __DEFAULT_DISABLE_LOADER__, __EXTERNALS__ */
1+
/* globals define, exports, require, globalThis, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __DEFAULT_DISABLE_LOADER__, __DISABLE_LEGACY_TEMPLATES__, __EXTERNALS__ */
22
/* eslint no-var: 'off' */
33
/* eslint prefer-arrow-callback: 'off' */
44

@@ -405,8 +405,10 @@ var oc = oc || {};
405405
}
406406

407407
var type = compiledViewInfo.type;
408-
if (type == 'jade' || type == 'handlebars') {
409-
type = 'oc-template-' + type;
408+
if (!__DISABLE_LEGACY_TEMPLATES__) {
409+
if (type == 'jade' || type == 'handlebars') {
410+
type = 'oc-template-' + type;
411+
}
410412
}
411413
var template = registeredTemplates[type];
412414

@@ -427,7 +429,8 @@ var oc = oc || {};
427429
try {
428430
callback(
429431
null,
430-
type == 'oc-template-handlebars'
432+
!__DISABLE_LEGACY_TEMPLATES__ &&
433+
type == 'oc-template-handlebars'
431434
? $window.Handlebars.template(compiledView, [])(model)
432435
: compiledView(model)
433436
);

tasks/compile.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,52 +53,46 @@ function parseConf(conf) {
5353
global: 'jQuery',
5454
url: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js'
5555
};
56+
const disableLegacyTemplates = Boolean(conf.disableLegacyTemplates ?? true);
57+
const transformedTemplates = transformTemplates(conf.templates);
58+
const templates = disableLegacyTemplates
59+
? {
60+
'oc-template-es6': baseTemplates['oc-template-es6'],
61+
...transformedTemplates
62+
}
63+
: { ...baseTemplates, ...transformedTemplates };
5664

5765
return {
5866
externals: [jQueryExternal].concat(conf.externals || []),
5967
retryLimit: conf.retryLimit || 30,
6068
retryInterval: conf.retryInterval || 5000,
69+
disableLegacyTemplates: disableLegacyTemplates,
6170
disableLoader: Boolean(conf.disableLoader ?? false),
62-
templates: {
63-
...baseTemplates,
64-
...transformTemplates(conf.templates)
65-
}
71+
templates
6672
};
6773
}
6874

69-
function getFiles({ sync = false, conf }) {
75+
function getFiles({ sync = false }) {
7076
const srcPath = '../src/';
7177
const vendorPath = '../vendor/';
7278

7379
const lPath = path.join(__dirname, vendorPath, 'l.js');
7480
const ocClientPath = path.join(__dirname, srcPath, 'oc-client.js');
75-
const replaceTemplates = x =>
76-
x
77-
.replaceAll(
78-
'__REGISTERED_TEMPLATES_PLACEHOLDER__',
79-
JSON.stringify(conf.templates)
80-
)
81-
.replaceAll('__EXTERNALS__', JSON.stringify(conf.externals))
82-
.replaceAll('__DEFAULT_RETRY_LIMIT__', conf.retryLimit)
83-
.replaceAll('__DEFAULT_RETRY_INTERVAL__', conf.retryInterval)
84-
.replaceAll('__DEFAULT_DISABLE_LOADER__', conf.disableLoader);
8581

8682
if (sync) {
8783
const l = fs.readFileSync(lPath, 'utf-8');
88-
const ocClient = replaceTemplates(fs.readFileSync(ocClientPath, 'utf-8'));
84+
const ocClient = fs.readFileSync(ocClientPath, 'utf-8');
8985

9086
return [l, ocClient];
9187
} else {
9288
const lPromise = readFile(lPath, 'utf-8');
93-
const ocClientPromise = readFile(ocClientPath, 'utf-8').then(
94-
replaceTemplates
95-
);
89+
const ocClientPromise = readFile(ocClientPath, 'utf-8');
9690

9791
return Promise.all([lPromise, ocClientPromise]);
9892
}
9993
}
10094

101-
function compileFiles(l, ocClient) {
95+
function compileFiles(l, ocClient, conf) {
10296
const version = packageJson.version;
10397
const licenseLink =
10498
'https://github.com/opencomponents/oc-client-browser/tree/master/LICENSES';
@@ -109,6 +103,16 @@ function compileFiles(l, ocClient) {
109103
sourceMap: {
110104
filename: 'oc-client.min.js',
111105
url: 'oc-client.min.map'
106+
},
107+
compress: {
108+
global_defs: {
109+
__DISABLE_LEGACY_TEMPLATES__: conf.disableLegacyTemplates,
110+
__DEFAULT_DISABLE_LOADER__: conf.disableLoader,
111+
__DEFAULT_RETRY_INTERVAL__: conf.retryInterval,
112+
__DEFAULT_RETRY_LIMIT__: conf.retryLimit,
113+
__EXTERNALS__: conf.externals,
114+
__REGISTERED_TEMPLATES_PLACEHOLDER__: conf.templates
115+
}
112116
}
113117
});
114118

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

120124
async function compile(conf = {}) {
121125
const parsedConf = parseConf(conf);
122-
const [l, ocClient] = await getFiles({ sync: false, conf: parsedConf });
126+
const [l, ocClient] = await getFiles({ sync: false });
123127
return compileFiles(l, ocClient, parsedConf);
124128
}
125129

126130
function compileSync(conf = {}) {
127131
const parsedConf = parseConf(conf);
128-
const [l, ocClient] = getFiles({ sync: true, conf: parsedConf });
132+
const [l, ocClient] = getFiles({ sync: true });
129133
return compileFiles(l, ocClient, parsedConf);
130134
}
131135

0 commit comments

Comments
 (0)