|
| 1 | +const SOURCE_PACKAGE = '@clerk/types'; |
| 2 | +const TARGET_PACKAGE = '@clerk/shared/types'; |
| 3 | +const UI_PACKAGE = '@clerk/ui'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Specifiers that should be redirected to `@clerk/ui` instead of `@clerk/shared/types`. |
| 7 | + */ |
| 8 | +const UI_SPECIFIERS = new Set(['Appearance']); |
| 9 | + |
| 10 | +/** |
| 11 | + * Transforms imports of `@clerk/types` to `@clerk/shared/types`, splitting out |
| 12 | + * `Appearance` to `@clerk/ui`. |
| 13 | + * |
| 14 | + * @param {import('jscodeshift').FileInfo} fileInfo |
| 15 | + * @param {import('jscodeshift').API} api |
| 16 | + * @returns {string|undefined} |
| 17 | + */ |
| 18 | +module.exports = function transformClerkTypesToSharedTypes({ source }, { jscodeshift: j }) { |
| 19 | + const root = j(source); |
| 20 | + let dirty = false; |
| 21 | + |
| 22 | + // --- Transform import declarations --- |
| 23 | + root.find(j.ImportDeclaration, { source: { value: SOURCE_PACKAGE } }).forEach(path => { |
| 24 | + const node = path.node; |
| 25 | + const specifiers = node.specifiers || []; |
| 26 | + const importKind = node.importKind; |
| 27 | + |
| 28 | + const uiSpecifiers = []; |
| 29 | + const sharedSpecifiers = []; |
| 30 | + |
| 31 | + for (const spec of specifiers) { |
| 32 | + if (j.ImportSpecifier.check(spec) && UI_SPECIFIERS.has(spec.imported.name)) { |
| 33 | + uiSpecifiers.push(spec); |
| 34 | + } else { |
| 35 | + sharedSpecifiers.push(spec); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (uiSpecifiers.length > 0 && sharedSpecifiers.length > 0) { |
| 40 | + // Mixed: split into two imports |
| 41 | + const sharedImport = j.importDeclaration(sharedSpecifiers, j.stringLiteral(TARGET_PACKAGE)); |
| 42 | + if (importKind) { |
| 43 | + sharedImport.importKind = importKind; |
| 44 | + } |
| 45 | + sharedImport.comments = node.comments; |
| 46 | + |
| 47 | + const uiImport = j.importDeclaration(uiSpecifiers, j.stringLiteral(UI_PACKAGE)); |
| 48 | + if (importKind) { |
| 49 | + uiImport.importKind = importKind; |
| 50 | + } |
| 51 | + |
| 52 | + j(path).replaceWith([sharedImport, uiImport]); |
| 53 | + dirty = true; |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (uiSpecifiers.length > 0) { |
| 58 | + // Only UI specifiers |
| 59 | + node.source.value = UI_PACKAGE; |
| 60 | + dirty = true; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Only shared specifiers (or namespace/default imports) |
| 65 | + node.source.value = TARGET_PACKAGE; |
| 66 | + dirty = true; |
| 67 | + }); |
| 68 | + |
| 69 | + // --- Transform require calls --- |
| 70 | + root |
| 71 | + .find(j.VariableDeclarator, { |
| 72 | + init: { |
| 73 | + callee: { name: 'require' }, |
| 74 | + arguments: [{ value: SOURCE_PACKAGE }], |
| 75 | + }, |
| 76 | + }) |
| 77 | + .forEach(path => { |
| 78 | + const node = path.node; |
| 79 | + const id = node.id; |
| 80 | + |
| 81 | + if (id.type === 'ObjectPattern') { |
| 82 | + const uiProperties = []; |
| 83 | + const sharedProperties = []; |
| 84 | + |
| 85 | + for (const prop of id.properties) { |
| 86 | + if (prop.key && UI_SPECIFIERS.has(prop.key.name)) { |
| 87 | + uiProperties.push(prop); |
| 88 | + } else { |
| 89 | + sharedProperties.push(prop); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (uiProperties.length > 0 && sharedProperties.length > 0) { |
| 94 | + // Mixed: keep shared on main, create new require for UI |
| 95 | + node.id.properties = sharedProperties; |
| 96 | + node.init.arguments[0] = j.literal(TARGET_PACKAGE); |
| 97 | + |
| 98 | + const variableDeclaration = path.parent.node; |
| 99 | + const kind = variableDeclaration.kind || 'const'; |
| 100 | + |
| 101 | + const uiDeclarator = j.variableDeclarator( |
| 102 | + j.objectPattern(uiProperties), |
| 103 | + j.callExpression(j.identifier('require'), [j.literal(UI_PACKAGE)]), |
| 104 | + ); |
| 105 | + const uiDeclaration = j.variableDeclaration(kind, [uiDeclarator]); |
| 106 | + |
| 107 | + j(path.parent).insertAfter(uiDeclaration); |
| 108 | + dirty = true; |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (uiProperties.length > 0) { |
| 113 | + node.init.arguments[0] = j.literal(UI_PACKAGE); |
| 114 | + dirty = true; |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Only shared or not destructured |
| 120 | + node.init.arguments[0] = j.literal(TARGET_PACKAGE); |
| 121 | + dirty = true; |
| 122 | + }); |
| 123 | + |
| 124 | + if (!dirty) { |
| 125 | + return undefined; |
| 126 | + } |
| 127 | + |
| 128 | + let result = root.toSource(); |
| 129 | + result = result.replace(/^(['"`][^'"`]+['"`]);;/gm, '$1;'); |
| 130 | + return result; |
| 131 | +}; |
| 132 | + |
| 133 | +module.exports.parser = 'tsx'; |
0 commit comments