Skip to content

Commit 9d9db02

Browse files
[squash] feat(index): add support to export HTML as a template {Function} (options.template)
1 parent d7a0f6c commit 9d9db02

File tree

6 files changed

+63
-10
lines changed

6 files changed

+63
-10
lines changed

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,24 @@ export default function loader(html) {
4949
.join('\n');
5050
}
5151

52-
// TODO
52+
const imports = '\n';
53+
5354
// #120 - Support exporting a template function
5455
//
5556
// import template from 'file.html'
5657
//
5758
// const html = template({...locals})
59+
const locals = typeof options.template === 'string'
60+
? options.template
61+
: '_';
5862

59-
const imports = '\n';
63+
const html = options.template
64+
? `function (${locals}) { return \`${result.html}\`; }`
65+
: `\`${result.html}\``
6066

6167
result = [
6268
`// HTML Imports\n${imports}\n`,
63-
`// HTML Content\nexport default \`${result.html}\``,
69+
`// HTML Content\nexport default ${html}`,
6470
].join('\n');
6571

6672
cb(null, result);

src/options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "boolean"
99
},
1010
"template": {
11-
"type": ["boolean", "object"]
11+
"type": [ "boolean", "string" ]
1212
}
1313
},
1414
"additionalProperties": false

test/__snapshots__/Errors.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ exports[`Errors Validation Error 1`] = `
55
66
HTML Loader Invalid Options
77
8-
options.template should be boolean,object
8+
options.template should be boolean,string
99
"
1010
`;

test/fixtures/templates/fixture.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
<title>HTML Loader</title>
66
</head>
77
<body>
8-
<!-- I'm a comment -->
8+
<import src="./file.html"></import>
9+
<!-- Interpolate -->
910
<div id="app">${ _.hello }</div>
11+
12+
<img src="./file.png">
1013
</body>
1114
</html>

test/options/__snapshots__/template.test.js.snap

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,42 @@ exports[`Options template {Boolean} 1`] = `
66
77
88
// HTML Content
9-
export default \`<!DOCTYPE html>
9+
export default function (_) { return \`<!DOCTYPE html>
1010
<html lang=\\"en\\">
1111
<head>
1212
<meta charset=\\"utf-8\\">
1313
<title>HTML Loader</title>
1414
</head>
1515
<body>
16-
<!-- I'm a comment -->
16+
\${HTML__IMPORT__0}
17+
<!-- Interpolate -->
1718
<div id=\\"app\\">\${ _.hello }</div>
19+
20+
<img src=\\"\${HTML__URL__0}\\">
21+
</body>
22+
</html>
23+
\`; }"
24+
`;
25+
26+
exports[`Options template {String} 1`] = `
27+
"// HTML Imports
28+
29+
30+
31+
// HTML Content
32+
export default function ($) { return \`<!DOCTYPE html>
33+
<html lang=\\"en\\">
34+
<head>
35+
<meta charset=\\"utf-8\\">
36+
<title>HTML Loader</title>
37+
</head>
38+
<body>
39+
\${HTML__IMPORT__0}
40+
<!-- Interpolate -->
41+
<div id=\\"app\\">\${ _.hello }</div>
42+
43+
<img src=\\"\${HTML__URL__0}\\">
1844
</body>
1945
</html>
20-
\`"
46+
\`; }"
2147
`;

test/options/template.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@ describe('Options', () => {
99
const config = {
1010
loader: {
1111
test: /\.html$/,
12-
options: {},
12+
options: {
13+
template: true,
14+
},
15+
},
16+
};
17+
18+
const stats = await webpack('templates/fixture.js', config);
19+
const { source } = stats.toJson().modules[1];
20+
21+
expect(source).toMatchSnapshot();
22+
});
23+
24+
test('{String}', async () => {
25+
const config = {
26+
loader: {
27+
test: /\.html$/,
28+
options: {
29+
template: '$',
30+
},
1331
},
1432
};
1533

0 commit comments

Comments
 (0)