Skip to content

Commit

Permalink
Allow for transformation of source and compiled code, handle use dire…
Browse files Browse the repository at this point in the history
…ctive
  • Loading branch information
backflip committed Sep 25, 2019
1 parent 1ac9d0f commit 186b63d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 18 deletions.
57 changes: 48 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,14 @@ See [./test](./test).
rules: [
{
test: /\.htl$/,
use: {
loader: "htl-loader",
options: {
globalName: "htl"
}
}
use: ["htl-loader"]
}
]
},
node: {
fs: "empty"
}
}
```


2. Create examplary `template.htl`:

```html
Expand All @@ -53,6 +46,52 @@ import template from "./template.htl";
})();
```

## Advanced

```js
{
module: {
rules: [
{
test: /\.htl$/,
use: [
{
loader: "htl-loader",
options: {
// Remove directives `@adobe/htlengine` does not understand
transformSource: source => {
const output = source
.replace(/data-sly-use\.templates?="(.*?)"/g, "")
.replace(/<sly[^>]+data-sly-call=(["']).*?\1.*?><\/sly>/g, "");

return output;
},
// Allow for custom models in data from `use` directives
transformCompiled: (compiled, settings) => {
const output = compiled.replace(
/(new Runtime\(\);)/,
`$1
const originalUse = runtime.use.bind(runtime);
runtime.use = function(uri, options) {
const settings = Object.assign({
model: '${settings.model}'
}, options);
return originalUse(uri, settings);
}`
);

return output;
},
useDir: path.resolve(__dirname, "../src/htlmocks")
}
}
]
}
]
}
}
```

## License

[MIT](http://www.opensource.org/licenses/mit-license)
54 changes: 45 additions & 9 deletions index.js
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}\``;
};

0 comments on commit 186b63d

Please sign in to comment.