@wii/fastify-webpack
Version:
fastify plugin for webpack
151 lines (149 loc) • 5.18 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getPaths = exports.getFilenameFromUrl = exports.formatServerEvent = exports.defer = exports.createSyncEvents = exports.canRead = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.promise.js");
var _nodeUrl = require("node:url");
var _nodePath = _interopRequireDefault(require("node:path"));
var _nodeQuerystring = _interopRequireDefault(require("node:querystring"));
const extractBundles = stats => {
if (stats.modules) {
return [stats];
}
if (stats.children) {
return stats.children;
}
throw new Error('Unexpected state.');
};
const buildModuleMap = (modules = []) => {
const map = {};
for (const module of modules) {
var _module$id;
map[(_module$id = module.id) !== null && _module$id !== void 0 ? _module$id : module.name] = module.name;
}
return map;
};
const createSyncEvents = stats => {
const syncEvents = [];
const bundles = extractBundles(stats.toJson({
all: false,
assets: true,
cached: true,
children: true,
hash: true,
modules: true,
timings: true
}));
for (const bundleStats of bundles) {
var _bundleStats$assets, _bundleStats$errors, _bundleStats$warnings;
if (!bundleStats.hash) {
throw new Error('hash is undefined');
}
syncEvents.push({
assets: (_bundleStats$assets = bundleStats.assets) !== null && _bundleStats$assets !== void 0 ? _bundleStats$assets : [],
errors: (_bundleStats$errors = bundleStats.errors) !== null && _bundleStats$errors !== void 0 ? _bundleStats$errors : [],
hash: bundleStats.hash,
modules: buildModuleMap(bundleStats.modules),
warnings: (_bundleStats$warnings = bundleStats.warnings) !== null && _bundleStats$warnings !== void 0 ? _bundleStats$warnings : []
});
}
return syncEvents;
};
exports.createSyncEvents = createSyncEvents;
const formatServerEvent = (name, data) => {
return 'event: ' + name + '\ndata: ' + JSON.stringify(data) + '\n\n';
};
exports.formatServerEvent = formatServerEvent;
const canRead = async (fs, path) => {
try {
await fs.promises.access(path, fs.constants.R_OK);
return true;
} catch (_unused) {
return false;
}
};
exports.canRead = canRead;
const getPaths = stats => {
var _compilation$outputOp;
const {
compilation
} = stats;
const outputPath = compilation.getPath((_compilation$outputOp = compilation.outputOptions.path) !== null && _compilation$outputOp !== void 0 ? _compilation$outputOp : '');
const publicPath = compilation.outputOptions.publicPath ? compilation.getPath(compilation.outputOptions.publicPath) : '';
return {
outputPath,
publicPath
};
};
exports.getPaths = getPaths;
const getFilenameFromUrl = async (fastify, outputFileSystem, stats, url) => {
let foundFilename = null;
try {
var _urlObject$pathname;
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
const urlObject = (0, _nodeUrl.parse)(url, false, true);
const {
publicPath,
outputPath
} = getPaths(stats);
let filename;
let publicPathObject;
try {
publicPathObject = (0, _nodeUrl.parse)(publicPath !== 'auto' && publicPath ? publicPath : '/', false, true);
} catch (error) {
fastify.log.trace(error, 'could not parse URL');
return null;
}
if (publicPathObject.pathname && (_urlObject$pathname = urlObject.pathname) !== null && _urlObject$pathname !== void 0 && _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 = _nodePath.default.join(outputPath, _nodeQuerystring.default.unescape(pathname));
}
let fsStats;
try {
fsStats = await outputFileSystem.promises.stat(filename);
} catch (error) {
if (error.message.includes('no such file or directory')) {
fastify.log.trace('no such file or directory %s', filename);
} else {
fastify.log.trace(error, 'could not stat path');
}
return null;
}
if (fsStats.isFile()) {
foundFilename = filename;
}
}
return foundFilename;
} catch (error) {
fastify.log.trace(error, 'could not parse URL');
return null;
}
};
exports.getFilenameFromUrl = getFilenameFromUrl;
const defer = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const deferred = {
rejected: false,
resolved: false
};
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = value => {
deferred.resolved = true;
resolve(value);
};
deferred.reject = error => {
deferred.rejected = false;
reject(error);
};
});
return deferred;
};
exports.defer = defer;