first
This commit is contained in:
172
node_modules/webpack-dev-middleware/dist/utils/compatibleAPI.js
generated
vendored
Normal file
172
node_modules/webpack-dev-middleware/dist/utils/compatibleAPI.js
generated
vendored
Normal file
@ -0,0 +1,172 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
||||
|
||||
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
||||
|
||||
/**
|
||||
* @typedef {Object} ExpectedRequest
|
||||
* @property {(name: string) => string | undefined} get
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} ExpectedResponse
|
||||
* @property {(name: string) => string | string[] | undefined} get
|
||||
* @property {(name: string, value: number | string | string[]) => void} set
|
||||
* @property {(status: number) => void} status
|
||||
* @property {(data: any) => void} send
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {ServerResponse} Response
|
||||
* @param {Response} res
|
||||
* @returns {string[]}
|
||||
*/
|
||||
function getHeaderNames(res) {
|
||||
if (typeof res.getHeaderNames !== "function") {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
return Object.keys(res._headers || {});
|
||||
}
|
||||
|
||||
return res.getHeaderNames();
|
||||
}
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @param {Request} req
|
||||
* @param {string} name
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
|
||||
|
||||
function getHeaderFromRequest(req, name) {
|
||||
// Express API
|
||||
if (typeof
|
||||
/** @type {Request & ExpectedRequest} */
|
||||
req.get === "function") {
|
||||
return (
|
||||
/** @type {Request & ExpectedRequest} */
|
||||
req.get(name)
|
||||
);
|
||||
} // Node.js API
|
||||
// @ts-ignore
|
||||
|
||||
|
||||
return req.headers[name];
|
||||
}
|
||||
/**
|
||||
* @template {ServerResponse} Response
|
||||
* @param {Response} res
|
||||
* @param {string} name
|
||||
* @returns {number | string | string[] | undefined}
|
||||
*/
|
||||
|
||||
|
||||
function getHeaderFromResponse(res, name) {
|
||||
// Express API
|
||||
if (typeof
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.get === "function") {
|
||||
return (
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.get(name)
|
||||
);
|
||||
} // Node.js API
|
||||
|
||||
|
||||
return res.getHeader(name);
|
||||
}
|
||||
/**
|
||||
* @template {ServerResponse} Response
|
||||
* @param {Response} res
|
||||
* @param {string} name
|
||||
* @param {number | string | string[]} value
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
|
||||
function setHeaderForResponse(res, name, value) {
|
||||
// Express API
|
||||
if (typeof
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.set === "function") {
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.set(name, typeof value === "number" ? String(value) : value);
|
||||
return;
|
||||
} // Node.js API
|
||||
|
||||
|
||||
res.setHeader(name, value);
|
||||
}
|
||||
/**
|
||||
* @template {ServerResponse} Response
|
||||
* @param {Response} res
|
||||
* @param {number} code
|
||||
*/
|
||||
|
||||
|
||||
function setStatusCode(res, code) {
|
||||
if (typeof
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.status === "function") {
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.status(code);
|
||||
return;
|
||||
} // eslint-disable-next-line no-param-reassign
|
||||
|
||||
|
||||
res.statusCode = code;
|
||||
}
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @template {ServerResponse} Response
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {string | Buffer | import("fs").ReadStream} bufferOtStream
|
||||
* @param {number} byteLength
|
||||
*/
|
||||
|
||||
|
||||
function send(req, res, bufferOtStream, byteLength) {
|
||||
if (typeof
|
||||
/** @type {import("fs").ReadStream} */
|
||||
bufferOtStream.pipe === "function") {
|
||||
setHeaderForResponse(res, "Content-Length", byteLength);
|
||||
|
||||
if (req.method === "HEAD") {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
/** @type {import("fs").ReadStream} */
|
||||
|
||||
|
||||
bufferOtStream.pipe(res);
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.send === "function") {
|
||||
/** @type {Response & ExpectedResponse} */
|
||||
res.send(bufferOtStream);
|
||||
return;
|
||||
} // Only Node.js API used
|
||||
|
||||
|
||||
res.setHeader("Content-Length", byteLength);
|
||||
|
||||
if (req.method === "HEAD") {
|
||||
res.end();
|
||||
} else {
|
||||
res.end(bufferOtStream);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getHeaderNames,
|
||||
getHeaderFromRequest,
|
||||
getHeaderFromResponse,
|
||||
setHeaderForResponse,
|
||||
setStatusCode,
|
||||
send
|
||||
};
|
137
node_modules/webpack-dev-middleware/dist/utils/getFilenameFromUrl.js
generated
vendored
Normal file
137
node_modules/webpack-dev-middleware/dist/utils/getFilenameFromUrl.js
generated
vendored
Normal file
@ -0,0 +1,137 @@
|
||||
"use strict";
|
||||
|
||||
const path = require("path");
|
||||
|
||||
const {
|
||||
parse
|
||||
} = require("url");
|
||||
|
||||
const querystring = require("querystring");
|
||||
|
||||
const getPaths = require("./getPaths");
|
||||
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
||||
|
||||
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
||||
|
||||
|
||||
const cacheStore = new WeakMap();
|
||||
/**
|
||||
* @param {Function} fn
|
||||
* @param {{ cache?: Map<any, any> }} [cache]
|
||||
* @returns {any}
|
||||
*/
|
||||
|
||||
const mem = (fn, {
|
||||
cache = new Map()
|
||||
} = {}) => {
|
||||
/**
|
||||
* @param {any} arguments_
|
||||
* @return {any}
|
||||
*/
|
||||
const memoized = (...arguments_) => {
|
||||
const [key] = arguments_;
|
||||
const cacheItem = cache.get(key);
|
||||
|
||||
if (cacheItem) {
|
||||
return cacheItem.data;
|
||||
}
|
||||
|
||||
const result = fn.apply(void 0, arguments_);
|
||||
cache.set(key, {
|
||||
data: result
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
cacheStore.set(memoized, cache);
|
||||
return memoized;
|
||||
};
|
||||
|
||||
const memoizedParse = mem(parse);
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @template {ServerResponse} Response
|
||||
* @param {import("../index.js").Context<Request, Response>} context
|
||||
* @param {string} url
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
|
||||
function getFilenameFromUrl(context, url) {
|
||||
const {
|
||||
options
|
||||
} = context;
|
||||
const paths = getPaths(context);
|
||||
let foundFilename;
|
||||
let urlObject;
|
||||
|
||||
try {
|
||||
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
|
||||
urlObject = memoizedParse(url, false, true);
|
||||
} catch (_ignoreError) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const {
|
||||
publicPath,
|
||||
outputPath
|
||||
} of paths) {
|
||||
let filename;
|
||||
let publicPathObject;
|
||||
|
||||
try {
|
||||
publicPathObject = memoizedParse(publicPath !== "auto" && publicPath ? publicPath : "/", false, true);
|
||||
} catch (_ignoreError) {
|
||||
// eslint-disable-next-line no-continue
|
||||
continue;
|
||||
}
|
||||
|
||||
if (urlObject.pathname && urlObject.pathname.startsWith(publicPathObject.pathname)) {
|
||||
filename = outputPath; // Strip the `pathname` property from the `publicPath` option from the start of requested url
|
||||
// `/complex/foo.js` => `foo.js`
|
||||
|
||||
const pathname = urlObject.pathname.slice(publicPathObject.pathname.length);
|
||||
|
||||
if (pathname) {
|
||||
filename = path.join(outputPath, querystring.unescape(pathname));
|
||||
}
|
||||
|
||||
let fsStats;
|
||||
|
||||
try {
|
||||
fsStats =
|
||||
/** @type {import("fs").statSync} */
|
||||
context.outputFileSystem.statSync(filename);
|
||||
} catch (_ignoreError) {
|
||||
// eslint-disable-next-line no-continue
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fsStats.isFile()) {
|
||||
foundFilename = filename;
|
||||
break;
|
||||
} else if (fsStats.isDirectory() && (typeof options.index === "undefined" || options.index)) {
|
||||
const indexValue = typeof options.index === "undefined" || typeof options.index === "boolean" ? "index.html" : options.index;
|
||||
filename = path.join(filename, indexValue);
|
||||
|
||||
try {
|
||||
fsStats =
|
||||
/** @type {import("fs").statSync} */
|
||||
context.outputFileSystem.statSync(filename);
|
||||
} catch (__ignoreError) {
|
||||
// eslint-disable-next-line no-continue
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fsStats.isFile()) {
|
||||
foundFilename = filename;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // eslint-disable-next-line consistent-return
|
||||
|
||||
|
||||
return foundFilename;
|
||||
}
|
||||
|
||||
module.exports = getFilenameFromUrl;
|
49
node_modules/webpack-dev-middleware/dist/utils/getPaths.js
generated
vendored
Normal file
49
node_modules/webpack-dev-middleware/dist/utils/getPaths.js
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("webpack").Compiler} Compiler */
|
||||
|
||||
/** @typedef {import("webpack").Stats} Stats */
|
||||
|
||||
/** @typedef {import("webpack").MultiStats} MultiStats */
|
||||
|
||||
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
||||
|
||||
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
||||
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @template {ServerResponse} Response
|
||||
* @param {import("../index.js").Context<Request, Response>} context
|
||||
*/
|
||||
function getPaths(context) {
|
||||
const {
|
||||
stats,
|
||||
options
|
||||
} = context;
|
||||
/** @type {Stats[]} */
|
||||
|
||||
const childStats =
|
||||
/** @type {MultiStats} */
|
||||
stats.stats ?
|
||||
/** @type {MultiStats} */
|
||||
stats.stats : [
|
||||
/** @type {Stats} */
|
||||
stats];
|
||||
const publicPaths = [];
|
||||
|
||||
for (const {
|
||||
compilation
|
||||
} of childStats) {
|
||||
// The `output.path` is always present and always absolute
|
||||
const outputPath = compilation.getPath(compilation.outputOptions.path || "");
|
||||
const publicPath = options.publicPath ? compilation.getPath(options.publicPath) : compilation.outputOptions.publicPath ? compilation.getPath(compilation.outputOptions.publicPath) : "";
|
||||
publicPaths.push({
|
||||
outputPath,
|
||||
publicPath
|
||||
});
|
||||
}
|
||||
|
||||
return publicPaths;
|
||||
}
|
||||
|
||||
module.exports = getPaths;
|
26
node_modules/webpack-dev-middleware/dist/utils/ready.js
generated
vendored
Normal file
26
node_modules/webpack-dev-middleware/dist/utils/ready.js
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
||||
|
||||
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
||||
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @template {ServerResponse} Response
|
||||
* @param {import("../index.js").Context<Request, Response>} context
|
||||
* @param {(...args: any[]) => any} callback
|
||||
* @param {Request} [req]
|
||||
* @returns {void}
|
||||
*/
|
||||
function ready(context, callback, req) {
|
||||
if (context.state) {
|
||||
callback(context.stats);
|
||||
return;
|
||||
}
|
||||
|
||||
const name = req && req.url || callback.name;
|
||||
context.logger.info(`wait until bundle finished${name ? `: ${name}` : ""}`);
|
||||
context.callbacks.push(callback);
|
||||
}
|
||||
|
||||
module.exports = ready;
|
216
node_modules/webpack-dev-middleware/dist/utils/setupHooks.js
generated
vendored
Normal file
216
node_modules/webpack-dev-middleware/dist/utils/setupHooks.js
generated
vendored
Normal file
@ -0,0 +1,216 @@
|
||||
"use strict";
|
||||
|
||||
const webpack = require("webpack");
|
||||
|
||||
const {
|
||||
isColorSupported
|
||||
} = require("colorette");
|
||||
/** @typedef {import("webpack").Configuration} Configuration */
|
||||
|
||||
/** @typedef {import("webpack").Compiler} Compiler */
|
||||
|
||||
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
|
||||
|
||||
/** @typedef {import("webpack").Stats} Stats */
|
||||
|
||||
/** @typedef {import("webpack").MultiStats} MultiStats */
|
||||
|
||||
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
||||
|
||||
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
||||
|
||||
/** @typedef {Configuration["stats"]} StatsOptions */
|
||||
|
||||
/** @typedef {{ children: Configuration["stats"][] }} MultiStatsOptions */
|
||||
|
||||
/** @typedef {Exclude<Configuration["stats"], boolean | string | undefined>} NormalizedStatsOptions */
|
||||
// TODO remove `color` after dropping webpack v4
|
||||
|
||||
/** @typedef {{ children: StatsOptions[], colors?: any }} MultiNormalizedStatsOptions */
|
||||
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @template {ServerResponse} Response
|
||||
* @param {import("../index.js").Context<Request, Response>} context
|
||||
*/
|
||||
|
||||
|
||||
function setupHooks(context) {
|
||||
function invalid() {
|
||||
if (context.state) {
|
||||
context.logger.log("Compilation starting...");
|
||||
} // We are now in invalid state
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
|
||||
|
||||
context.state = false; // eslint-disable-next-line no-param-reassign, no-undefined
|
||||
|
||||
context.stats = undefined;
|
||||
} // @ts-ignore
|
||||
|
||||
|
||||
const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;
|
||||
/**
|
||||
* @param {Configuration["stats"]} statsOptions
|
||||
* @returns {NormalizedStatsOptions}
|
||||
*/
|
||||
|
||||
function normalizeStatsOptions(statsOptions) {
|
||||
if (statsForWebpack4) {
|
||||
if (typeof statsOptions === "undefined") {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
statsOptions = {};
|
||||
} else if (typeof statsOptions === "boolean" || typeof statsOptions === "string") {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
statsOptions = webpack.Stats.presetToOptions(statsOptions);
|
||||
} // @ts-ignore
|
||||
|
||||
|
||||
return statsOptions;
|
||||
}
|
||||
|
||||
if (typeof statsOptions === "undefined") {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
statsOptions = {
|
||||
preset: "normal"
|
||||
};
|
||||
} else if (typeof statsOptions === "boolean") {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
statsOptions = statsOptions ? {
|
||||
preset: "normal"
|
||||
} : {
|
||||
preset: "none"
|
||||
};
|
||||
} else if (typeof statsOptions === "string") {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
statsOptions = {
|
||||
preset: statsOptions
|
||||
};
|
||||
}
|
||||
|
||||
return statsOptions;
|
||||
}
|
||||
/**
|
||||
* @param {Stats | MultiStats} stats
|
||||
*/
|
||||
|
||||
|
||||
function done(stats) {
|
||||
// We are now on valid state
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
context.state = true; // eslint-disable-next-line no-param-reassign
|
||||
|
||||
context.stats = stats; // Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
|
||||
|
||||
process.nextTick(() => {
|
||||
const {
|
||||
compiler,
|
||||
logger,
|
||||
options,
|
||||
state,
|
||||
callbacks
|
||||
} = context; // Check if still in valid state
|
||||
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log("Compilation finished");
|
||||
const isMultiCompilerMode = Boolean(
|
||||
/** @type {MultiCompiler} */
|
||||
compiler.compilers);
|
||||
/**
|
||||
* @type {StatsOptions | MultiStatsOptions | NormalizedStatsOptions | MultiNormalizedStatsOptions}
|
||||
*/
|
||||
|
||||
let statsOptions;
|
||||
|
||||
if (typeof options.stats !== "undefined") {
|
||||
statsOptions = isMultiCompilerMode ? {
|
||||
children:
|
||||
/** @type {MultiCompiler} */
|
||||
compiler.compilers.map(() => options.stats)
|
||||
} : options.stats;
|
||||
} else {
|
||||
statsOptions = isMultiCompilerMode ? {
|
||||
children:
|
||||
/** @type {MultiCompiler} */
|
||||
compiler.compilers.map(child => child.options.stats)
|
||||
} :
|
||||
/** @type {Compiler} */
|
||||
compiler.options.stats;
|
||||
}
|
||||
|
||||
if (isMultiCompilerMode) {
|
||||
/** @type {MultiNormalizedStatsOptions} */
|
||||
statsOptions.children =
|
||||
/** @type {MultiStatsOptions} */
|
||||
statsOptions.children.map(
|
||||
/**
|
||||
* @param {StatsOptions} childStatsOptions
|
||||
* @return {NormalizedStatsOptions}
|
||||
*/
|
||||
childStatsOptions => {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
childStatsOptions = normalizeStatsOptions(childStatsOptions);
|
||||
|
||||
if (typeof childStatsOptions.colors === "undefined") {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
childStatsOptions.colors = isColorSupported;
|
||||
}
|
||||
|
||||
return childStatsOptions;
|
||||
});
|
||||
} else {
|
||||
/** @type {NormalizedStatsOptions} */
|
||||
statsOptions = normalizeStatsOptions(
|
||||
/** @type {StatsOptions} */
|
||||
statsOptions);
|
||||
|
||||
if (typeof statsOptions.colors === "undefined") {
|
||||
statsOptions.colors = isColorSupported;
|
||||
}
|
||||
} // TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
|
||||
|
||||
|
||||
if (
|
||||
/** @type {MultiCompiler} */
|
||||
compiler.compilers && statsForWebpack4) {
|
||||
/** @type {MultiNormalizedStatsOptions} */
|
||||
statsOptions.colors =
|
||||
/** @type {MultiNormalizedStatsOptions} */
|
||||
statsOptions.children.some(
|
||||
/**
|
||||
* @param {StatsOptions} child
|
||||
*/
|
||||
// @ts-ignore
|
||||
child => child.colors);
|
||||
}
|
||||
|
||||
const printedStats = stats.toString(statsOptions); // Avoid extra empty line when `stats: 'none'`
|
||||
|
||||
if (printedStats) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(printedStats);
|
||||
} // eslint-disable-next-line no-param-reassign
|
||||
|
||||
|
||||
context.callbacks = []; // Execute callback that are delayed
|
||||
|
||||
callbacks.forEach(
|
||||
/**
|
||||
* @param {(...args: any[]) => Stats | MultiStats} callback
|
||||
*/
|
||||
callback => {
|
||||
callback(stats);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
context.compiler.hooks.watchRun.tap("webpack-dev-middleware", invalid);
|
||||
context.compiler.hooks.invalid.tap("webpack-dev-middleware", invalid);
|
||||
context.compiler.hooks.done.tap("webpack-dev-middleware", done);
|
||||
}
|
||||
|
||||
module.exports = setupHooks;
|
58
node_modules/webpack-dev-middleware/dist/utils/setupOutputFileSystem.js
generated
vendored
Normal file
58
node_modules/webpack-dev-middleware/dist/utils/setupOutputFileSystem.js
generated
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
"use strict";
|
||||
|
||||
const path = require("path");
|
||||
|
||||
const memfs = require("memfs");
|
||||
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
|
||||
|
||||
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
||||
|
||||
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
||||
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @template {ServerResponse} Response
|
||||
* @param {import("../index.js").Context<Request, Response>} context
|
||||
*/
|
||||
|
||||
|
||||
function setupOutputFileSystem(context) {
|
||||
let outputFileSystem;
|
||||
|
||||
if (context.options.outputFileSystem) {
|
||||
const {
|
||||
outputFileSystem: outputFileSystemFromOptions
|
||||
} = context.options; // Todo remove when we drop webpack@4 support
|
||||
|
||||
if (typeof outputFileSystemFromOptions.join !== "function") {
|
||||
throw new Error("Invalid options: options.outputFileSystem.join() method is expected");
|
||||
} // Todo remove when we drop webpack@4 support
|
||||
// @ts-ignore
|
||||
|
||||
|
||||
if (typeof outputFileSystemFromOptions.mkdirp !== "function") {
|
||||
throw new Error("Invalid options: options.outputFileSystem.mkdirp() method is expected");
|
||||
}
|
||||
|
||||
outputFileSystem = outputFileSystemFromOptions;
|
||||
} else {
|
||||
outputFileSystem = memfs.createFsFromVolume(new memfs.Volume()); // TODO: remove when we drop webpack@4 support
|
||||
// @ts-ignore
|
||||
|
||||
outputFileSystem.join = path.join.bind(path);
|
||||
}
|
||||
|
||||
const compilers =
|
||||
/** @type {MultiCompiler} */
|
||||
context.compiler.compilers || [context.compiler];
|
||||
|
||||
for (const compiler of compilers) {
|
||||
compiler.outputFileSystem = outputFileSystem;
|
||||
} // @ts-ignore
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
|
||||
|
||||
context.outputFileSystem = outputFileSystem;
|
||||
}
|
||||
|
||||
module.exports = setupOutputFileSystem;
|
111
node_modules/webpack-dev-middleware/dist/utils/setupWriteToDisk.js
generated
vendored
Normal file
111
node_modules/webpack-dev-middleware/dist/utils/setupWriteToDisk.js
generated
vendored
Normal file
@ -0,0 +1,111 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
|
||||
const path = require("path");
|
||||
/** @typedef {import("webpack").Compiler} Compiler */
|
||||
|
||||
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
|
||||
|
||||
/** @typedef {import("webpack").Compilation} Compilation */
|
||||
|
||||
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
||||
|
||||
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
||||
|
||||
/**
|
||||
* @template {IncomingMessage} Request
|
||||
* @template {ServerResponse} Response
|
||||
* @param {import("../index.js").Context<Request, Response>} context
|
||||
*/
|
||||
|
||||
|
||||
function setupWriteToDisk(context) {
|
||||
/**
|
||||
* @type {Compiler[]}
|
||||
*/
|
||||
const compilers =
|
||||
/** @type {MultiCompiler} */
|
||||
context.compiler.compilers || [context.compiler];
|
||||
|
||||
for (const compiler of compilers) {
|
||||
compiler.hooks.emit.tap("DevMiddleware",
|
||||
/**
|
||||
* @param {Compilation} compilation
|
||||
*/
|
||||
compilation => {
|
||||
// @ts-ignore
|
||||
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
|
||||
return;
|
||||
}
|
||||
|
||||
compiler.hooks.assetEmitted.tapAsync("DevMiddleware", (file, info, callback) => {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
let targetPath;
|
||||
/**
|
||||
* @type {Buffer}
|
||||
*/
|
||||
|
||||
let content; // webpack@5
|
||||
|
||||
if (info.compilation) {
|
||||
({
|
||||
targetPath,
|
||||
content
|
||||
} = info);
|
||||
} else {
|
||||
let targetFile = file;
|
||||
const queryStringIdx = targetFile.indexOf("?");
|
||||
|
||||
if (queryStringIdx >= 0) {
|
||||
targetFile = targetFile.slice(0, queryStringIdx);
|
||||
}
|
||||
|
||||
let {
|
||||
outputPath
|
||||
} = compiler;
|
||||
outputPath = compilation.getPath(outputPath, {}); // @ts-ignore
|
||||
|
||||
content = info;
|
||||
targetPath = path.join(outputPath, targetFile);
|
||||
}
|
||||
|
||||
const {
|
||||
writeToDisk: filter
|
||||
} = context.options;
|
||||
const allowWrite = filter && typeof filter === "function" ? filter(targetPath) : true;
|
||||
|
||||
if (!allowWrite) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
const dir = path.dirname(targetPath);
|
||||
const name = compiler.options.name ? `Child "${compiler.options.name}": ` : "";
|
||||
return fs.mkdir(dir, {
|
||||
recursive: true
|
||||
}, mkdirError => {
|
||||
if (mkdirError) {
|
||||
context.logger.error(`${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`);
|
||||
return callback(mkdirError);
|
||||
}
|
||||
|
||||
return fs.writeFile(targetPath, content, writeFileError => {
|
||||
if (writeFileError) {
|
||||
context.logger.error(`${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`);
|
||||
return callback(writeFileError);
|
||||
}
|
||||
|
||||
context.logger.log(`${name}Asset written to disk: "${targetPath}"`);
|
||||
return callback();
|
||||
});
|
||||
});
|
||||
}); // @ts-ignore
|
||||
|
||||
compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = setupWriteToDisk;
|
Reference in New Issue
Block a user