Files
node_modules
.bin
@ampproject
@babel
@discoveryjs
@gar
@istanbuljs
@jridgewell
@leichtgewicht
@npmcli
@riotjs
@tootallnate
@types
@ungap
@webassemblyjs
@webpack-cli
@xtuc
abab
abbrev
accepts
acorn
acorn-globals
acorn-import-assertions
acorn-walk
agent-base
agentkeepalive
aggregate-error
ajv
ajv-formats
ajv-keywords
ansi-colors
ansi-html-community
ansi-regex
ansi-styles
anymatch
append-transform
aproba
archy
are-we-there-yet
argparse
array-flatten
arrify
assertion-error
ast-types
async-foreach
asynckit
babel-plugin-dynamic-import-node
babel-plugin-polyfill-corejs2
babel-plugin-polyfill-corejs3
babel-plugin-polyfill-regenerator
balanced-match
batch
bianco.attr
bianco.dom-to-array
bianco.events
bianco.query
big.js
binary-extensions
body-parser
bonjour-service
boolbase
brace-expansion
braces
browser-process-hrtime
browser-stdout
browserslist
buffer-from
bytes
cacache
caching-transform
call-bind
camel-case
camelcase
camelcase-keys
caniuse-lite
chai
chalk
check-error
chokidar
chownr
chrome-trace-event
clean-css
clean-stack
cliui
clone-deep
color-convert
color-name
color-support
colorette
combined-stream
commander
commondir
compressible
compression
concat-map
connect-history-api-fallback
console-control-strings
content-disposition
content-type
convert-source-map
cookie
cookie-signature
core-js-compat
core-util-is
cross-spawn
css-select
css-what
cssesc
cssom
cssstyle
cumpa
curri
data-urls
debug
decamelize
decamelize-keys
decimal.js
deep-eql
deep-is
default-gateway
default-require-extensions
define-lazy-prop
define-properties
delayed-stream
delegates
depd
destroy
detect-node
diff
dns-equal
dns-packet
dom-converter
dom-nodes
dom-serializer
domelementtype
domexception
domhandler
domutils
dot-case
ee-first
electron-to-chromium
emoji-regex
emojis-list
encodeurl
encoding
enhanced-resolve
entities
env-paths
envinfo
err-code
erre
error-ex
es-module-lexer
es6-error
escalade
escape-html
escape-string-regexp
escodegen
eslint-scope
esm
esprima
esrecurse
estraverse
esutils
etag
eventemitter3
events
execa
express
fast-deep-equal
fast-json-stable-stringify
fast-levenshtein
fastest-levenshtein
faye-websocket
fill-range
finalhandler
find-up
flat
follow-redirects
foreground-child
form-data
forwarded
fresh
fromentries
fs-minipass
fs-monkey
fs.realpath
function-bind
gauge
gaze
gensync
get-caller-file
get-func-name
get-intrinsic
get-package-type
get-stdin
get-stream
glob
glob-parent
glob-to-regexp
globals
globule
graceful-fs
growl
handle-thing
hard-rejection
has
has-flag
has-property-descriptors
has-symbols
has-unicode
hasha
he
hosted-git-info
hpack.js
html-encoding-sniffer
html-entities
html-escaper
html-minifier-terser
html-webpack-plugin
lib
cached-child-compiler.js
child-compiler.js
chunksorter.js
errors.js
file-watcher-api.js
hooks.js
html-tags.js
loader.js
CHANGELOG.md
LICENSE
README.md
default_index.ejs
index.js
package.json
typings.d.ts
htmlparser2
http-cache-semantics
http-deceiver
http-errors
http-parser-js
http-proxy
http-proxy-agent
http-proxy-middleware
https-proxy-agent
human-signals
humanize-ms
iconv-lite
imurmurhash
indent-string
infer-owner
inflight
inherits
interpret
ip
ipaddr.js
is-arrayish
is-binary-path
is-core-module
is-docker
is-extglob
is-fullwidth-code-point
is-glob
is-lambda
is-number
is-plain-obj
is-plain-object
is-potential-custom-element-name
is-stream
is-typedarray
is-windows
is-wsl
isarray
isexe
isobject
istanbul-lib-coverage
istanbul-lib-hook
istanbul-lib-instrument
istanbul-lib-processinfo
istanbul-lib-report
istanbul-lib-source-maps
istanbul-reports
jest-worker
js-base64
js-tokens
js-yaml
jsdom
jsdom-global
jsesc
json-parse-even-better-errors
json-schema-traverse
json5
kind-of
levn
lines-and-columns
loader-runner
locate-path
lodash
lodash.debounce
lodash.flattendeep
log-symbols
loupe
lower-case
lru-cache
make-fetch-happen
map-obj
media-typer
memfs
meow
merge-descriptors
merge-stream
methods
micromatch
mime
mime-db
mime-types
mimic-fn
min-indent
minimalistic-assert
minimatch
minimist-options
minipass
minipass-collect
minipass-fetch
minipass-flush
minipass-pipeline
minipass-sized
minizlib
mkdirp
mocha
ms
multicast-dns
nan
nanoid
negotiator
neo-async
no-case
node-forge
node-gyp
node-preload
node-releases
node-sass
nopt
normalize-package-data
normalize-path
npm-run-path
npmlog
nth-check
nwsapi
nyc
object-inspect
object-keys
object.assign
obuf
on-finished
on-headers
once
onetime
open
optionator
p-limit
p-locate
p-map
p-retry
p-try
package-hash
param-case
parse-json
parse5
parseurl
pascal-case
path-exists
path-is-absolute
path-key
path-parse
path-to-regexp
pathval
picocolors
picomatch
pirates
prelude-ls
pretty-error
process-nextick-args
process-on-spawn
promise-inflight
promise-retry
proxy-addr
psl
punycode
qs
quick-lru
randombytes
range-parser
raw-body
rawth
read-pkg
read-pkg-up
readable-stream
readdirp
recast
rechoir
redent
regenerate
regenerate-unicode-properties
regenerator-runtime
regenerator-transform
regexpu-core
regjsgen
regjsparser
relateurl
release-zalgo
renderkid
require-directory
require-from-string
require-main-filename
requires-port
resolve
retry
rimraf
riot
ruit
safe-buffer
safer-buffer
sass-graph
saxes
schema-utils
scss-tokenizer
select-hose
selfsigned
semver
send
serialize-javascript
serve-index
serve-static
set-blocking
setprototypeof
shallow-clone
shebang-command
shebang-regex
side-channel
signal-exit
smart-buffer
sockjs
socks
socks-proxy-agent
source-map
source-map-support
spawn-wrap
spdx-correct
spdx-exceptions
spdx-expression-parse
spdx-license-ids
spdy
spdy-transport
sprintf-js
ssri
statuses
stdout-stream
string-width
string_decoder
strip-ansi
strip-final-newline
strip-indent
strip-json-comments
supports-color
symbol-tree
tapable
tar
terser
terser-webpack-plugin
test-exclude
thunky
to-fast-properties
to-regex-range
toidentifier
tough-cookie
tr46
trim-newlines
true-case-path
tslib
type-check
type-detect
type-fest
type-is
typedarray-to-buffer
unicode-canonical-property-names-ecmascript
unicode-match-property-ecmascript
unicode-match-property-value-ecmascript
unicode-property-aliases-ecmascript
unique-filename
unique-slug
universalify
unpipe
update-browserslist-db
uri-js
util-deprecate
utila
utils-merge
uuid
validate-npm-package-license
vary
w3c-hr-time
w3c-xmlserializer
watchpack
wbuf
webidl-conversions
webpack
webpack-cli
webpack-dev-middleware
webpack-dev-server
webpack-merge
webpack-sources
websocket-driver
websocket-extensions
whatwg-encoding
whatwg-mimetype
whatwg-url
which
which-module
wide-align
wildcard
word-wrap
workerpool
wrap-ansi
wrappy
write-file-atomic
ws
xml-name-validator
xmlchars
y18n
yallist
yargs
yargs-parser
yargs-unparser
yocto-queue
.package-lock.json
src
LICENSE
package-lock.json
package.json
readme.md
webpack.config.js
parcoursup/node_modules/html-webpack-plugin/lib/cached-child-compiler.js

