| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { CachedInputFileSystem } = require("enhanced-resolve");
|
|---|
| 9 | const fs = require("graceful-fs");
|
|---|
| 10 | const createConsoleLogger = require("../logging/createConsoleLogger");
|
|---|
| 11 | const NodeWatchFileSystem = require("./NodeWatchFileSystem");
|
|---|
| 12 | const nodeConsole = require("./nodeConsole");
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
|
|---|
| 15 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 16 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Defines the node environment plugin options type used by this module.
|
|---|
| 20 | * @typedef {object} NodeEnvironmentPluginOptions
|
|---|
| 21 | * @property {InfrastructureLogging} infrastructureLogging infrastructure logging options
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | const PLUGIN_NAME = "NodeEnvironmentPlugin";
|
|---|
| 25 |
|
|---|
| 26 | class NodeEnvironmentPlugin {
|
|---|
| 27 | /**
|
|---|
| 28 | * Creates an instance of NodeEnvironmentPlugin.
|
|---|
| 29 | * @param {NodeEnvironmentPluginOptions} options options
|
|---|
| 30 | */
|
|---|
| 31 | constructor(options) {
|
|---|
| 32 | /** @type {NodeEnvironmentPluginOptions} */
|
|---|
| 33 | this.options = options;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 38 | * @param {Compiler} compiler the compiler instance
|
|---|
| 39 | * @returns {void}
|
|---|
| 40 | */
|
|---|
| 41 | apply(compiler) {
|
|---|
| 42 | const { infrastructureLogging } = this.options;
|
|---|
| 43 | compiler.infrastructureLogger = createConsoleLogger({
|
|---|
| 44 | level: infrastructureLogging.level || "info",
|
|---|
| 45 | debug: infrastructureLogging.debug || false,
|
|---|
| 46 | console:
|
|---|
| 47 | infrastructureLogging.console ||
|
|---|
| 48 | nodeConsole({
|
|---|
| 49 | colors: infrastructureLogging.colors,
|
|---|
| 50 | appendOnly: infrastructureLogging.appendOnly,
|
|---|
| 51 | stream:
|
|---|
| 52 | /** @type {NodeJS.WritableStream} */
|
|---|
| 53 | (infrastructureLogging.stream),
|
|---|
| 54 | compiler
|
|---|
| 55 | })
|
|---|
| 56 | });
|
|---|
| 57 | // @ts-expect-error need to fix on enhanced-resolve side
|
|---|
| 58 | compiler.inputFileSystem = new CachedInputFileSystem(fs, 60000);
|
|---|
| 59 | const inputFileSystem =
|
|---|
| 60 | /** @type {InputFileSystem} */
|
|---|
| 61 | (compiler.inputFileSystem);
|
|---|
| 62 | compiler.outputFileSystem = fs;
|
|---|
| 63 | compiler.intermediateFileSystem = fs;
|
|---|
| 64 | compiler.watchFileSystem = new NodeWatchFileSystem(inputFileSystem);
|
|---|
| 65 | compiler.hooks.beforeRun.tap(PLUGIN_NAME, (compiler) => {
|
|---|
| 66 | if (
|
|---|
| 67 | compiler.inputFileSystem === inputFileSystem &&
|
|---|
| 68 | inputFileSystem.purge
|
|---|
| 69 | ) {
|
|---|
| 70 | compiler.fsStartTime = Date.now();
|
|---|
| 71 | inputFileSystem.purge();
|
|---|
| 72 | }
|
|---|
| 73 | });
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | module.exports = NodeEnvironmentPlugin;
|
|---|