Skip to content

Commit 6cbff48

Browse files
committed
3.4.0 Add getPublicUrlOrPath.js
1 parent 71a7611 commit 6cbff48

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
"use strict";
9+
10+
const { URL } = require("url");
11+
12+
module.exports = getPublicUrlOrPath;
13+
14+
/**
15+
* Returns a URL or a path with slash at the end
16+
* In production can be URL, abolute path, relative path
17+
* In development always will be an absolute path
18+
* In development can use `path` module functions for operations
19+
*
20+
* @param {boolean} craIsEnvDevelopment; unused
21+
* @param {(string|undefined)} homepage a valid url or pathname
22+
* @param {(string|undefined)} envPublicUrl a valid url or pathname
23+
* @returns {string}
24+
*/
25+
function getPublicUrlOrPath(craIsEnvDevelopment, homepage, envPublicUrl) {
26+
const isEnvDevelopment = false; // create-react-wptheme always uses PROD paths.
27+
const stubDomain = "https://create-react-wptheme.dev";
28+
29+
if (envPublicUrl) {
30+
// ensure last slash exists
31+
envPublicUrl = envPublicUrl.endsWith("/") ? envPublicUrl : envPublicUrl + "/";
32+
33+
// validate if `envPublicUrl` is a URL or path like
34+
// `stubDomain` is ignored if `envPublicUrl` contains a domain
35+
const validPublicUrl = new URL(envPublicUrl, stubDomain);
36+
37+
return isEnvDevelopment
38+
? envPublicUrl.startsWith(".")
39+
? "/"
40+
: validPublicUrl.pathname
41+
: // Some apps do not use client-side routing with pushState.
42+
// For these, "homepage" can be set to "." to enable relative asset paths.
43+
envPublicUrl;
44+
}
45+
46+
if (homepage) {
47+
// strip last slash if exists
48+
homepage = homepage.endsWith("/") ? homepage : homepage + "/";
49+
50+
// validate if `homepage` is a URL or path like and use just pathname
51+
const validHomepagePathname = new URL(homepage, stubDomain).pathname;
52+
return isEnvDevelopment
53+
? homepage.startsWith(".")
54+
? "/"
55+
: validHomepagePathname
56+
: // Some apps do not use client-side routing with pushState.
57+
// For these, "homepage" can be set to "." to enable relative asset paths.
58+
homepage.startsWith(".")
59+
? homepage
60+
: validHomepagePathname;
61+
}
62+
63+
return "/";
64+
}

packages/create-react-wptheme-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"files": [
2121
"fileFunctions.js",
2222
"fileWatcherPlugin.js",
23+
"getPublicUrlOrPath.js",
2324
"getUserConfig.js",
2425
"postInstallerInfo.js",
2526
"shell-js.js",

0 commit comments

Comments
 (0)