simple-keyboard/config/paths.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-04-21 04:34:02 +08:00
'use strict';
const path = require('path');
const fs = require('fs');
const url = require('url');
// Make sure any symlinks in the project folder are resolved:
2018-11-10 00:06:46 +08:00
// https://github.com/facebook/create-react-app/issues/637
2018-04-21 04:34:02 +08:00
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const envPublicUrl = process.env.PUBLIC_URL;
2018-11-10 00:06:46 +08:00
function ensureSlash(inputPath, needsSlash) {
const hasSlash = inputPath.endsWith('/');
2018-04-21 04:34:02 +08:00
if (hasSlash && !needsSlash) {
2018-11-10 00:06:46 +08:00
return inputPath.substr(0, inputPath.length - 1);
2018-04-21 04:34:02 +08:00
} else if (!hasSlash && needsSlash) {
2018-11-10 00:06:46 +08:00
return `${inputPath}/`;
2018-04-21 04:34:02 +08:00
} else {
2018-11-10 00:06:46 +08:00
return inputPath;
2018-04-21 04:34:02 +08:00
}
}
const getPublicUrl = appPackageJson =>
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 : '/');
return ensureSlash(servedUrl, true);
}
2018-11-10 00:06:46 +08:00
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) => {
const extension = moduleFileExtensions.find(extension =>
fs.existsSync(resolveFn(`${filePath}.${extension}`))
);
if (extension) {
return resolveFn(`${filePath}.${extension}`);
}
return resolveFn(`${filePath}.js`);
};
2018-04-21 04:34:02 +08:00
// config after eject: we're in ./config/
module.exports = {
dotenv: resolveApp('.env'),
2018-11-10 00:06:46 +08:00
appPath: resolveApp('.'),
2018-04-21 04:34:02 +08:00
appBuild: resolveApp('build'),
2018-11-10 00:06:46 +08:00
appDemo: resolveApp('demo'),
2018-04-21 04:34:02 +08:00
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
2018-11-10 00:06:46 +08:00
appIndexJs: resolveModule(resolveApp, 'src/demo/index'),
appLibIndexJs: resolveModule(resolveApp, 'src/lib/index'),
appDemoIndexJs: resolveModule(resolveApp, 'src/demo/index'),
2018-04-21 04:34:02 +08:00
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
2018-11-10 00:06:46 +08:00
appSrcLib: resolveApp('src/lib'),
appSrcLibTypes: resolveApp('src/lib/@types'),
2018-11-10 00:06:46 +08:00
appSrcDemo: resolveApp('src/demo'),
appTsConfig: resolveApp('tsconfig.json'),
2019-04-28 10:19:34 +08:00
appJsConfig: resolveApp('jsconfig.json'),
2018-04-21 04:34:02 +08:00
yarnLockFile: resolveApp('yarn.lock'),
2018-11-10 00:06:46 +08:00
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
proxySetup: resolveApp('src/setupProxy.js'),
2018-04-21 04:34:02 +08:00
appNodeModules: resolveApp('node_modules'),
publicUrl: getPublicUrl(resolveApp('package.json')),
2018-11-10 00:06:46 +08:00
//servedPath: getServedPath(resolveApp('package.json')),
servedPath: ''
2018-04-21 04:34:02 +08:00
};
2018-11-10 00:06:46 +08:00
module.exports.moduleFileExtensions = moduleFileExtensions;