UNPKG

1.04 kBPlain TextView Raw
1import * as HtmlWebpackPlugin from 'html-webpack-plugin';
2import { load } from 'cheerio';
3import { readFileSync } from 'fs';
4import { join, dirname } from 'path';
5import { Compiler, Plugin } from 'webpack';
6import { getTemplates, extractParts, setEntries } from './helpers';
7
8export interface Html5EntryWebpackPluginOptions {}
9
10export 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}