123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 'use strict';
- const path = require('path');
- 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');
- module.exports.compileTemplate = function compileTemplate (template, context, outputFilename, compilation) {
-
-
- const outputOptions = {
- filename: outputFilename,
- publicPath: compilation.outputOptions.publicPath
- };
-
- const assetsBeforeCompilation = Object.assign({}, compilation.assets[outputOptions.filename]);
-
-
-
- const compilerName = getCompilerName(context, outputFilename);
- const childCompiler = compilation.createChildCompiler(compilerName, outputOptions);
- childCompiler.context = context;
- new NodeTemplatePlugin(outputOptions).apply(childCompiler);
- new NodeTargetPlugin().apply(childCompiler);
- new LibraryTemplatePlugin('HTML_WEBPACK_PLUGIN_RESULT', 'var').apply(childCompiler);
-
-
-
-
-
- new SingleEntryPlugin(this.context, template, undefined).apply(childCompiler);
- new LoaderTargetPlugin('node').apply(childCompiler);
-
-
-
-
- (childCompiler.hooks ? childCompiler.hooks.compilation.tap.bind(childCompiler.hooks.compilation, 'HtmlWebpackPlugin') : childCompiler.plugin.bind(childCompiler, 'compilation'))(compilation => {
- if (compilation.cache) {
- if (!compilation.cache[compilerName]) {
- compilation.cache[compilerName] = {};
- }
- compilation.cache = compilation.cache[compilerName];
- }
- });
-
- return new Promise((resolve, reject) => {
- childCompiler.runAsChild((err, entries, childCompilation) => {
-
- if (childCompilation && childCompilation.errors && childCompilation.errors.length) {
- const errorDetails = childCompilation.errors.map(error => error.message + (error.error ? ':\n' + error.error : '')).join('\n');
- reject(new Error('Child compilation failed:\n' + errorDetails));
- } else if (err) {
- reject(err);
- } else {
-
-
- const outputName = compilation.mainTemplate.getAssetPath
- ? compilation.mainTemplate.hooks.assetPath.call(outputOptions.filename, {
- hash: childCompilation.hash,
- chunk: entries[0]
- })
- : compilation.mainTemplate.applyPluginsWaterfall(
- 'asset-path',
- outputOptions.filename,
- {
- hash: childCompilation.hash,
- chunk: entries[0]
- });
-
-
- compilation.assets[outputName] = assetsBeforeCompilation[outputName];
- if (assetsBeforeCompilation[outputName] === undefined) {
-
- delete compilation.assets[outputName];
- }
- resolve({
-
- hash: entries[0].hash,
-
- outputName: outputName,
-
- content: childCompilation.assets[outputName].source()
- });
- }
- });
- });
- };
- function getCompilerName (context, filename) {
- const absolutePath = path.resolve(context, filename);
- const relativePath = path.relative(context, absolutePath);
- return 'html-webpack-plugin for "' + (absolutePath.length < relativePath.length ? absolutePath : relativePath) + '"';
- }
|