liferay-npm-bundler-plugin-replace-browser-modules
Advanced tools
Comparing version 2.6.2 to 2.6.3-alpha.126
257
lib/index.js
@@ -1,142 +0,133 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = function (_ref, _ref2) { | ||
var pkg = _ref.pkg, | ||
log = _ref.log; | ||
var pkgJson = _ref2.pkgJson; | ||
var browser = pkgJson.browser || pkgJson.unpkg || pkgJson.jsdelivr; | ||
if (browser) { | ||
if (typeof browser === 'string') { | ||
replaceMainModule(pkg.dir, browser, pkgJson, log); | ||
} else { | ||
replaceModules(pkg.dir, browser, pkgJson, log); | ||
} | ||
} else { | ||
log.info('replace-browser-modules', 'No browser modules found'); | ||
} | ||
}; | ||
var _fsExtra = require('fs-extra'); | ||
var _fsExtra2 = _interopRequireDefault(_fsExtra); | ||
var _packages = require('liferay-npm-build-tools-common/lib/packages'); | ||
var pkgs = _interopRequireWildcard(_packages); | ||
var _path = require('path'); | ||
var _path2 = _interopRequireDefault(_path); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
"use strict"; | ||
/** | ||
* Copy "browser"/"module" module file on top of "main" module file. | ||
* @param {String} 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} | ||
* © 2017 Liferay, Inc. <https://liferay.com> | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
function replaceMainModule(pkgDir, browser, pkgJson, log) { | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
var main = pkgJson.main || 'index.js'; | ||
var src = _path2.default.join(pkgDir, pkgs.resolveModuleFile(pkgDir, browser)); | ||
var dest = _path2.default.join(pkgDir, pkgs.resolveModuleFile(pkgDir, main)); | ||
replaceFile(pkgId, src, browser, dest, main, log); | ||
} | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
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"); | ||
/** | ||
* Copy "browser"/"module" module files on top of their server versions. | ||
* @param {String} 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} | ||
* Plugin entry point | ||
*/ | ||
/** | ||
* @return {void} | ||
*/ | ||
function replaceModules(pkgDir, browser, pkgJson, log) { | ||
var pkgId = pkgJson.name + '@' + pkgJson.version; | ||
Object.keys(browser).forEach(function (from) { | ||
var to = browser[from]; | ||
var dest = _path2.default.join(pkgDir, pkgs.resolveModuleFile(pkgDir, from)); | ||
if (to == false) { | ||
ignoreFile(dest, from, log); | ||
} else { | ||
var src = _path2.default.join(pkgDir, pkgs.resolveModuleFile(pkgDir, to)); | ||
replaceFile(pkgId, src, to, dest, from, log); | ||
} | ||
}); | ||
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; | ||
/** | ||
* Replace one package file with another. | ||
* @param {String} pkgId package id (name@version) | ||
* @param {String} src path to source file | ||
* @param {String} srcName the name of the source file | ||
* @param {String} dest path to destination file | ||
* @param {String} destName the name of the destination file | ||
* @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 replaceFile(pkgId, src, srcName, dest, destName, log) { | ||
var srcModuleName = srcName.replace('.js', ''); | ||
var destModuleName = destName.replace('.js', ''); | ||
log.info('replace-browser-modules', 'Replacing module ' + destName + ' with module ' + srcName); | ||
try { | ||
var contents = ''; | ||
try { | ||
contents = _fsExtra2.default.readFileSync(src).toString(); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
contents = contents.replace('\'' + pkgId + '/' + srcModuleName + '\'', '\'' + pkgId + '/' + destModuleName + '\''); | ||
_fsExtra2.default.mkdirsSync(_path2.default.dirname(dest)); | ||
_fsExtra2.default.writeFileSync(dest, '/* Module replaced with ' + srcName + ' by liferay-npm-bundler-plugin-replace-browser-modules */\n' + contents); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
} | ||
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 })); | ||
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; | ||
} | ||
} | ||
}); | ||
} | ||
/** | ||
* Ignores one package | ||
* @param {String} file path to file to be ignored | ||
* @param {String} fileName the name of the file | ||
* @param {PluginLogger} log a logger | ||
* @return {void} | ||
*/ | ||
function ignoreFile(file, fileName, log) { | ||
log.info('replace-browser-modules', 'Emptying module ' + fileName + ' because it is server-only'); | ||
_fsExtra2.default.mkdirsSync(_path2.default.dirname(file)); | ||
_fsExtra2.default.writeFileSync(file, '/* Module ignored by ' + 'liferay-npm-bundler-plugin-replace-browser-modules */\n'); | ||
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]; | ||
} | ||
}); | ||
// 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); | ||
}); | ||
} | ||
//# sourceMappingURL=index.js.map | ||
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.6.2", | ||
"version": "2.6.3-alpha.126+f9a8cd2", | ||
"description": "A liferay-npm-bundler plugin to replace files listed under the browser/module entry of package.json files.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"build": "babel --source-maps -D -d lib src", | ||
"prepublish": "npm run build" | ||
"copyfiles": "node ../../scripts/copyfiles.js", | ||
"build": "tsc && yarn copyfiles", | ||
"prepublish": "yarn build" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-preset-es2015": "^6.24.1", | ||
"recursive-copy": "^2.0.9" | ||
"dependencies": { | ||
"dot-prop": "^5.0.1", | ||
"fs-extra": "^8.1.0", | ||
"liferay-npm-build-tools-common": "2.6.3-alpha.126+f9a8cd2" | ||
}, | ||
"dependencies": { | ||
"fs-extra": "^7.0.1", | ||
"liferay-npm-build-tools-common": "2.6.2" | ||
} | ||
"gitHead": "f9a8cd23c70ecc6fff238bea906286c1c6cb2ecc" | ||
} |
# 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
Copyleft License
License(Experimental) Copyleft license information was found.
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
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
0
804
74
4
37134
3
12
3
70
2
2
+ Addeddot-prop@^5.0.1
+ Addeddot-prop@5.3.0(transitive)
+ Addedfs-extra@8.1.0(transitive)
+ Addedis-obj@2.0.0(transitive)
- Removedfs-extra@7.0.1(transitive)
- Removedliferay-npm-build-tools-common@2.6.2(transitive)
- Removedread-json-sync@2.0.1(transitive)
Updatedfs-extra@^8.1.0
Updatedliferay-npm-build-tools-common@2.6.3-alpha.126+f9a8cd2