1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = getFilenameFromUrl;
|
---|
7 |
|
---|
8 | var _path = _interopRequireDefault(require("path"));
|
---|
9 |
|
---|
10 | var _url = require("url");
|
---|
11 |
|
---|
12 | var _querystring = _interopRequireDefault(require("querystring"));
|
---|
13 |
|
---|
14 | var _mem = _interopRequireDefault(require("mem"));
|
---|
15 |
|
---|
16 | var _getPaths = _interopRequireDefault(require("./getPaths"));
|
---|
17 |
|
---|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
19 |
|
---|
20 | const memoizedParse = (0, _mem.default)(_url.parse);
|
---|
21 |
|
---|
22 | function getFilenameFromUrl(context, url) {
|
---|
23 | const {
|
---|
24 | options
|
---|
25 | } = context;
|
---|
26 | const paths = (0, _getPaths.default)(context);
|
---|
27 | let foundFilename;
|
---|
28 | let urlObject;
|
---|
29 |
|
---|
30 | try {
|
---|
31 | // The `url` property of the `request` is contains only `pathname`, `search` and `hash`
|
---|
32 | urlObject = memoizedParse(url, false, true);
|
---|
33 | } catch (_ignoreError) {
|
---|
34 | return;
|
---|
35 | }
|
---|
36 |
|
---|
37 | for (const {
|
---|
38 | publicPath,
|
---|
39 | outputPath
|
---|
40 | } of paths) {
|
---|
41 | let filename;
|
---|
42 | let publicPathObject;
|
---|
43 |
|
---|
44 | try {
|
---|
45 | publicPathObject = memoizedParse(publicPath !== "auto" && publicPath ? publicPath : "/", false, true);
|
---|
46 | } catch (_ignoreError) {
|
---|
47 | // eslint-disable-next-line no-continue
|
---|
48 | continue;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (urlObject.pathname && urlObject.pathname.startsWith(publicPathObject.pathname)) {
|
---|
52 | filename = outputPath; // Strip the `pathname` property from the `publicPath` option from the start of requested url
|
---|
53 | // `/complex/foo.js` => `foo.js`
|
---|
54 |
|
---|
55 | const pathname = urlObject.pathname.substr(publicPathObject.pathname.length);
|
---|
56 |
|
---|
57 | if (pathname) {
|
---|
58 | filename = _path.default.join(outputPath, _querystring.default.unescape(pathname));
|
---|
59 | }
|
---|
60 |
|
---|
61 | let fsStats;
|
---|
62 |
|
---|
63 | try {
|
---|
64 | fsStats = context.outputFileSystem.statSync(filename);
|
---|
65 | } catch (_ignoreError) {
|
---|
66 | // eslint-disable-next-line no-continue
|
---|
67 | continue;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (fsStats.isFile()) {
|
---|
71 | foundFilename = filename;
|
---|
72 | break;
|
---|
73 | } else if (fsStats.isDirectory() && (typeof options.index === "undefined" || options.index)) {
|
---|
74 | const indexValue = typeof options.index === "undefined" || typeof options.index === "boolean" ? "index.html" : options.index;
|
---|
75 | filename = _path.default.join(filename, indexValue);
|
---|
76 |
|
---|
77 | try {
|
---|
78 | fsStats = context.outputFileSystem.statSync(filename);
|
---|
79 | } catch (__ignoreError) {
|
---|
80 | // eslint-disable-next-line no-continue
|
---|
81 | continue;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (fsStats.isFile()) {
|
---|
85 | foundFilename = filename;
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | } // eslint-disable-next-line consistent-return
|
---|
91 |
|
---|
92 |
|
---|
93 | return foundFilename;
|
---|
94 | } |
---|