@ms-cloudpack/config
Advanced tools
Comparing version 0.28.2 to 0.28.3
@@ -10,5 +10,5 @@ import { PackageDefinitions } from '@ms-cloudpack/package-utilities'; | ||
const packages = new PackageDefinitions(undefined, config); | ||
packages.registerTransform((newConfig) => createPackageSettingsTransform(newConfig)); | ||
packages.registerTransform((newConfig) => (newConfig ? createPackageSettingsTransform(newConfig) : () => undefined)); | ||
return packages; | ||
} | ||
//# sourceMappingURL=createPackageDefinitions.js.map |
@@ -0,3 +1,4 @@ | ||
import { addExportsMapEntry, getExportsMap } from '@ms-cloudpack/package-utilities'; | ||
import { slash } from '@ms-cloudpack/path-string-parsing'; | ||
import { getPackageSettings } from './getPackageSettings.js'; | ||
import { addExportsMapEntry, getExportsMap } from '@ms-cloudpack/package-utilities'; | ||
import { mergePackageSettings } from './mergePackageSettings.js'; | ||
@@ -83,27 +84,22 @@ /** | ||
const { packagePath, definition, userPackageSettings, generatedPackageSettings } = options; | ||
const { appPath } = context.config; | ||
// TODO: an array of export objects is not really valid per the spec and most implementations | ||
const exports = []; | ||
// If this is the app package, ensure the routes are part of the exports map. | ||
if (appPath === packagePath) { | ||
const { routes = [] } = context.config; | ||
const { appPath, routes } = context.config; | ||
// If this is the app package and it has routes with entries, use those to define the exports map. | ||
if (slash(appPath) === slash(packagePath) && routes?.length) { | ||
const routeExports = {}; | ||
let hasRouteExports = false; | ||
for (const route of routes) { | ||
const { entry } = route; | ||
if (entry?.length) { | ||
for (const entryItem of entry) { | ||
hasRouteExports = | ||
(await addExportsMapEntry({ | ||
exports: routeExports, | ||
packagePath, | ||
environmentCondition: 'browser', | ||
filePath: entryItem.sourcePath, | ||
importPath: entryItem.importPath, | ||
}, context)) || hasRouteExports; | ||
} | ||
if (!entry?.length) { | ||
continue; | ||
} | ||
for (const entryItem of entry) { | ||
await addExportsMapEntry({ | ||
exports: routeExports, | ||
packagePath, | ||
environmentCondition: 'browser', | ||
filePath: entryItem.sourcePath, | ||
importPath: entryItem.importPath, | ||
}, context); | ||
} | ||
} | ||
// Insert them at the start, so that future exports have precedence. | ||
if (hasRouteExports) { | ||
if (Object.keys(routeExports).length) { | ||
// We short circuit here because we don't want to include any unused exports that might | ||
@@ -114,2 +110,4 @@ // be pulled from package.json. | ||
} | ||
// TODO: an array of export objects is not really valid per the spec and most implementations | ||
const exports = []; | ||
// Push exports from the package. | ||
@@ -116,0 +114,0 @@ exports.push(userPackageSettings?.exports || |
@@ -0,1 +1,2 @@ | ||
import { isShorthandBootstrapRoute, isShorthandRenderedRoute } from '@ms-cloudpack/common-types'; | ||
import { readAppConfig } from './readAppConfig.js'; | ||
@@ -6,3 +7,3 @@ import { readGeneratedConfig } from './readGeneratedConfig.js'; | ||
import path from 'path'; | ||
import { readJson } from '@ms-cloudpack/json-utilities'; | ||
import { readJsonSync } from '@ms-cloudpack/json-utilities'; | ||
/** | ||
@@ -21,66 +22,56 @@ * Reads the config file asynchronously (and merges generated config into the result.) | ||
// Expands all routes which refer to source files into fully transformed route objects. | ||
await expandRouteShorthandEntries(config, appPath); | ||
const routes = appConfig.routes; | ||
if (routes?.length) { | ||
config.routes = expandRoutes(routes, appPath); | ||
} | ||
return config; | ||
} | ||
/** | ||
* Ensure all source entries in route are expanded to fully qualified entry paths. | ||
* Ensure all source entries in routes are expanded to fully qualified entry paths. | ||
*/ | ||
async function expandRouteShorthandEntries(config, appPath) { | ||
const { routes } = config; | ||
let definition; | ||
if (!routes?.length) { | ||
return; | ||
} | ||
for (const route of routes) { | ||
const shorthandRoute = route; | ||
const renderedRoute = route; | ||
renderedRoute.entry = ensureArray(shorthandRoute.entry)?.map((filePath) => expandRouteEntry({ | ||
filePath, | ||
appPath, | ||
})); | ||
function expandRoutes(routes, appPath) { | ||
// Cached flattened exports for appPath, used to expand deprecated exportEntry props if present | ||
let flattenedExports; | ||
return routes.map((route) => { | ||
if (!isShorthandRenderedRoute(route) && !isShorthandBootstrapRoute(route)) { | ||
// Skip extra processing for other route types. | ||
return route; | ||
} | ||
// eslint-disable-next-line etc/no-deprecated | ||
if (shorthandRoute.exportEntry) { | ||
if (!definition) { | ||
// We can't use PackageDefinitions.get here because we're reading the config, and definitions | ||
// cache requires config resulting in a cycle. | ||
definition = (await readJson(path.join(appPath, 'package.json'))); | ||
const { entry, exportEntry, ...rest } = route; | ||
const renderedRoute = rest; | ||
if (entry?.length) { | ||
// Expand the entry to full info about the file path. | ||
renderedRoute.entry = expandEntry(entry, appPath); | ||
} | ||
if (exportEntry) { | ||
// Handle backwards compatibility with moving from exportEntry to entry. | ||
flattenedExports ??= getFlattenedExports(appPath); | ||
const filePath = flattenedExports[exportEntry]; | ||
if (!filePath) { | ||
throw new Error(`A route referenced exportEntry "${exportEntry}" but this couldn't be resolved from the package.json.`); | ||
} | ||
expandDeprecatedExportEntry(route, definition, appPath); | ||
const expanded = expandRouteEntry({ importPath: exportEntry, filePath, appPath }); | ||
console.warn('The route property "exportEntry" is deprecated. Update your route as follows:', JSON.stringify({ ...renderedRoute, entry: expanded.sourcePath }, null, 2)); | ||
(renderedRoute.entry ??= []).push(expanded); | ||
} | ||
} | ||
return renderedRoute; | ||
}); | ||
} | ||
function expandEntry(entry, appPath) { | ||
entry = Array.isArray(entry) ? entry : entry !== undefined ? [entry] : []; | ||
return entry.map((filePath) => expandRouteEntry({ filePath, appPath })); | ||
} | ||
/** | ||
* Handle backwards compatibility with moving from exportEntry to entry. When exportEntry is deleted, | ||
* this function can be removed. | ||
* Get flattened exports from package.json at `appPath`. | ||
* If there's no export for `.`, it will be filled in from `module || main`. | ||
*/ | ||
function expandDeprecatedExportEntry(route, definition, appPath) { | ||
const shorthandRoute = route; | ||
const renderedRoute = route; | ||
// eslint-disable-next-line etc/no-deprecated | ||
const { exportEntry } = shorthandRoute; | ||
const flattenedExports = flattenExportsMap(definition.exports || {}); | ||
if (exportEntry) { | ||
renderedRoute.entry = ensureArray(renderedRoute.entry); | ||
const filePath = flattenedExports[exportEntry] || definition.module || definition.main; | ||
if (!filePath) { | ||
throw new Error(`A route referenced exportEntry "${exportEntry}" but this couldn't be resolved from the package.json.`); | ||
} | ||
// We are converting the exportEntry to an ExpandedSourcePath so that the route | ||
// can be returned as a RenderedRoute. | ||
renderedRoute.entry.push(expandRouteEntry({ | ||
importPath: exportEntry, | ||
filePath, | ||
appPath, | ||
})); | ||
// eslint-disable-next-line etc/no-deprecated | ||
delete shorthandRoute.exportEntry; | ||
} | ||
function getFlattenedExports(appPath) { | ||
// We can't use PackageDefinitions.get here because we're reading the config, and definitions | ||
// cache requires config resulting in a cycle. | ||
const definition = readJsonSync(path.join(appPath, 'package.json')) || {}; | ||
const flatExports = flattenExportsMap(definition.exports || {}); | ||
flatExports['.'] ??= definition.module || definition.main; | ||
return flatExports; | ||
} | ||
/** Ensures the value is an array, or is wrapped in one. If a value is blank, will return a blank array. */ | ||
function ensureArray(value) { | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
return (value !== undefined ? [value] : []); | ||
} | ||
/** Expands shorthand route entries into fully qualified ones. */ | ||
@@ -87,0 +78,0 @@ function expandRouteEntry(options) { |
{ | ||
"name": "@ms-cloudpack/config", | ||
"version": "0.28.2", | ||
"version": "0.28.3", | ||
"description": "Configuration handling for cloudpack.", | ||
@@ -17,6 +17,7 @@ "license": "MIT", | ||
"dependencies": { | ||
"@ms-cloudpack/common-types": "^0.19.2", | ||
"@ms-cloudpack/common-types": "^0.19.3", | ||
"@ms-cloudpack/json-utilities": "^0.1.7", | ||
"@ms-cloudpack/package-utilities": "^10.0.2", | ||
"@ms-cloudpack/path-utilities": "^2.7.39", | ||
"@ms-cloudpack/package-utilities": "^10.0.3", | ||
"@ms-cloudpack/path-string-parsing": "^1.2.4", | ||
"@ms-cloudpack/path-utilities": "^2.7.40", | ||
"import-meta-resolve": "^4.0.0", | ||
@@ -23,0 +24,0 @@ "semver": "^7.6.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
131609
7
1104