liferay-npm-bundler-preset-reactjs
Version:
liferay npm bundler preset react
108 lines (107 loc) • 4.25 kB
JavaScript
;
/**
* 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.resolveModuleFile = exports.getPackageTargetDir = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const read_json_sync_1 = __importDefault(require("read-json-sync"));
/**
* Converts a package name (optionally versioned) to its target folder name
* inside bundled node_modules.
* @param name a package name
* @param version an optional package version
* @return the target folder
*/
function getPackageTargetDir(name, version = null) {
let targetFolder = name.replace('/', '%2F');
if (version) {
targetFolder += `@${version}`;
}
return targetFolder;
}
exports.getPackageTargetDir = getPackageTargetDir;
/**
* Resolves a module name inside a package directory to a file relative (to
* package directory) path.
* For example, if you pass './lib' as moduleName and there's an 'index.js' file
* inside the 'lib' dir, the method returns './lib/index.js'.
* It also honors any 'package.json' with a 'main' entry in package subfolders.
* @param pkgPath path to package directory
* @param moduleName the module name
* @return a path relative to pkgDir
*/
function resolveModuleFile(pkgPath, moduleName) {
let fullModulePath = path_1.default.resolve(path_1.default.join(pkgPath, ...moduleName.split('/')));
let moduleStats = safeStat(fullModulePath);
if (moduleStats.isDirectory()) {
// Given module name is a directory
const pkgJsonPath = path_1.default.join(fullModulePath, 'package.json');
const pkgJsonStats = safeStat(pkgJsonPath);
if (pkgJsonStats.isFile()) {
// Module directory has package.json file
const pkgJson = read_json_sync_1.default(pkgJsonPath);
const { main } = pkgJson;
if (main) {
// Module directory has package.json file with main entry:
// recursively resolve the main entry's file path
fullModulePath = path_1.default.join(pkgPath, resolveModuleFile(pkgPath, path_1.default.join(moduleName, main)));
}
else {
// Module directory has package.json file without main entry:
// assume index.js
fullModulePath = path_1.default.join(fullModulePath, 'index.js');
}
}
else {
// Module directory has not package.json file: assume index.js
fullModulePath = path_1.default.join(fullModulePath, 'index.js');
}
}
else if (moduleStats.isFile()) {
// Given module name is a file: do nothing
}
else if (fullModulePath.endsWith('.js')) {
// Given module name is not a directory nor a file but ends with '.js'
// extension: see if corresponding '.js.js' file exists
moduleStats = safeStat(`${fullModulePath}.js`);
if (moduleStats.isFile()) {
// Given module name has a corresponding '.js.js' file: add '.js'
// extension
fullModulePath += '.js';
}
else {
// Given module name has no corresponding '.js.js' file: do nothing
// and assume that the '.js' in the module name is just the
// extension of the file and doesn't belong to its name.
}
}
else {
// Given module name is not a directory nor a file and doesn't end with
// '.js' extension: add '.js' extension
fullModulePath += '.js';
}
return path_1.default.relative(pkgPath, fullModulePath);
}
exports.resolveModuleFile = resolveModuleFile;
/**
* Do as fs.statSync without throwing errors.
* @param path path to check
* @return {fs.Stats} a real fs.Stats object or a null object
*/
function safeStat(path) {
try {
return fs_1.default.statSync(path);
}
catch (err) {
return {
isDirectory: () => false,
isFile: () => false,
};
}
}