1 | import * as HtmlWebpackPlugin from 'html-webpack-plugin';
|
2 | import { load } from 'cheerio';
|
3 | import { readFileSync } from 'fs';
|
4 | import { join, dirname } from 'path';
|
5 | import { Compiler, Plugin } from 'webpack';
|
6 | import { getTemplates, extractParts, setEntries } from './helpers';
|
7 |
|
8 | export interface Html5EntryWebpackPluginOptions {}
|
9 |
|
10 | export class Html5EntryWebpackPlugin implements Plugin {
|
11 | constructor(private options: Html5EntryWebpackPluginOptions = {}) {}
|
12 |
|
13 | apply(compiler: Compiler) {
|
14 | const entry = compiler.options.entry;
|
15 | const [template] = getTemplates(entry);
|
16 |
|
17 | if (template) {
|
18 | const src = dirname(template);
|
19 | const templateContent = load(readFileSync(template, 'utf8'));
|
20 | const entries = extractParts(templateContent).map(entry => join(src, entry));
|
21 | const plugins = [
|
22 | new HtmlWebpackPlugin({
|
23 | templateContent: templateContent.html(),
|
24 | }),
|
25 | ];
|
26 |
|
27 | setEntries(compiler.options, template, entries);
|
28 | plugins.forEach(plugin => plugin.apply(compiler));
|
29 | }
|
30 | }
|
31 | }
|