shlink-web-client/config/paths.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-09-06 10:32:16 +03:00
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/promise-function-async, @typescript-eslint/prefer-optional-chain */
2018-05-13 09:57:47 +03:00
const path = require('path');
const fs = require('fs');
const url = require('url');
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebook/create-react-app/issues/637
2018-05-13 09:57:47 +03:00
const appDirectory = fs.realpathSync(process.cwd());
2019-01-06 01:16:13 +03:00
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
2018-05-13 09:57:47 +03:00
const envPublicUrl = process.env.PUBLIC_URL;
function ensureSlash(inputPath, needsSlash) {
const hasSlash = inputPath.endsWith('/');
2019-01-06 01:16:13 +03:00
2018-05-13 09:57:47 +03:00
if (hasSlash && !needsSlash) {
return inputPath.substr(0, inputPath.length - 1);
2018-05-13 09:57:47 +03:00
} else if (!hasSlash && needsSlash) {
return `${inputPath}/`;
2018-05-13 09:57:47 +03:00
}
2019-01-06 01:16:13 +03:00
return inputPath;
2018-05-13 09:57:47 +03:00
}
2019-01-06 01:16:13 +03:00
const getPublicUrl = (appPackageJson) =>
2018-05-13 09:57:47 +03:00
envPublicUrl || require(appPackageJson).homepage;
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
function getServedPath(appPackageJson) {
const publicUrl = getPublicUrl(appPackageJson);
const servedUrl =
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
2019-01-06 01:16:13 +03:00
2018-05-13 09:57:47 +03:00
return ensureSlash(servedUrl, true);
}
const moduleFileExtensions = [
'web.mjs',
'mjs',
'web.js',
'js',
'web.ts',
'ts',
'web.tsx',
'tsx',
'json',
'web.jsx',
'jsx',
];
// Resolve file paths in the same order as webpack
const resolveModule = (resolveFn, filePath) => {
2019-01-06 01:16:13 +03:00
const extension = moduleFileExtensions.find((extension) =>
fs.existsSync(resolveFn(`${filePath}.${extension}`)));
if (extension) {
return resolveFn(`${filePath}.${extension}`);
}
return resolveFn(`${filePath}.js`);
};
2018-05-13 09:57:47 +03:00
// config after eject: we're in ./config/
module.exports = {
dotenv: resolveApp('.env'),
appPath: resolveApp('.'),
2018-05-13 09:57:47 +03:00
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
appIndexJs: resolveModule(resolveApp, 'src/index'),
2018-05-13 09:57:47 +03:00
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
appTsConfig: resolveApp('tsconfig.json'),
2018-05-13 09:57:47 +03:00
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
proxySetup: resolveApp('src/setupProxy.js'),
2018-05-13 09:57:47 +03:00
appNodeModules: resolveApp('node_modules'),
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json')),
swSrc: resolveModule(resolveApp, 'src/service-worker'),
2018-05-13 09:57:47 +03:00
};
module.exports.moduleFileExtensions = moduleFileExtensions;