liferay-npm-bundler-preset-reactjs
Version:
liferay npm bundler preset react
138 lines (137 loc) • 6.74 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.reportAndResolveCollisions = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const alias_1 = require("liferay-npm-build-tools-common/lib/alias");
const file_path_1 = __importDefault(require("liferay-npm-build-tools-common/lib/file-path"));
const project_1 = __importDefault(require("liferay-npm-build-tools-common/lib/project"));
const config_1 = require("./config");
const util_1 = require("./util");
/**
* Plugin entry point
*/
function default_1(params, _state) {
const { log, pkg } = params;
const absRootDir = project_1.default.dir.join(pkg.dir);
const aliasesMap = config_1.readAliases(params, absRootDir);
const unrolledAliasesMap = util_1.unrollAliasesMap(aliasesMap);
reportAndResolveCollisions(log, absRootDir, unrolledAliasesMap);
processAliases(log, absRootDir, unrolledAliasesMap);
}
exports.default = default_1;
/**
*
* @param log
* @param absRootDir
* @param unrolledAliasesMap should be filtered so that there's only one alias
* per entry.
*/
function processAliases(log, absRootDir, unrolledAliasesMap) {
Object.entries(unrolledAliasesMap).forEach(([absFromPath, unrolledAliases]) => {
// Sanity check
if (unrolledAliases.length > 1) {
throw new Error('Unrolled aliases map has unresolved collisions');
}
const alias = unrolledAliases[0];
const absFromFile = new file_path_1.default(absFromPath);
const rootRelAbsDirPosixPath = absRootDir.relative(alias.absDir)
.asPosix;
const rootRelFilePosixPath = absRootDir.relative(absFromFile)
.asPosix;
switch (alias_1.getAliasToType(alias.to)) {
case alias_1.AliasToType.IGNORE: {
rewriteFile(absFromFile, `/* ignored by alias field(s) configured in ${rootRelAbsDirPosixPath} */`);
log.info('replace-browser-modules', `Emptied file '${rootRelFilePosixPath}' as ` +
`configured in '${rootRelAbsDirPosixPath}'`).linkToCode(1);
break;
}
case alias_1.AliasToType.EXTERNAL: {
rewriteFile(absFromFile, `/* redirected by alias field(s) in ${rootRelAbsDirPosixPath} */`, `module.exports = require('${alias.to}');`);
log.info('replace-browser-modules', `Redirected file '${rootRelFilePosixPath}' to ` +
`'${alias.to}' as configured in ` +
`'${rootRelAbsDirPosixPath}'`).linkToCode(2);
break;
}
case alias_1.AliasToType.LOCAL: {
const absToFile = alias.absDir.join(new file_path_1.default(alias.to, { posix: true }));
if (!absToFile.is(absFromFile)) {
const fromRelToFile = absFromFile
.dirname()
.relative(absToFile);
rewriteFile(absFromFile, `/* redirected by alias field(s) in ${rootRelAbsDirPosixPath} */`, `module.exports = require('./${fromRelToFile.asPosix}');`);
log.info('replace-browser-modules', `Redirected file '${rootRelFilePosixPath}' to ` +
`'./${fromRelToFile.asPosix}' as configured in ` +
`'${rootRelAbsDirPosixPath}'`).linkToCode(2);
}
break;
}
default:
break;
}
});
}
function reportAndResolveCollisions(log, absRootDir, unrolledAliasesMap) {
// Remove aliases out of ancestry line
Object.keys(unrolledAliasesMap).forEach((absFromPosixPath) => {
unrolledAliasesMap[absFromPosixPath] = unrolledAliasesMap[absFromPosixPath].filter((alias) => {
const included = absFromPosixPath.startsWith(alias.absDir.asPosix);
// No need to log anything because this type of alias must be
// addressed by rewriting requires, not by module rewrite
return included;
});
});
// Remove aliases of external modules that would overwrite a local one
Object.keys(unrolledAliasesMap).forEach((absFromPosixPath) => {
unrolledAliasesMap[absFromPosixPath] = unrolledAliasesMap[absFromPosixPath].filter((alias) => {
const included = alias.fromType !== alias_1.AliasFromType.EXTERNAL ||
!util_1.moduleExists(absFromPosixPath);
if (!included) {
const rootRelDir = absRootDir.relative(alias.absDir);
const where = rootRelDir.asPosix === ''
? "project's root folder"
: `'${rootRelDir.asPosix}'`;
log.warn('replace-browser-modules', `Alias '${alias.from}' configured in ${where} will not ` +
`be visible from outside because a local module with ` +
`the same name exists`).linkToCode(3);
}
return included;
});
});
// Remove empty aliases
Object.keys(unrolledAliasesMap).forEach((absFromPath) => {
if (!unrolledAliasesMap[absFromPath].length) {
delete unrolledAliasesMap[absFromPath];
}
});
// Resolve collisions in multiple length aliases
Object.entries(unrolledAliasesMap)
.filter(([_absFromPath, unrolledAliases]) => unrolledAliases.length > 1)
.forEach(([absFromPath, unrolledAliases]) => {
// Sort by distance to absFromPath
unrolledAliases.sort((a, b) => a.absDir.asPosix.length - b.absDir.asPosix.length);
// we always use the last
unrolledAliases.splice(0, unrolledAliases.length - 1);
const alias = unrolledAliases[0];
const rootRelFromPosixPath = absRootDir.relative(absFromPath)
.asPosix;
const rootRelDir = absRootDir.relative(alias.absDir);
const where = rootRelDir.asPosix === ''
? "project's root folder"
: `'${rootRelDir.asPosix}'`;
log.warn('replace-browser-modules', `File '${rootRelFromPosixPath}' is aliased more than once, ` +
`only the alias configured in ${where} will be visible ` +
`when required from outside`).linkToCode(4);
});
}
exports.reportAndResolveCollisions = reportAndResolveCollisions;
function rewriteFile(absFile, ...lines) {
fs_extra_1.default.ensureDirSync(absFile.dirname().asNative);
fs_extra_1.default.writeFileSync(absFile.asNative, lines.join('\n'));
}