Skip to content

Commit

Permalink
refactor: avoid extra file (#1806)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 19, 2023
1 parent 6bf471c commit 8570383
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 85 deletions.
81 changes: 67 additions & 14 deletions lib/cached-child-compiler.js
Expand Up @@ -24,10 +24,10 @@
'use strict';

// Import types
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
/** @typedef {import("webpack/lib/Compiler.js")} Compiler */
/** @typedef {import("webpack/lib/Compilation.js")} Compilation */
/** @typedef {import("webpack/lib/FileSystemInfo").Snapshot} Snapshot */
/** @typedef {{hash: string, entry: any, content: string }} ChildCompilationResultEntry */
/** @typedef {import("./file-watcher-api").Snapshot} Snapshot */
/** @typedef {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} FileDependencies */
/** @typedef {{
dependencies: FileDependencies,
Expand All @@ -38,24 +38,23 @@
}} ChildCompilationResult */

const { HtmlWebpackChildCompiler } = require('./child-compiler');
const fileWatcherApi = require('./file-watcher-api');

/**
* This plugin is a singleton for performance reasons.
* To keep track if a plugin does already exist for the compiler they are cached
* in this map
* @type {WeakMap<WebpackCompiler, PersistentChildCompilerSingletonPlugin>}}
* @type {WeakMap<Compiler, PersistentChildCompilerSingletonPlugin>}}
*/
const compilerMap = new WeakMap();

class CachedChildCompilation {
/**
* @param {WebpackCompiler} compiler
* @param {Compiler} compiler
*/
constructor (compiler) {
/**
* @private
* @type {WebpackCompiler}
* @type {Compiler}
*/
this.compiler = compiler;
// Create a singleton instance for the compiler
Expand Down Expand Up @@ -114,6 +113,60 @@ class CachedChildCompilation {
}

class PersistentChildCompilerSingletonPlugin {
/**
*
* @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies
* @param {Compilation} mainCompilation
* @param {number} startTime
*/
static createSnapshot (fileDependencies, mainCompilation, startTime) {
return new Promise((resolve, reject) => {
mainCompilation.fileSystemInfo.createSnapshot(
startTime,
fileDependencies.fileDependencies,
fileDependencies.contextDependencies,
fileDependencies.missingDependencies,
null,
(err, snapshot) => {
if (err) {
return reject(err);
}
resolve(snapshot);
}
);
});
}

/**
* Returns true if the files inside this snapshot
* have not been changed
*
* @param {Snapshot} snapshot
* @param {Compilation} mainCompilation
* @returns {Promise<boolean>}
*/
static isSnapshotValid (snapshot, mainCompilation) {
return new Promise((resolve, reject) => {
mainCompilation.fileSystemInfo.checkSnapshotValid(
snapshot,
(err, isValid) => {
if (err) {
reject(err);
}
resolve(isValid);
}
);
});
}

static watchFiles (mainCompilation, fileDependencies) {
Object.keys(fileDependencies).forEach((depencyTypes) => {
fileDependencies[depencyTypes].forEach(fileDependency => {
mainCompilation[depencyTypes].add(fileDependency);
});
});
}

constructor () {
/**
* @private
Expand Down Expand Up @@ -158,7 +211,7 @@ class PersistentChildCompilerSingletonPlugin {

/**
* apply is called by the webpack main compiler during the start phase
* @param {WebpackCompiler} compiler
* @param {Compiler} compiler
*/
apply (compiler) {
/** @type Promise<ChildCompilationResult> */
Expand Down Expand Up @@ -216,7 +269,7 @@ class PersistentChildCompilerSingletonPlugin {
// this might possibly cause bugs if files were changed inbetween
// compilation start and snapshot creation
compiledEntriesPromise.then((childCompilationResult) => {
return fileWatcherApi.createSnapshot(childCompilationResult.dependencies, mainCompilation, compilationStartTime);
return PersistentChildCompilerSingletonPlugin.createSnapshot(childCompilationResult.dependencies, mainCompilation, compilationStartTime);
}).then((snapshot) => {
previousFileSystemSnapshot = snapshot;
});
Expand Down Expand Up @@ -309,7 +362,7 @@ class PersistentChildCompilerSingletonPlugin {
* Verify that the cache is still valid
* @private
* @param {Snapshot | undefined} snapshot
* @param {WebpackCompilation} mainCompilation
* @param {Compilation} mainCompilation
* @returns {Promise<boolean>}
*/
isCacheValid (snapshot, mainCompilation) {
Expand All @@ -328,14 +381,14 @@ class PersistentChildCompilerSingletonPlugin {
if (!snapshot) {
return Promise.resolve(false);
}
return fileWatcherApi.isSnapShotValid(snapshot, mainCompilation);
return PersistentChildCompilerSingletonPlugin.isSnapshotValid(snapshot, mainCompilation);
}

/**
* Start to compile all templates
*
* @private
* @param {WebpackCompilation} mainCompilation
* @param {Compilation} mainCompilation
* @param {string[]} entries
* @returns {Promise<ChildCompilationResult>}
*/
Expand Down Expand Up @@ -366,11 +419,11 @@ class PersistentChildCompilerSingletonPlugin {

/**
* @private
* @param {WebpackCompilation} mainCompilation
* @param {Compilation} mainCompilation
* @param {FileDependencies} files
*/
watchFiles (mainCompilation, files) {
fileWatcherApi.watchFiles(mainCompilation, files);
PersistentChildCompilerSingletonPlugin.watchFiles(mainCompilation, files);
}
}

Expand Down
71 changes: 0 additions & 71 deletions lib/file-watcher-api.js

This file was deleted.

0 comments on commit 8570383

Please sign in to comment.