liferay-npm-bundler-preset-reactjs
Version:
liferay npm bundler preset react
146 lines (145 loc) • 6.31 kB
JavaScript
"use strict";
/**
* SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getModuleName = exports.getPackageDir = exports.getPackageJsonPath = void 0;
const path_1 = __importDefault(require("path"));
const file_path_1 = __importDefault(require("./file-path"));
const project_1 = __importDefault(require("./project"));
var FileOrigin;
(function (FileOrigin) {
FileOrigin[FileOrigin["SOURCE_PROJECT"] = 0] = "SOURCE_PROJECT";
FileOrigin[FileOrigin["SOURCE_DEPENDENCY"] = 1] = "SOURCE_DEPENDENCY";
FileOrigin[FileOrigin["BUILD_PROJECT"] = 2] = "BUILD_PROJECT";
FileOrigin[FileOrigin["BUILD_DEPENDENCY"] = 3] = "BUILD_DEPENDENCY";
})(FileOrigin || (FileOrigin = {}));
const fileOriginCache = {};
const packageDirCache = {};
/**
* Get the full path of the package.json file for a given JS module file.
*
* @remarks
* This method assumes that you are inside the Babel transform phase of the
* bundler. Otherwise, the return path may be incorrect.
*
* @param moduleFilePath the module file path
* @return the full path to the package.json file (with native path
* separators)
*/
function getPackageJsonPath(moduleFilePath) {
return path_1.default.join(getPackageDir(moduleFilePath), 'package.json');
}
exports.getPackageJsonPath = getPackageJsonPath;
/**
* Get the full path of the package directory for a given JS module file.
*
* @remarks
* This method assumes that the module file lives in a liferay-npm-bundler
* project. Otherwise, the return path may be incorrect.
*
* @param moduleFilePath the module file path
* @return the full path to the package directory (with native path
* separators)
*/
function getPackageDir(moduleFilePath) {
const absModuleFilePosixPath = new file_path_1.default(moduleFilePath).resolve()
.asPosix;
let absPkgDir = packageDirCache[absModuleFilePosixPath];
if (absPkgDir) {
return absPkgDir.asNative;
}
switch (getFileOrigin(moduleFilePath)) {
case FileOrigin.SOURCE_DEPENDENCY: {
const absModuleFilePosixPath = new file_path_1.default(moduleFilePath).resolve().asPosix;
const absNodeModulesPosixPath = project_1.default.dir.join('node_modules')
.asPosix;
const relModuleFilePosixPath = absModuleFilePosixPath.substring(absNodeModulesPosixPath.length + 1);
const modulePathParts = relModuleFilePosixPath.split('/');
const pkgDir = relModuleFilePosixPath.startsWith('@')
? `${modulePathParts[0]}/${modulePathParts[1]}`
: modulePathParts[0];
absPkgDir = project_1.default.dir.join('node_modules', pkgDir);
break;
}
case FileOrigin.BUILD_DEPENDENCY: {
const absModuleFilePosixPath = new file_path_1.default(moduleFilePath).resolve().asPosix;
const absBuildNodeModulesPosixPath = project_1.default.dir.join(project_1.default.buildDir, 'node_modules').asPosix;
const relBuildNodeModulesPosixPath = absModuleFilePosixPath.substring(absBuildNodeModulesPosixPath.length + 1);
const modulePathParts = relBuildNodeModulesPosixPath.split('/');
const pkgDir = modulePathParts[0];
absPkgDir = project_1.default.dir.join(project_1.default.buildDir, 'node_modules', pkgDir);
break;
}
case FileOrigin.SOURCE_PROJECT: {
absPkgDir = project_1.default.dir;
break;
}
case FileOrigin.BUILD_PROJECT: {
absPkgDir = project_1.default.dir.join(project_1.default.buildDir);
break;
}
default:
break;
}
packageDirCache[absModuleFilePosixPath] = absPkgDir;
return absPkgDir.asNative;
}
exports.getPackageDir = getPackageDir;
/**
* Get the name of a module given its file path.
*
* @remarks
* This method assumes that the module file lives in a liferay-npm-bundler
* project. Otherwise, the return path may be incorrect.
*
* @param moduleFilePath the module file path
* @return the name of the module
*/
function getModuleName(moduleFilePath) {
const absModuleFilePosixPath = new file_path_1.default(moduleFilePath).resolve()
.asPosix;
const absPkgPosixPath = new file_path_1.default(getPackageDir(moduleFilePath)).asPosix;
let moduleName = absModuleFilePosixPath.substring(absPkgPosixPath.length + 1);
moduleName = moduleName.replace(/\.js$/i, '');
if (getFileOrigin(moduleFilePath) === FileOrigin.SOURCE_PROJECT) {
for (const source of project_1.default.sources) {
const prefix = source.asPosix.substr(2);
if (moduleName.startsWith(prefix)) {
moduleName = moduleName.substring(prefix.length + 1);
break;
}
}
}
const pkgJson = require(getPackageJsonPath(moduleFilePath));
return `${pkgJson.name}@${pkgJson.version}/${moduleName}`;
}
exports.getModuleName = getModuleName;
function getFileOrigin(filePath) {
const absModuleFilePosixPath = new file_path_1.default(filePath).resolve().asPosix;
let fileOrigin = fileOriginCache[absModuleFilePosixPath];
if (fileOrigin) {
return fileOrigin;
}
const absBuildNodeModulesPosixPath = project_1.default.dir.join(project_1.default.buildDir, 'node_modules').asPosix;
const absBuildPosixPath = project_1.default.dir.join(project_1.default.buildDir).asPosix;
const absNodeModulesPosixPath = project_1.default.dir.join('node_modules').asPosix;
if (absModuleFilePosixPath.startsWith(absBuildNodeModulesPosixPath)) {
fileOrigin = FileOrigin.BUILD_DEPENDENCY;
}
else if (absModuleFilePosixPath.startsWith(absBuildPosixPath)) {
fileOrigin = FileOrigin.BUILD_PROJECT;
}
else if (absModuleFilePosixPath.startsWith(absNodeModulesPosixPath)) {
fileOrigin = FileOrigin.SOURCE_DEPENDENCY;
}
else {
fileOrigin = FileOrigin.SOURCE_PROJECT;
}
fileOriginCache[absModuleFilePosixPath] = fileOrigin;
return fileOrigin;
}