123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- "use strict";
- const path = require("path");
- const {
- parse
- } = require("url");
- const querystring = require("querystring");
- const getPaths = require("./getPaths");
- const cacheStore = new WeakMap();
- const mem = (fn, {
- cache = new Map()
- } = {}) => {
-
- 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);
- function getFilenameFromUrl(context, url) {
- const {
- options
- } = context;
- const paths = getPaths(context);
- let foundFilename;
- let urlObject;
- try {
-
- 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) {
-
- continue;
- }
- if (urlObject.pathname && urlObject.pathname.startsWith(publicPathObject.pathname)) {
- filename = outputPath;
-
- const pathname = urlObject.pathname.slice(publicPathObject.pathname.length);
- if (pathname) {
- filename = path.join(outputPath, querystring.unescape(pathname));
- }
- let fsStats;
- try {
- fsStats =
-
- context.outputFileSystem.statSync(filename);
- } catch (_ignoreError) {
-
- 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 =
-
- context.outputFileSystem.statSync(filename);
- } catch (__ignoreError) {
-
- continue;
- }
- if (fsStats.isFile()) {
- foundFilename = filename;
- break;
- }
- }
- }
- }
- return foundFilename;
- }
- module.exports = getFilenameFromUrl;
|