@omnia/tooling-vue
Version:
Used to bundle and serve manifests web component that build on Vue framework.
223 lines (222 loc) • 8.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveTsPath = exports.resolveModulePath = exports.modify = exports.parsePhysicalPath = exports.parseHmrDynamicBundlePath = exports.parseHmrEsmPath = exports.parseOmniaBundlePath = exports.isImageFilePath = exports.isPublicDirPath = exports.isNodeModulePath = exports.isTsPath = exports.isJavaScriptSuperset = exports.isSourceCodePath = exports.isEsmPath = exports.isBundlePath = exports.WORKER_IDENTIFIER = exports.HMR_DYNAMIC_BUNDLE_REQUEST = exports.HMR_ESM_REQUEST = exports.OMNIA_BUNDLE_REQUEST = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const vite_1 = require("vite");
const globby_1 = require("globby");
const envUtils = tslib_1.__importStar(require("./env"));
exports.OMNIA_BUNDLE_REQUEST = '/omnia/bundles';
exports.HMR_ESM_REQUEST = '/hmr/esm';
exports.HMR_DYNAMIC_BUNDLE_REQUEST = '/hmr/dynamicbundles';
exports.WORKER_IDENTIFIER = 'omniaworker';
let _tsConfig;
function getTsConfig() {
if (!_tsConfig) {
const value = require(envUtils.getTsConfigPath());
const resolvedBaseUrl = path_1.default.join(envUtils.getRootDirPath(), value.compilerOptions.baseUrl);
value.compilerOptions.baseUrl = (0, vite_1.normalizePath)(resolvedBaseUrl.replace(envUtils.getRootDirPath(), ''));
_tsConfig = value;
}
return _tsConfig;
}
function isBundlePath(value) {
return ensureRelativePath(value).startsWith(exports.OMNIA_BUNDLE_REQUEST);
}
exports.isBundlePath = isBundlePath;
function isEsmPath(value) {
return ensureRelativePath(value).startsWith(exports.HMR_ESM_REQUEST);
}
exports.isEsmPath = isEsmPath;
function isSourceCodePath(value) {
const relativePath = ensureRelativePath(value);
const baseUrl = getTsConfig().compilerOptions.baseUrl;
if (!baseUrl) {
return isNodeModulePath(value) == false && isPublicDirPath(value) == false;
}
return relativePath.startsWith(baseUrl) || relativePath.startsWith('.' + baseUrl);
}
exports.isSourceCodePath = isSourceCodePath;
function isJavaScriptSuperset(value) {
return value.endsWith('.ts') || value.endsWith('.tsx');
}
exports.isJavaScriptSuperset = isJavaScriptSuperset;
function isTsPath(importPath) {
const configBaseUrl = getTsConfig().compilerOptions.baseUrl;
const hasBaseUrlImport = !!configBaseUrl ? !!resolveModulePath((0, vite_1.normalizePath)(path_1.default.join(envUtils.getRootDirPath(), configBaseUrl, importPath))) : false;
return hasBaseUrlImport || Object.keys(getTsConfig().compilerOptions.paths || {}).some(p => importPath.startsWith(p.replace('/*', '')));
}
exports.isTsPath = isTsPath;
function isNodeModulePath(value) {
const relativePath = ensureRelativePath(value);
return relativePath.startsWith('/node_modules') || relativePath.startsWith('./node_modules');
}
exports.isNodeModulePath = isNodeModulePath;
function isPublicDirPath(value) {
const relativePath = ensureRelativePath(value);
return relativePath.startsWith('/wwwroot') || relativePath.startsWith('./wwwroot');
}
exports.isPublicDirPath = isPublicDirPath;
function isImageFilePath(value) {
return value.endsWith('.svg');
}
exports.isImageFilePath = isImageFilePath;
function parseOmniaBundlePath(value) {
value = ensureRelativePath(value).replace(exports.OMNIA_BUNDLE_REQUEST, '');
if (value.startsWith('/')) {
value = value.substr(1);
}
const [pathName, queryParams] = value.split('?');
const [resourceId, bundleFileExtension] = pathName.split('/');
const fromWorker = queryParams ? queryParams.split('&').some(q => q.startsWith(exports.WORKER_IDENTIFIER)) : false;
return {
resourceId,
fromWorker,
bundleFileExtension
};
}
exports.parseOmniaBundlePath = parseOmniaBundlePath;
function parseHmrEsmPath(value) {
value = ensureRelativePath(value).replace(exports.HMR_ESM_REQUEST, '');
if (value.startsWith('/')) {
value = value.substr(1);
}
const [pathName, queryParams] = value.split('?');
const [resourceId] = pathName.split('/');
const fromWorker = queryParams ? queryParams.split('&').some(q => q.startsWith(exports.WORKER_IDENTIFIER)) : false;
return {
resourceId,
fromWorker
};
}
exports.parseHmrEsmPath = parseHmrEsmPath;
function parseHmrDynamicBundlePath(value) {
value = ensureRelativePath(value).replace(exports.HMR_DYNAMIC_BUNDLE_REQUEST, '');
if (value.startsWith('/')) {
value = value.substr(1);
}
const [pathName, queryParams] = value.split('?');
const [resourceId] = pathName.split('/');
const fromWorker = queryParams ? queryParams.split('&').some(q => q.startsWith(exports.WORKER_IDENTIFIER)) : false;
return {
resourceId,
fromWorker
};
}
exports.parseHmrDynamicBundlePath = parseHmrDynamicBundlePath;
function parsePhysicalPath(value) {
value = ensureRelativePath(value).replace(envUtils.getRootDirPath(), '');
if (value.startsWith('/')) {
value = value.substr(1);
}
const [pathname, queryParams] = value.split('?');
const fromWorker = queryParams ? queryParams.split('&').some(q => q.startsWith(exports.WORKER_IDENTIFIER)) : false;
return {
pathname,
fromWorker
};
}
exports.parsePhysicalPath = parsePhysicalPath;
function modify(value) {
const builder = {
appendWorkerIdentifier(checker) {
if (checker) {
value += `${value.includes('?') ? '&' : '?'}${exports.WORKER_IDENTIFIER}`;
}
return builder;
},
appendTimestamp(timestamp) {
if (timestamp) {
value += `${value.includes('?') ? '&' : '?'}t=${timestamp}`;
}
return builder;
},
getValue() {
return value;
}
};
return builder;
}
exports.modify = modify;
function resolveModulePath(modulePath, external) {
let pattern = null;
if (external) {
pattern = [modulePath + '.js', modulePath + '/index.js', modulePath + '.ts', modulePath + '.tsx', modulePath + '/index.ts'];
}
else if (modulePath.endsWith('.css')) {
pattern = [modulePath + '.ts', modulePath];
}
else {
pattern = [modulePath + '.ts', modulePath + '.tsx', modulePath + '/index.ts'];
}
const result = (0, globby_1.sync)(pattern);
if (result.length == 1) {
return result[0];
}
if (result.length > 1) {
if (modulePath.endsWith('.css')) {
// priority typestyle
let filePath = result.find(p => p.endsWith('.css.ts'));
// then raw css
if (!filePath) {
filePath = result.find(p => p.endsWith('.css'));
}
if (filePath) {
return filePath;
}
}
else {
// priority tsx or ts files for components that may have same name for both tsx and styling
let filePath = result.find(p => p.endsWith('.tsx'));
if (!filePath) {
filePath = result.find(p => p.endsWith('.ts'));
}
if (filePath) {
return filePath;
}
}
console.error('Unknown module path: ' + result);
}
}
exports.resolveModulePath = resolveModulePath;
function resolveTsPath(importPath) {
const configBaseUrl = getTsConfig().compilerOptions.baseUrl;
const configPaths = getTsConfig().compilerOptions.paths;
if (!configBaseUrl) {
return;
}
const baseUrl = path_1.default.join(envUtils.getRootDirPath(), configBaseUrl);
const basePath = resolveModulePath((0, vite_1.normalizePath)(path_1.default.join(baseUrl, importPath)));
if (!!basePath) {
return basePath;
}
if (!configPaths) {
return;
}
const modulePathMapping = Object.keys(configPaths).filter(p => importPath.startsWith(p.replace('/*', ''))).sort((a, b) => b.length - a.length)[0];
const moduleName = modulePathMapping.includes('*') ? importPath.replace(modulePathMapping.replace('*', ''), '') : null;
const pathMappings = configPaths[modulePathMapping].slice();
const loop = () => {
let modulePath = null;
if (pathMappings.length > 0) {
const currentPathMapping = pathMappings.shift();
modulePath = (0, vite_1.normalizePath)(path_1.default.join(baseUrl, moduleName ? currentPathMapping.replace('*', moduleName) : currentPathMapping));
}
else {
return;
}
const filePath = resolveModulePath(modulePath);
if (filePath) {
return filePath;
}
return loop();
};
return loop();
}
exports.resolveTsPath = resolveTsPath;
function ensureRelativePath(value) {
if (path_1.default.isAbsolute(value)) {
value = value.replace(envUtils.getRootDirPath(), '/');
}
return value;
}