1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const fs = require('fs-extra');
|
14 | const path = require('path');
|
15 | const Compiler = require('@adobe/htlengine/src/compiler/Compiler');
|
16 | const Asset = require('parcel-bundler/src/Asset');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class HTLAsset extends Asset {
|
22 | constructor(name, options) {
|
23 | super(name, options);
|
24 | this.type = 'js';
|
25 | }
|
26 |
|
27 |
|
28 | async parse(code) {
|
29 | const template = await fs.readFile(path.resolve(__dirname, 'engine', 'RuntimeTemplate.js'), 'utf-8');
|
30 | const compiler = new Compiler()
|
31 | .withOutputDirectory('')
|
32 | .includeRuntime(true)
|
33 | .withRuntimeVar('content')
|
34 | .withRuntimeVar('request')
|
35 | .withRuntimeGlobalName('payload')
|
36 | .withCodeTemplate(template)
|
37 | .withSourceMap(true);
|
38 |
|
39 | return compiler.compile(code);
|
40 | }
|
41 |
|
42 | generate() {
|
43 | const { template, sourceMap } = this.ast;
|
44 | if (sourceMap) {
|
45 | sourceMap.sources = [this.relativeName];
|
46 | sourceMap.sourcesContent = [this.contents];
|
47 | }
|
48 | return [{
|
49 | type: 'helix-js',
|
50 | value: template,
|
51 | sourceMap,
|
52 | }];
|
53 | }
|
54 | }
|
55 |
|
56 | module.exports = HTLAsset;
|