UNPKG

1.12 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 extends Omit<HtmlWebpackPlugin.Options, "templateContent"> {}
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 ...this.options,
24 templateContent: templateContent.html(),
25 }),
26 ];
27
28 setEntries(compiler.options, template, entries);
29 plugins.forEach(plugin => plugin.apply(compiler));
30 }
31 }
32}