From 0781c6a3a2a69dce800a3106908b9d9c694d26df Mon Sep 17 00:00:00 2001 From: Lukas Spirig Date: Tue, 16 Apr 2024 10:32:07 +0200 Subject: [PATCH] fix: adapt react package import paths missed during migration (#2589) This PR fixes the remaining mistaken imports in the React package. --- .../custom-elements-manifest.config.js | 72 +++++++++++-------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/tools/manifest/custom-elements-manifest.config.js b/tools/manifest/custom-elements-manifest.config.js index 2b232d8106..c1c3c51599 100644 --- a/tools/manifest/custom-elements-manifest.config.js +++ b/tools/manifest/custom-elements-manifest.config.js @@ -4,10 +4,16 @@ export default { litelement: true, globs: ['src/components/**/*.ts'], - exclude: ['**/*[.-]{stories,spec,e2e,test-utils}.ts', '**/private/*', 'vite.config.ts'], + exclude: [ + '**/*[.-]{stories,spec,e2e,test-utils}.ts', + '**/private/*', + '**/private.ts', + 'vite.config.ts', + ], outdir: 'dist/components', dependencies: false, packagejson: false, + /** @type {import('@custom-elements-manifest/analyzer').Plugin[]} */ plugins: [ { analyzePhase({ ts, node, moduleDoc }) { @@ -26,19 +32,21 @@ export default { } }, packageLinkPhase({ customElementsManifest }) { - function fixModulePaths(node) { + function fixModulePaths(node, pathAction) { for (const [key, value] of Object.entries(node)) { if (Array.isArray(value)) { - value.forEach(fixModulePaths); + value.forEach((v) => fixModulePaths(v, pathAction)); } else if (typeof value === 'object') { - fixModulePaths(value); + fixModulePaths(value, pathAction); } else if (key === 'module' || key === 'path') { - node[key] = value.replace(/^\/?src\/components\//, '').replace(/.ts$/, '.js'); + node[key] = pathAction(value); } } } + const fixTsPaths = (value) => + value.replace(/^\/?src\/components\//, '').replace(/.ts$/, '.js'); for (const module of customElementsManifest.modules) { - fixModulePaths(module); + fixModulePaths(module, fixTsPaths); for (const declaration of module.declarations.filter((d) => d.kind === 'class')) { if (declaration.name === 'SbbIconBase') { delete declaration.customElement; @@ -54,36 +62,40 @@ export default { } } } - const isInlinedEntryPoint = (entry) => { - const parts = entry.declaration.package?.split(/[./]+/) ?? []; - return ( - entry.name === '*' && - entry.declaration.name === '*' && - parts.length === 4 && - parts[0] === '' && - parts[1] === parts[2] && - parts[3] === 'js' - ); - }; + const reexportModules = []; + const moduleMapping = new Map(); for (const moduleEntry of customElementsManifest.modules) { if ( - !moduleEntry.declarations.length && - moduleEntry.exports.length === 1 && - isInlinedEntryPoint(moduleEntry.exports[0]) + !reexportModules.includes(moduleEntry) && + moduleEntry.exports?.length && + moduleEntry.exports.some((e) => e.name !== '*') ) { - const entry = moduleEntry.exports.pop(); - const path = `${moduleEntry.path.replace(/\/[\w-]+.js/, '/').replace(/[\w-]+.js$/, '')}${entry.declaration.package.substring(2)}`; - const compiledModule = customElementsManifest.modules.find((m) => m.path === path); - moduleEntry.declarations.push(...compiledModule.declarations); - for (const entry of compiledModule.exports) { - entry.declaration.module = entry.declaration.module.replace(/\/[\w-]+.js/, '.js'); - } - moduleEntry.exports.push(...compiledModule.exports); - customElementsManifest.modules = customElementsManifest.modules.filter( - (m) => m !== compiledModule, + const reexportPath = moduleEntry.path.replace(/\/[\w-]+\.js/, '.js'); + const reexportModule = customElementsManifest.modules.find( + (m) => m.path === reexportPath, ); + if (reexportModule) { + moduleMapping.set(moduleEntry.path, reexportModule.path); + reexportModules.push(reexportModule); + reexportModule.declarations.push(...moduleEntry.declarations); + for (const entry of moduleEntry.exports) { + entry.declaration.module = reexportModule.path; + } + reexportModule.exports = reexportModule.exports.filter( + (e) => + e.name !== '*' || + moduleEntry.path === + reexportModule.path.replace(/\/[\w-]+\.js/, '') + + e.declaration.package.substring(2), + ); + reexportModule.exports.push(...moduleEntry.exports); + customElementsManifest.modules = customElementsManifest.modules.filter( + (m) => m !== moduleEntry, + ); + } } } + fixModulePaths(customElementsManifest, (value) => moduleMapping.get(value) ?? value); }, }, ],