liferay-npm-bundler-plugin-replace-browser-modules
Advanced tools
Comparing version 2.13.3-alpha.6 to 2.13.3-alpha.8
207
lib/index.js
@@ -10,116 +10,125 @@ "use strict"; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const pkgs = __importStar(require("liferay-npm-build-tools-common/lib/packages")); | ||
const path_1 = __importDefault(require("path")); | ||
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"); | ||
/** | ||
* @return {void} | ||
* Plugin entry point | ||
*/ | ||
function default_1({ log, pkg }, { pkgJson }) { | ||
const browser = pkgJson.browser || pkgJson.unpkg || pkgJson.jsdelivr; | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson, log); | ||
} | ||
else if (browser) { | ||
replaceModules(pkg.dir, browser, pkgJson, log); | ||
} | ||
else { | ||
log.info('replace-browser-modules', 'No browser modules found'); | ||
} | ||
function default_1(params, {}) { | ||
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; | ||
/** | ||
* Copy "browser"/"module" module file on top of "main" module file. | ||
* @param {FilePath} pkgDir directory where package is placed | ||
* @param {String} browser the value of the "browser"/"module" field | ||
* @param {Object} pkgJson package.json contents | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
* | ||
* @param log | ||
* @param absRootDir | ||
* @param unrolledAliasesMap should be filtered so that there's only one alias | ||
* per entry. | ||
*/ | ||
function replaceMainModule(pkgDir, browser, pkgJson, log) { | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
const main = pkgJson.main || 'index.js'; | ||
const srcPath = pkgDir.join(pkgs.resolveModuleFile(pkgDir.asNative, browser)).asNative; | ||
const destPath = pkgDir.join(pkgs.resolveModuleFile(pkgDir.asNative, main)) | ||
.asNative; | ||
replaceFile(pkgId, srcPath, browser, destPath, main, log); | ||
} | ||
/** | ||
* Copy "browser"/"module" module files on top of their server versions. | ||
* @param {FilePath} pkgDir directory where package is placed | ||
* @param {Object} browser the value of the "browser"/"module" field | ||
* @param {Object} pkgJson package.json contents | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function replaceModules(pkgDir, browser, pkgJson, log) { | ||
const pkgId = `${pkgJson.name}@${pkgJson.version}`; | ||
Object.keys(browser).forEach(fromModuleName => { | ||
const toModuleName = browser[fromModuleName]; | ||
const destPath = pkgDir.join(pkgs.resolveModuleFile(pkgDir.asNative, fromModuleName)).asNative; | ||
if (toModuleName == false) { | ||
ignoreFile(destPath, fromModuleName, log); | ||
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'); | ||
} | ||
else { | ||
const srcPath = pkgDir.join(pkgs.resolveModuleFile(pkgDir.asNative, toModuleName)).asNative; | ||
replaceFile(pkgId, srcPath, toModuleName, destPath, fromModuleName, log); | ||
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 })); | ||
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; | ||
} | ||
} | ||
}); | ||
} | ||
/** | ||
* Replace one package file with another. | ||
* @param {String} pkgId package id (name@version) | ||
* @param {String} srcPath path to source file | ||
* @param {String} srcName the name of the source file | ||
* @param {String} destPath path to destination file | ||
* @param {String} destName the name of the destination file | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function replaceFile(pkgId, srcPath, srcName, destPath, destName, log) { | ||
const srcModuleName = srcName.replace('.js', ''); | ||
const destModuleName = destName.replace('.js', ''); | ||
log.info('replace-browser-modules', `Replacing module ${destName} with module ${srcName}`); | ||
try { | ||
let contents = ''; | ||
try { | ||
contents = fs_extra_1.default.readFileSync(srcPath).toString(); | ||
} | ||
catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
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 == 0) { | ||
delete unrolledAliasesMap[absFromPath]; | ||
} | ||
contents = contents.replace(`'${pkgId}/${srcModuleName}'`, `'${pkgId}/${destModuleName}'`); | ||
fs_extra_1.default.mkdirsSync(path_1.default.dirname(destPath)); | ||
fs_extra_1.default.writeFileSync(destPath, '/* Module replaced with ' + | ||
srcName + | ||
' by liferay-npm-bundler-plugin-replace-browser-modules */\n' + | ||
contents); | ||
} | ||
catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
}); | ||
// 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); | ||
}); | ||
} | ||
/** | ||
* Ignores one package | ||
* @param {String} filePath path to file to be ignored | ||
* @param {String} moduleName the name of the file | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function ignoreFile(filePath, moduleName, log) { | ||
log.info('replace-browser-modules', `Emptying module ${moduleName} because it is server-only`); | ||
fs_extra_1.default.mkdirsSync(path_1.default.dirname(filePath)); | ||
fs_extra_1.default.writeFileSync(filePath, '/* Module ignored by ' + | ||
'liferay-npm-bundler-plugin-replace-browser-modules */\n'); | ||
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')); | ||
} |
{ | ||
"name": "liferay-npm-bundler-plugin-replace-browser-modules", | ||
"version": "2.13.3-alpha.6+ae09044", | ||
"version": "2.13.3-alpha.8+682dbe8", | ||
"description": "A liferay-npm-bundler plugin to replace files listed under the browser/module entry of package.json files.", | ||
@@ -11,10 +11,8 @@ "main": "lib/index.js", | ||
}, | ||
"devDependencies": { | ||
"recursive-copy": "^2.0.9" | ||
}, | ||
"dependencies": { | ||
"dot-prop": "^5.0.1", | ||
"fs-extra": "^8.1.0", | ||
"liferay-npm-build-tools-common": "2.13.3-alpha.6+ae09044" | ||
"liferay-npm-build-tools-common": "2.13.3-alpha.8+682dbe8" | ||
}, | ||
"gitHead": "ae09044beb216743cb2f1e2273aa6e055716ce64" | ||
"gitHead": "682dbe86c34e5e1ab30b7476081b6a2ccfeb6b64" | ||
} |
# liferay-npm-bundler-plugin-replace-browser-modules | ||
> Replace modules listed under `browser`/`unpkg`/`jsdelivr` section of | ||
`package.json` files. | ||
> Rewrite aliased modules (those under `browser` section or any other | ||
> configured alias field of `package.json` files) to that they reexport their | ||
> targets. | ||
@@ -18,6 +19,4 @@ ## Installation | ||
{ | ||
"*": { | ||
"plugins": [ | ||
"replace-browser-modules" | ||
] | ||
"*": { | ||
"plugins": ["replace-browser-modules"] | ||
} | ||
@@ -27,11 +26,50 @@ } | ||
## Configuration | ||
The plugin can be configured using | ||
[.npmbundlerrc global config section](https://github.com/liferay/liferay-js-toolkit/wiki/.npmbundlerrc-file-reference#config) | ||
or in the plugin configuration itself. | ||
In both cases the structure is the same: | ||
```json | ||
{ | ||
"resolve": { | ||
"aliasFields": ["browser"] | ||
} | ||
} | ||
``` | ||
This resembles webpack's | ||
[resolve.aliasFields](https://webpack.js.org/configuration/resolve/#resolvealiasfields) | ||
which serves the same purpose. | ||
Normally global config is preferred, but you can leverage plugin configuration | ||
when you need different alias fields for different packages. | ||
The default value for `resolve.aliasFields` is `['browser']` as in webpack. | ||
Note that this plugin used to look for `unpkg` and `jsdelivr` fields too, but it | ||
caused problems (see https://github.com/liferay/liferay-js-toolkit/issues/365 | ||
for more information). | ||
## Technical Details | ||
This plugin scans `package.json` for a `browser`/`unpkg`/`jsdelivr` entry and | ||
copies browser modules on top of server modules or deletes them when set to | ||
`false`. | ||
This plugin scans `package.json` for fields defined in `resolve.aliasFields` | ||
and redirects aliased modules to their configured target or empties them when | ||
set to `false`. | ||
Please read the | ||
[`browser` field specification](https://github.com/defunctzombie/package-browser-field-spec) | ||
This plugin only does one part of the whole implementation of the aliases. | ||
Aliases implementation have two parts: | ||
1. They redirect existing modules or provide virtual ones when seen from | ||
the outside, from another package. | ||
2. They make local requires divert to a different target. | ||
This plugin does only the 1st part. The second one is performed by | ||
[babel-plugin-alias-modules](https://github.com/liferay/liferay-js-toolkit/tree/master/packages/babel-plugin-alias-modules). | ||
Please read the | ||
[`browser` field specification](https://github.com/defunctzombie/package-browser-field-spec) | ||
for more information. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
37132
0
804
74
3
12
2
+ Addeddot-prop@^5.0.1
+ Addeddot-prop@5.3.0(transitive)
+ Addedis-obj@2.0.0(transitive)
Updatedliferay-npm-build-tools-common@2.13.3-alpha.8+682dbe8