380 lines
13 KiB
JavaScript
Raw Normal View History

2023-03-05 13:23:23 +01:00
// @ts-check
/**
* @file
* Helper plugin manages the cached state of the child compilation
*
* To optimize performance the child compilation is running asyncronously.
* Therefore it needs to be started in the compiler.make phase and ends after
* the compilation.afterCompile phase.
*
* To prevent bugs from blocked hooks there is no promise or event based api
* for this plugin.
*
* Example usage:
*
* ```js
const childCompilerPlugin = new PersistentChildCompilerPlugin();
childCompilerPlugin.addEntry('./src/index.js');
compiler.hooks.afterCompile.tapAsync('MyPlugin', (compilation, callback) => {
console.log(childCompilerPlugin.getCompilationResult()['./src/index.js']));
return true;
});
* ```
*/
// Import types
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
/** @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,
compiledEntries: {[entryName: string]: ChildCompilationResultEntry}
} | {
dependencies: FileDependencies,
error: Error
}} ChildCompilationResult */
'use strict';
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>}}
*/
const compilerMap = new WeakMap();
class CachedChildCompilation {
/**
* @param {WebpackCompiler} compiler
*/
constructor (compiler) {
/**
* @private
* @type {WebpackCompiler}
*/
this.compiler = compiler;
// Create a singleton instance for the compiler
// if there is none
if (compilerMap.has(compiler)) {
return;
}
const persistentChildCompilerSingletonPlugin = new PersistentChildCompilerSingletonPlugin();
compilerMap.set(compiler, persistentChildCompilerSingletonPlugin);
persistentChildCompilerSingletonPlugin.apply(compiler);
}
/**
* apply is called by the webpack main compiler during the start phase
* @param {string} entry
*/
addEntry (entry) {
const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
if (!persistentChildCompilerSingletonPlugin) {
throw new Error(
'PersistentChildCompilerSingletonPlugin instance not found.'
);
}
persistentChildCompilerSingletonPlugin.addEntry(entry);
}
getCompilationResult () {
const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
if (!persistentChildCompilerSingletonPlugin) {
throw new Error(
'PersistentChildCompilerSingletonPlugin instance not found.'
);
}
return persistentChildCompilerSingletonPlugin.getLatestResult();
}
/**
* Returns the result for the given entry
* @param {string} entry
* @returns {
| { mainCompilationHash: string, error: Error }
| { mainCompilationHash: string, compiledEntry: ChildCompilationResultEntry }
}
*/
getCompilationEntryResult (entry) {
const latestResult = this.getCompilationResult();
const compilationResult = latestResult.compilationResult;
return 'error' in compilationResult ? {
mainCompilationHash: latestResult.mainCompilationHash,
error: compilationResult.error
} : {
mainCompilationHash: latestResult.mainCompilationHash,
compiledEntry: compilationResult.compiledEntries[entry]
};
}
}
class PersistentChildCompilerSingletonPlugin {
constructor () {
/**
* @private
* @type {
| {
isCompiling: false,
isVerifyingCache: false,
entries: string[],
compiledEntries: string[],
mainCompilationHash: string,
compilationResult: ChildCompilationResult
}
| Readonly<{
isCompiling: false,
isVerifyingCache: true,
entries: string[],
previousEntries: string[],
previousResult: ChildCompilationResult
}>
| Readonly <{
isVerifyingCache: false,
isCompiling: true,
entries: string[],
}>
} the internal compilation state */
this.compilationState = {
isCompiling: false,
isVerifyingCache: false,
entries: [],
compiledEntries: [],
mainCompilationHash: 'initial',
compilationResult: {
dependencies: {
fileDependencies: [],
contextDependencies: [],
missingDependencies: []
},
compiledEntries: {}
}
};
}
/**
* apply is called by the webpack main compiler during the start phase
* @param {WebpackCompiler} compiler
*/
apply (compiler) {
/** @type Promise<ChildCompilationResult> */
let childCompilationResultPromise = Promise.resolve({
dependencies: {
fileDependencies: [],
contextDependencies: [],
missingDependencies: []
},
compiledEntries: {}
});
/**
* The main compilation hash which will only be updated
* if the childCompiler changes
*/
let mainCompilationHashOfLastChildRecompile = '';
/** @typedef{Snapshot|undefined} */
let previousFileSystemSnapshot;
let compilationStartTime = new Date().getTime();
compiler.hooks.make.tapAsync(
'PersistentChildCompilerSingletonPlugin',
(mainCompilation, callback) => {
if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
return callback(new Error('Child compilation has already started'));
}
// Update the time to the current compile start time
compilationStartTime = new Date().getTime();
// The compilation starts - adding new templates is now not possible anymore
this.compilationState = {
isCompiling: false,
isVerifyingCache: true,
previousEntries: this.compilationState.compiledEntries,
previousResult: this.compilationState.compilationResult,
entries: this.compilationState.entries
};
// Validate cache:
const isCacheValidPromise = this.isCacheValid(previousFileSystemSnapshot, mainCompilation);
let cachedResult = childCompilationResultPromise;
childCompilationResultPromise = isCacheValidPromise.then((isCacheValid) => {
// Reuse cache
if (isCacheValid) {
return cachedResult;
}
// Start the compilation
const compiledEntriesPromise = this.compileEntries(
mainCompilation,
this.compilationState.entries
);
// Update snapshot as soon as we know the filedependencies
// 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);
}).then((snapshot) => {
previousFileSystemSnapshot = snapshot;
});
return compiledEntriesPromise;
});
// Add files to compilation which needs to be watched:
mainCompilation.hooks.optimizeTree.tapAsync(
'PersistentChildCompilerSingletonPlugin',
(chunks, modules, callback) => {
const handleCompilationDonePromise = childCompilationResultPromise.then(
childCompilationResult => {
this.watchFiles(
mainCompilation,
childCompilationResult.dependencies
);
});
handleCompilationDonePromise.then(() => callback(null, chunks, modules), callback);
}
);
// Store the final compilation once the main compilation hash is known
mainCompilation.hooks.additionalAssets.tapAsync(
'PersistentChildCompilerSingletonPlugin',
(callback) => {
const didRecompilePromise = Promise.all([childCompilationResultPromise, cachedResult]).then(
([childCompilationResult, cachedResult]) => {
// Update if childCompilation changed
return (cachedResult !== childCompilationResult);
}
);
const handleCompilationDonePromise = Promise.all([childCompilationResultPromise, didRecompilePromise]).then(
([childCompilationResult, didRecompile]) => {
// Update hash and snapshot if childCompilation changed
if (didRecompile) {
mainCompilationHashOfLastChildRecompile = mainCompilation.hash;
}
this.compilationState = {
isCompiling: false,
isVerifyingCache: false,
entries: this.compilationState.entries,
compiledEntries: this.compilationState.entries,
compilationResult: childCompilationResult,
mainCompilationHash: mainCompilationHashOfLastChildRecompile
};
});
handleCompilationDonePromise.then(() => callback(null), callback);
}
);
// Continue compilation:
callback(null);
}
);
}
/**
* Add a new entry to the next compile run
* @param {string} entry
*/
addEntry (entry) {
if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
throw new Error(
'The child compiler has already started to compile. ' +
"Please add entries before the main compiler 'make' phase has started or " +
'after the compilation is done.'
);
}
if (this.compilationState.entries.indexOf(entry) === -1) {
this.compilationState.entries = [...this.compilationState.entries, entry];
}
}
getLatestResult () {
if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
throw new Error(
'The child compiler is not done compiling. ' +
"Please access the result after the compiler 'make' phase has started or " +
'after the compilation is done.'
);
}
return {
mainCompilationHash: this.compilationState.mainCompilationHash,
compilationResult: this.compilationState.compilationResult
};
}
/**
* Verify that the cache is still valid
* @private
* @param {Snapshot | undefined} snapshot
* @param {WebpackCompilation} mainCompilation
* @returns {Promise<boolean>}
*/
isCacheValid (snapshot, mainCompilation) {
if (!this.compilationState.isVerifyingCache) {
return Promise.reject(new Error('Cache validation can only be done right before the compilation starts'));
}
// If there are no entries we don't need a new child compilation
if (this.compilationState.entries.length === 0) {
return Promise.resolve(true);
}
// If there are new entries the cache is invalid
if (this.compilationState.entries !== this.compilationState.previousEntries) {
return Promise.resolve(false);
}
// Mark the cache as invalid if there is no snapshot
if (!snapshot) {
return Promise.resolve(false);
}
return fileWatcherApi.isSnapShotValid(snapshot, mainCompilation);
}
/**
* Start to compile all templates
*
* @private
* @param {WebpackCompilation} mainCompilation
* @param {string[]} entries
* @returns {Promise<ChildCompilationResult>}
*/
compileEntries (mainCompilation, entries) {
const compiler = new HtmlWebpackChildCompiler(entries);
return compiler.compileTemplates(mainCompilation).then((result) => {
return {
// The compiled sources to render the content
compiledEntries: result,
// The file dependencies to find out if a
// recompilation is required
dependencies: compiler.fileDependencies,
// The main compilation hash can be used to find out
// if this compilation was done during the current compilation
mainCompilationHash: mainCompilation.hash
};
}, error => ({
// The compiled sources to render the content
error,
// The file dependencies to find out if a
// recompilation is required
dependencies: compiler.fileDependencies,
// The main compilation hash can be used to find out
// if this compilation was done during the current compilation
mainCompilationHash: mainCompilation.hash
}));
}
/**
* @private
* @param {WebpackCompilation} mainCompilation
* @param {FileDependencies} files
*/
watchFiles (mainCompilation, files) {
fileWatcherApi.watchFiles(mainCompilation, files);
}
}
module.exports = {
CachedChildCompilation
};