ui5-tooling-transpile
Advanced tools
Comparing version 3.0.3 to 3.0.4
@@ -6,2 +6,13 @@ # Change Log | ||
## [3.0.4](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/ui5-tooling-transpile@3.0.3...ui5-tooling-transpile@3.0.4) (2023-08-24) | ||
### Bug Fixes | ||
* **ui5-tooling-transpile:** support preset/plugin resolution with UI5 tooling binary ([#802](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/802)) ([a3943ab](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/a3943abf118c78bc37019c0a39d5a41adcf240db)) | ||
## [3.0.3](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/ui5-tooling-transpile@3.0.2...ui5-tooling-transpile@3.0.3) (2023-08-12) | ||
@@ -8,0 +19,0 @@ |
128
lib/util.js
@@ -27,2 +27,3 @@ const os = require("os"); | ||
// helper to find the babel configuration | ||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
@@ -68,2 +69,87 @@ async function findBabelConfig(dir) { | ||
// utility to resolve the node modules (also for mono repo environments) | ||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
function resolveNodeModule(moduleName, cwd = process.cwd()) { | ||
let modulePath; | ||
// resolve from node_modules via regular lookup | ||
try { | ||
// try the lookup relative to CWD | ||
modulePath = require.resolve(moduleName, { | ||
paths: [cwd] // necessary for PNPM and/or DEBUG scenario | ||
}); | ||
} catch (err) { | ||
// use the default lookup | ||
try { | ||
modulePath = require.resolve(moduleName); | ||
} catch (err) { | ||
// gracefully ignore the error | ||
//console.error(err); | ||
} | ||
} | ||
return modulePath; | ||
} | ||
// utility to normalite the name of the babel preset or plugin | ||
// like specified here: https://babeljs.io/docs/options#name-normalization | ||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
function normalizePresetOrPlugin(babelPresetOrPlugin, isPreset) { | ||
const type = isPreset ? "preset" : "plugin"; | ||
let moduleName = babelPresetOrPlugin; | ||
let matches; | ||
if (!(typeof moduleName === "string")) { | ||
// ConfigItems are ignored (means preset/plugin is already loaded) | ||
} else if (!moduleName) { | ||
// empty module names stay untouched | ||
} else if (path.isAbsolute(moduleName)) { | ||
// absolute paths stay untouched | ||
} else if (moduleName.startsWith("./")) { | ||
// relative paths stay untouched | ||
} else if ((matches = /^module:(.*)/.exec(moduleName))) { | ||
// any identifier prefixed with module: will have the prefix removed but otherwise be untouched. | ||
moduleName = matches[1]; | ||
} else if ((matches = new RegExp(`^@babel/(?!${type}-)([^/]+)$`).exec(moduleName))) { | ||
// plugin-/preset- will be injected at the start of any @babel-scoped package that doesn't have it as a prefix. | ||
moduleName = `@babel/${type}-${matches[1]}`; | ||
} else if ((matches = /^@([^/]+)$/.exec(moduleName))) { | ||
// babel-plugin/babel-preset will be injected as the package name if only the @-scope name is given. | ||
moduleName = `${moduleName}/babel-${type}`; | ||
} else if ((matches = new RegExp(`^(?!(@|babel-${type}-))([^/]+)$`).exec(moduleName))) { | ||
// babel-plugin-/babel-preset- will be injected as a prefix any unscoped package that doesn't have it as a prefix | ||
moduleName = `babel-${type}-${moduleName}`; | ||
} else if ((matches = new RegExp(`^(@(?!babel)(?:[^/]+)/)([^/]+)$`).exec(moduleName))) { | ||
// babel-plugin-/babel-preset- will be injected as a prefix any @-scoped package that doesn't have it anywhere in their name. | ||
if (!new RegExp(`babel-${type}`).test(matches[2])) { | ||
moduleName = `${matches[1]}babel-${type}-${matches[2]}`; | ||
} | ||
} | ||
return moduleName; | ||
} | ||
// utility to normalize and resolve the babel preset or plugin | ||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
function resolvePresetOrPlugin(babelPresetOrPlugin, isPreset, cwd = process.cwd()) { | ||
if (Array.isArray(babelPresetOrPlugin)) { | ||
const normalized = normalizePresetOrPlugin(babelPresetOrPlugin[0], isPreset); | ||
babelPresetOrPlugin[0] = resolveNodeModule(normalized, cwd); | ||
} else { | ||
const normalized = normalizePresetOrPlugin(babelPresetOrPlugin, isPreset); | ||
babelPresetOrPlugin = resolveNodeModule(normalized, cwd); | ||
} | ||
return babelPresetOrPlugin; | ||
} | ||
// helper to normalize and resolve the babel configuration (resolve plugins and presets to absolute paths) | ||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
function normalizeBabelConfig(babelConfig, cwd = process.cwd()) { | ||
// resolve the presets | ||
if (Array.isArray(babelConfig?.presets)) { | ||
babelConfig.presets = babelConfig.presets.map((preset) => resolvePresetOrPlugin(preset, true, cwd)); | ||
} | ||
// resolve the plugins | ||
if (Array.isArray(babelConfig?.plugins)) { | ||
babelConfig.plugins = babelConfig.plugins.map((plugin) => resolvePresetOrPlugin(plugin, false, cwd)); | ||
} | ||
return babelConfig; | ||
} | ||
module.exports = function (log) { | ||
@@ -139,3 +225,4 @@ const _this = { | ||
targetBrowsers: config.targetBrowsers, | ||
removeConsoleStatements: config.removeConsoleStatements | ||
removeConsoleStatements: config.removeConsoleStatements, | ||
skipBabelPresetPluginResolve: config.skipBabelPresetPluginResolve | ||
}; | ||
@@ -178,7 +265,16 @@ config.debug && | ||
// utility to add source maps support for middleware usage | ||
const enhanceForSourceMaps = function (babelConfig) { | ||
// utility to update the babel config | ||
const updateBabelConfig = function (babelConfig) { | ||
// make the paths of the babel plugins and presets absolute | ||
if (!configuration.skipBabelPresetPluginResolve) { | ||
normalizeBabelConfig(babelConfig, cwd); | ||
} | ||
// in the middleware case we generate the sourcemaps inline for | ||
// debugging purposes since the middleware may not know about the | ||
// sourcemaps files next to the source file | ||
if (isMiddleware) { | ||
babelConfig.sourceMaps = "inline"; | ||
} | ||
// some logging | ||
configuration?.debug && log.verbose(`${JSON.stringify(babelConfig, null, 2)}`); | ||
return babelConfig; | ||
@@ -195,3 +291,3 @@ }; | ||
} | ||
return enhanceForSourceMaps(babelConfig); | ||
return updateBabelConfig(babelConfig); | ||
} | ||
@@ -207,3 +303,3 @@ | ||
} | ||
return enhanceForSourceMaps(config.options); | ||
return updateBabelConfig(config.options); | ||
} | ||
@@ -303,14 +399,6 @@ | ||
// in the middleware case we generate the sourcemaps inline for | ||
// debugging purposes since the middleware may not know about the | ||
// sourcemaps files next to the source file | ||
if (isMiddleware) { | ||
babelConfig.sourceMaps = "inline"; | ||
} else { | ||
babelConfig.sourceMaps = true; | ||
} | ||
// include the source maps | ||
babelConfig.sourceMaps = true; | ||
configuration?.debug && log.verbose(`${JSON.stringify(babelConfig, null, 2)}`); | ||
return babelConfig; | ||
return updateBabelConfig(babelConfig); | ||
}, | ||
@@ -433,3 +521,11 @@ | ||
}; | ||
// expose internal functions for testing purposes | ||
_this._helpers = { | ||
findBabelConfig, | ||
normalizeBabelConfig, | ||
resolvePresetOrPlugin, | ||
resolveNodeModule, | ||
normalizePresetOrPlugin | ||
}; | ||
return _this; | ||
}; |
{ | ||
"name": "ui5-tooling-transpile", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "UI5 tooling extensions to transpile code", | ||
@@ -37,3 +37,3 @@ "author": "Jorge Martins, Peter Muessig", | ||
}, | ||
"gitHead": "6233f8b4d2bfaf7e7f2b32afe4320acb7fab521a" | ||
"gitHead": "c1177cc76795828b254beb662ad66144c9fe67a9" | ||
} |
@@ -56,2 +56,5 @@ # UI5 Tooling Extension for Transpiling JS/TS | ||
- skipBabelPresetPluginResolve: `boolean` (*experimental feature*) | ||
if enabled, the babel presets and plugins will not be resolved by the tooling extension and babel itself will do it. This can cause babel presets or plugins not to be found in case of working in monorepos. | ||
The following configuration options will only be taken into account if no inline babel configuration is maintained in the `ui5.yaml` as `babelConfig` or no external babel configuration exists in any configuration file as described in [Babels configuration section](https://babeljs.io/docs/configuration): | ||
@@ -58,0 +61,0 @@ |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
85898
918
175
8