123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 'use strict';
- let instanceId = 0;
- class HtmlWebpackChildCompiler {
-
- constructor (templates) {
-
- this.id = instanceId++;
-
- 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) {
- const webpack = mainCompilation.compiler.webpack;
- const Compilation = webpack.Compilation;
- const NodeTemplatePlugin = webpack.node.NodeTemplatePlugin;
- const NodeTargetPlugin = webpack.node.NodeTargetPlugin;
- const LoaderTargetPlugin = webpack.LoaderTargetPlugin;
- const EntryPlugin = webpack.EntryPlugin;
-
-
-
- if (this.compilationPromise) {
- return this.compilationPromise;
- }
- const outputOptions = {
- filename: '__child-[name]',
- publicPath: '',
- library: {
- type: 'var',
- name: 'HTML_WEBPACK_PLUGIN_RESULT'
- },
- scriptType: ('text/javascript'),
- iife: true
- };
- const compilerName = 'HtmlWebpackCompiler';
-
-
-
- const childCompiler = mainCompilation.createChildCompiler(compilerName, outputOptions, [
-
- new NodeTargetPlugin(),
- new NodeTemplatePlugin(),
- new LoaderTargetPlugin('node'),
- new webpack.library.EnableLibraryPlugin('var')
- ]);
-
- childCompiler.context = mainCompilation.compiler.context;
-
- const temporaryTemplateNames = this.templates.map((template, index) => `__child-HtmlWebpackPlugin_${index}-${this.id}`);
-
- this.templates.forEach((template, index) => {
- new EntryPlugin(childCompiler.context, 'data:text/javascript,__webpack_public_path__ = __webpack_base_uri__ = htmlWebpackPluginPublicPath;', `HtmlWebpackPlugin_${index}-${this.id}`).apply(childCompiler);
- new EntryPlugin(childCompiler.context, template, `HtmlWebpackPlugin_${index}-${this.id}`).apply(childCompiler);
- });
-
-
-
- childCompiler.options.module = { ...childCompiler.options.module };
- childCompiler.options.module.parser = { ...childCompiler.options.module.parser };
- childCompiler.options.module.parser.javascript = { ...childCompiler.options.module.parser.javascript,
- url: 'relative' };
- this.compilationStartedTimestamp = new Date().getTime();
- this.compilationPromise = new Promise((resolve, reject) => {
- const extractedAssets = [];
- childCompiler.hooks.thisCompilation.tap('HtmlWebpackPlugin', (compilation) => {
- compilation.hooks.processAssets.tap(
- {
- name: 'HtmlWebpackPlugin',
- stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
- },
- (assets) => {
- temporaryTemplateNames.forEach((temporaryTemplateName) => {
- if (assets[temporaryTemplateName]) {
- extractedAssets.push(assets[temporaryTemplateName]);
- compilation.deleteAsset(temporaryTemplateName);
- }
- });
- }
- );
- });
- childCompiler.runAsChild((err, entries, childCompilation) => {
-
- const compiledTemplates = entries
- ? extractedAssets.map((asset) => asset.source())
- : [];
-
- if (entries && childCompilation) {
- 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.stack) {
- message += '\n' + error.stack;
- }
- return message;
- }).join('\n');
- reject(new Error('Child compilation failed:\n' + errorDetails));
- return;
- }
-
- if (err) {
- reject(err);
- return;
- }
- if (!childCompilation || !entries) {
- reject(new Error('Empty child compilation'));
- return;
- }
-
- const result = {};
- compiledTemplates.forEach((templateSource, entryIndex) => {
-
-
-
- result[this.templates[entryIndex]] = {
- content: templateSource,
- hash: childCompilation.hash || 'XXXX',
- entry: entries[entryIndex]
- };
- });
- this.compilationEndedTimestamp = new Date().getTime();
- resolve(result);
- });
- });
- return this.compilationPromise;
- }
- }
- module.exports = {
- HtmlWebpackChildCompiler
- };
|