-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
37 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { cwd } from "../../utils/runtimeChecks.js"; | ||
import printWarning from "../../utils/printWarning.js"; | ||
|
||
// To strip off params when checking for file on disk. | ||
const paramPattern = /\?.*/ | ||
|
||
/** | ||
* getSrcPath allows the use of `src` attributes relative to either the /public folder or project root. | ||
* | ||
* It first checks to see if the src is a file relative to the cwd (project root). | ||
* If the file isn't found, it will look in the /public folder. | ||
* Finally, if it still can't be found, the original input will be returned. | ||
*/ | ||
export function getSrcPath(src) { | ||
// If this is already resolved to a file, return it. | ||
if (fs.existsSync(src.replace(paramPattern, ''))) return src; | ||
|
||
const rootPath = path.join(cwd, src); | ||
const rootTest = rootPath.replace(paramPattern, '') | ||
if (fs.existsSync(rootTest)) return rootPath; | ||
|
||
const publicPath = path.join(cwd, 'public', src); | ||
const publicTest = publicPath.replace(paramPattern, '') | ||
if (fs.existsSync(publicTest)) return publicPath; | ||
|
||
// Fallback | ||
printWarning({message: `"${src}" could not not be found at \n${rootTest} \n\tor \n${publicTest}`}); | ||
return src; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters