-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template: don't call lodashTemplate
if define
param is undefined
#46
Template: don't call lodashTemplate
if define
param is undefined
#46
Conversation
Thanks for this contribution. :) I think skipping lodash |
return compiledTemplateFn({ define }) | ||
|
||
if (define === undefined) { | ||
return template |
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.
Maybe I'm wrong here, but I feel like this should be:
return template | |
return template || defaultHtmlTemplate |
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.
You are right, good catch, thanks for checking!
Fixed slightly differently:
5fffc45
… const to use in condition
Good find! Yeah, it looks like a nice possible option to restrict a syntax for replacement. |
Sorry this took a while:) |
…ates This patch disables using ${} in the html templates. This is mostly to avoid conflicts with the provided template that might use ${} in inline javascript natively. This patch also reverts the changes of #46. lodash.template is now always called. this is useful for when you want to use `process.env`. Therefore, this change is breaking, as it removes ${}, but also always calls lodash again. If this ever will be a problem, we can also add a configuration option to enable ${} manually again.
…ates This patch disables using ${} in the html templates. This is mostly to avoid conflicts with the provided template that might use ${} in inline javascript natively. This patch also reverts the changes of #46. lodash.template is now always called. this is useful for when you want to use `process.env`. Therefore, this change is breaking, as it removes ${}, but also always calls lodash again. If this ever will be a problem, we can also add a configuration option to enable ${} manually again.
Description
I attempt to use
esbuild-plugin-html
with custom HTML templates for different locales. HTML templates contain a script tag where the object with translation messages for react-intl is defined. Example:The problem is that translation message contain
$
character with a placeholder for a runtime value inside of curly braces. Example:"${amount} / 年"
(it will be$99 / 年
at runtime).${amount}
is a legit target of lodash template function, but I got esbuild error becauseamount
value is missing indefine
param object. I don't usedefine
param inesbuild-plugin-html
and I don't need to replace anything in my HTML templates.Currently, it produces a build error:
Solution
I propose to simply don't call
lodashTemplate
function ifdefine
param is undefined.Another possible option is to introduce an extra param like
useLodashTemplate: bool
or something, but I think my suggestion is simpler and just works.Overall, I think it's a good idea to avoid calling
lodashTemplate
function at all when it isn't needed as a performance optimization.