-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for transformation of source and compiled code, handle use dire…
…ctive
- Loading branch information
Showing
2 changed files
with
93 additions
and
18 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
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,24 +1,60 @@ | ||
const { getOptions } = require("loader-utils"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { getOptions, parseQuery } = require("loader-utils"); | ||
const { Compiler } = require("@adobe/htlengine"); | ||
|
||
module.exports = async function(source) { | ||
const options = getOptions(this); | ||
const query = this.resourceQuery ? parseQuery(this.resourceQuery) : null; | ||
const settings = Object.assign( | ||
{ | ||
globalName: "htl" | ||
globalName: "htl", | ||
model: "default", | ||
useDir: null, | ||
transformSource: null, | ||
transformCompiled: null | ||
}, | ||
options | ||
options, | ||
query | ||
); | ||
|
||
let input = source; | ||
|
||
// Optionally transform source, e.g. remove directives `@adobe/htlengine` does not understand | ||
if (settings.transformSource) { | ||
input = settings.transformSource(source, settings); | ||
} | ||
|
||
// Set up compiler | ||
const compiler = new Compiler() | ||
.includeRuntime(true) | ||
.withRuntimeGlobalName(settings.globalName); | ||
|
||
const compiledCode = await compiler.compileToString(source); | ||
// Compile | ||
let compiledCode = await compiler.compileToString(input); | ||
|
||
// Specify location for data files from `use` directives | ||
if (settings.useDir) { | ||
// Remove files from cache | ||
fs.readdirSync(settings.useDir).forEach(file => { | ||
const filePath = path.join(settings.useDir, file); | ||
delete require.cache[filePath]; | ||
}); | ||
|
||
compiledCode = compiledCode.replace( | ||
/(runtime\.setGlobal\(resource\);)/, | ||
`$1\nruntime.withUseDirectory('${settings.useDir}');` | ||
); | ||
} | ||
|
||
// Optionally transform compiled, e.g. to customize runtime | ||
if (settings.transformCompiled) { | ||
compiledCode = settings.transformCompiled(compiledCode, settings); | ||
} | ||
|
||
// Run | ||
const template = eval(compiledCode); | ||
const html = await template({}); | ||
|
||
return ` | ||
${compiledCode} | ||
const template = module.exports.main; | ||
module.exports = async (data) => await template(data); | ||
`; | ||
return `module.exports = \`${html}\``; | ||
}; |