Closed AtofStryker closed 4 years ago
@difelice thanks for taking a look at this! I figured we would likely look at the https://github.com/difelice/ejs-loader/pull/41 first because there is an important commit we need in there that will break the options if none passed that I didn't update this branch to have yet 😅. I will update https://github.com/difelice/ejs-loader/pull/41 to rebase out the first commit and just include https://github.com/difelice/ejs-loader/pull/41/commits/83e79a6a1f49d361115f3db7939f1637d03f2e4a
The purpose of this PR is to add an option to export the ejs template as an ES6 Module in order to leverage Code Splitting and Tree Shaking. This is pretty important to a lot of people who are still using legacy ejs templates, but want to optimize their bundles.
There were a few things I had to change to get this to working:
1) Why is
variable
required: Whenlodash
orunderscore
templates are instantiated without avariable
option, awith
statement is used. This violatesstrict
mode and fails compilation.2) Why not use a default? What makes using a default difficult in this case is the template needs to know the variables being passed into it. In order to likely do something like this, we would have to change interpolation settings. This seems to be a lot of effort to go through to support a default case. We could set a default and not handle the context, but my fear would be that the template compiles find at build, but fails at runtime, which could be risky.
2) Not being able to leverage
requireFromString
. SincerequireFromString
only supports formodule.exports
, I had to get creative with parsingexport default
for ES Modules. Since I could not find a solution that already handles this, I wrote a RegExp that removes the string and creates a new function/Please see #41 for commit 814bcc5. And please let me know if there is anything you all would like to see improved or changed :smile: