Skip to content

Commit

Permalink
chore: rework imports
Browse files Browse the repository at this point in the history
  • Loading branch information
theo-mesnil committed Jan 22, 2025
1 parent 97951f6 commit c75ef35
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Getting started

```js
import React from 'react'
import { Button, createTheme, WuiProvider } from 'welcome-ui'
import { Button, createTheme, WuiProvider } from 'welcome-ui/theme'
import { Button } from 'welcome-ui/Button'

// Add theme options (if you want)
const options = {
Expand Down
84 changes: 41 additions & 43 deletions scripts/update-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,43 @@ const glob = require('glob')

const [pattern = ''] = process.argv.slice(2)

function mergeImports(content) {
// Extract all `welcome-ui` imports (normal and type imports)
const importRegex =
/import\s+(type\s+)?{([^}]+)}\s+from\s+['"](@welcome-ui\/[^'"]+|welcome-ui)['"]/g
const imports = new Set()
const typeImports = new Set()
let match
function transformToUpperCamelCase(str) {
return str.replace(/\/(\w+)$/, match => {
// Remove leading slash and transform each word to uppercase
return (
'/' +
match
.slice(1)
.split('-')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join('')
)
})
}

while ((match = importRegex.exec(content)) !== null) {
const isTypeImport = match[1]?.trim() === 'type'
const importSet = isTypeImport ? typeImports : imports
function transformImports(content) {
// Handle special cases first
content = content.replace(/@welcome-ui\/core/g, 'welcome-ui/theme')
content = content.replace(/@welcome-ui\/copy/g, 'welcome-ui/utils')

match[2]
// Transform WuiTheme to ThemeValues in imports, preserving type imports
const importBlockRegex = /(import\s+(?:type\s+)?){([^}]+)}\s+from/g
content = content.replace(importBlockRegex, (match, importStatement, importBlock) => {
const transformedImports = importBlock
.split(',')
.map(i => i.trim())
.filter(Boolean)
.forEach(i => {
// Rename WuiTheme to ThemeValues for type imports
if (isTypeImport && i === 'WuiTheme') {
importSet.add('ThemeValues')
} else {
importSet.add(i)
}
})
}

// Remove old imports
content = content.replace(
/import\s+(type\s+)?{[^}]+}\s+from\s+['"](@welcome-ui\/[^'"]+|welcome-ui)['"]\n?/g,
''
)

// Add merged imports at top
if (imports.size > 0) {
const mergedImport = `import { ${Array.from(imports).join(', ')} } from 'welcome-ui';\n`
content = mergedImport + content
}

if (typeImports.size > 0) {
const mergedTypeImport = `import type { ${Array.from(typeImports).join(', ')} } from 'welcome-ui';\n`
content = mergedTypeImport + content
}
.map(item => item.trim())
.map(item => (item === 'WuiTheme' ? 'ThemeValues' : item))
.join(', ')
return `${importStatement}{ ${transformedImports} } from`
})

// Transform other @welcome-ui imports
const importRegex = /from\s+['"](@welcome-ui\/[^'"]+)['"]/g
content = content.replace(importRegex, (match, importPath) => {
// Remove @ prefix and transform to UpperCamelCase
const newPath = importPath.replace('@welcome-ui/', 'welcome-ui/')
return `from '${transformToUpperCamelCase(newPath)}'`
})

return content
}
Expand All @@ -57,8 +52,11 @@ const files = glob.sync(pattern, {

files.forEach(file => {
const content = fs.readFileSync(file, 'utf8')
const newContent = mergeImports(content)
fs.writeFileSync(file, newContent)
// eslint-disable-next-line no-console
console.log(`Updated ${file}`)
const newContent = transformImports(content)

if (content !== newContent) {
fs.writeFileSync(file, newContent)
// eslint-disable-next-line no-console
console.log(`Updated ${file}`)
}
})
2 changes: 1 addition & 1 deletion website/build-app/pages/foundations/migration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ yarn add welcome-ui

You need to check packages you used and add missing peer dependencies.

### 4. Change imports
### 4. Other changes

1. `WuiTheme` becomes `ThemeValues`

0 comments on commit c75ef35

Please sign in to comment.