-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lwc-template-compiler): Add new compiler API compileToFunction (#…
…528) ## Details This PR updates the `lwc-template-compiler` package to add `compileToFunction` API. This method takes as parameter a string template and a configuration and returns the instantiated template function. The primary use-case of this new API is to allow inline compilation of template for unit tests. This PR also introduce the `compileTemplate` test utility to the `lwc-engine` package unit tests. Once this utility is stable, we will move it to the official `lwc-tests-utils` package for the general consumption. ## Does this PR introduce a breaking change? * [ ] Yes * [X] No
- Loading branch information
Showing
14 changed files
with
423 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
const path = require('path'); | ||
const BASE_CONFIG = require('../../scripts/jest/base.config'); | ||
|
||
module.exports = { | ||
...BASE_CONFIG, | ||
|
||
displayName: 'lwc-engine', | ||
moduleNameMapper: { | ||
'test-utils': path.resolve(__dirname, 'scripts/jest/test-utils.js'), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { compileToFunction } = require('lwc-template-compiler'); | ||
|
||
const TEMPLATE_CACHE = Object.create(null); | ||
|
||
/** | ||
* Compiles a template string and returns the instantiated function. | ||
* | ||
* @param {string} source The template string | ||
* @param {object=} config The template configuration | ||
* @param {object=} config.modules The map of the modules used in the template | ||
* @returns {function} | ||
*/ | ||
function compileTemplate(source, config = {}) { | ||
const { modules = {} } = config; | ||
|
||
// Check if the same template has already been compiled | ||
if (!(source in TEMPLATE_CACHE)) { | ||
TEMPLATE_CACHE[source] = compileToFunction(source); | ||
} | ||
|
||
const templateFactory = TEMPLATE_CACHE[source]; | ||
return templateFactory(modules); | ||
} | ||
|
||
module.exports = { | ||
compileTemplate, | ||
}; |
16 changes: 13 additions & 3 deletions
16
packages/lwc-engine/src/framework/__tests__/class-list.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.