123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 'use strict';
- 'use strict';
- const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
- const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
- const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
- const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
- const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
- class HtmlWebpackChildCompiler {
-
- constructor (templates) {
-
- this.templates = templates;
-
- this.compilationPromise;
-
- this.compilationStartedTimestamp;
-
- this.compilationEndedTimestamp;
-
- this.fileDependencies = { fileDependencies: [], contextDependencies: [], missingDependencies: [] };
- }
-
- isCompiling () {
- return !this.didCompile() && this.compilationStartedTimestamp !== undefined;
- }
-
- didCompile () {
- return this.compilationEndedTimestamp !== undefined;
- }
-
- compileTemplates (mainCompilation) {
-
-
-
- if (this.compilationPromise) {
- return this.compilationPromise;
- }
-
-
- const outputOptions = {
- filename: '__child-[name]',
- publicPath: mainCompilation.outputOptions.publicPath
- };
- const compilerName = 'HtmlWebpackCompiler';
-
-
-
- const childCompiler = mainCompilation.createChildCompiler(compilerName, outputOptions);
-
- childCompiler.context = mainCompilation.compiler.context;
-
- new NodeTemplatePlugin(outputOptions).apply(childCompiler);
- new NodeTargetPlugin().apply(childCompiler);
- new LibraryTemplatePlugin('HTML_WEBPACK_PLUGIN_RESULT', 'var').apply(childCompiler);
- new LoaderTargetPlugin('node').apply(childCompiler);
-
- this.templates.forEach((template, index) => {
- new SingleEntryPlugin(childCompiler.context, template, `HtmlWebpackPlugin_${index}`).apply(childCompiler);
- });
- this.compilationStartedTimestamp = new Date().getTime();
- this.compilationPromise = new Promise((resolve, reject) => {
- childCompiler.runAsChild((err, entries, childCompilation) => {
-
- const compiledTemplates = entries
- ? extractHelperFilesFromCompilation(mainCompilation, childCompilation, outputOptions.filename, entries)
- : [];
-
- if (entries) {
- this.fileDependencies = { fileDependencies: Array.from(childCompilation.fileDependencies), contextDependencies: Array.from(childCompilation.contextDependencies), missingDependencies: Array.from(childCompilation.missingDependencies) };
- }
-
- if (childCompilation && childCompilation.errors && childCompilation.errors.length) {
- const errorDetails = childCompilation.errors.map(error => {
- let message = error.message;
- if (error.error) {
- message += ':\n' + error.error;
- }
- if (error.stack) {
- message += '\n' + error.stack;
- }
- return message;
- }).join('\n');
- reject(new Error('Child compilation failed:\n' + errorDetails));
- return;
- }
-
- if (err) {
- reject(err);
- return;
- }
-
- const result = {};
- compiledTemplates.forEach((templateSource, entryIndex) => {
-
-
-
- result[this.templates[entryIndex]] = {
- content: templateSource,
- hash: childCompilation.hash,
- entry: entries[entryIndex]
- };
- });
- this.compilationEndedTimestamp = new Date().getTime();
- resolve(result);
- });
- });
- return this.compilationPromise;
- }
- }
- function extractHelperFilesFromCompilation (mainCompilation, childCompilation, filename, childEntryChunks) {
- const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
- const helperAssetNames = childEntryChunks.map((entryChunk, index) => {
- const entryConfig = {
- hash: childCompilation.hash,
- chunk: entryChunk,
- name: `HtmlWebpackPlugin_${index}`
- };
- return webpackMajorVersion === 4
- ? mainCompilation.mainTemplate.getAssetPath(filename, entryConfig)
- : mainCompilation.getAssetPath(filename, entryConfig);
- });
- helperAssetNames.forEach((helperFileName) => {
- delete mainCompilation.assets[helperFileName];
- });
- const helperContents = helperAssetNames.map((helperFileName) => {
- return childCompilation.assets[helperFileName].source();
- });
- return helperContents;
- }
- module.exports = {
- HtmlWebpackChildCompiler
- };
|