-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vite): Change config for mantine and chakra to use export default (…
- Loading branch information
Showing
10 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/codemods/src/codemods/v6.x.x/updateThemeConfig/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
``` | ||
npx @redwoodjs/codemods@canary update-theme-config | ||
``` | ||
# Update Theme Config | ||
Modifies the config files specifically for mantine and chakra-ui to use ESM syntax to export the theme. | ||
|
||
```diff | ||
// This is common JS lets get rid of it! | ||
- module.exports = {/**....your theme **/} | ||
+ const theme = {/**....your theme **/} | ||
+ export default theme | ||
``` |
3 changes: 3 additions & 0 deletions
3
packages/codemods/src/codemods/v6.x.x/updateThemeConfig/__testfixtures__/default.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This object will be used to override Mantine theme defaults. | ||
// See https://mantine.dev/theming/mantine-provider/#theme-object for theming options | ||
module.exports = {} |
4 changes: 4 additions & 0 deletions
4
packages/codemods/src/codemods/v6.x.x/updateThemeConfig/__testfixtures__/default.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This object will be used to override Mantine theme defaults. | ||
// See https://mantine.dev/theming/mantine-provider/#theme-object for theming options | ||
const theme = {} | ||
export default theme |
5 changes: 5 additions & 0 deletions
5
.../codemods/src/codemods/v6.x.x/updateThemeConfig/__testfixtures__/identifierTheme.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const customTheme = { | ||
bazinga: 'kittens' | ||
} | ||
|
||
module.exports = customTheme |
5 changes: 5 additions & 0 deletions
5
...codemods/src/codemods/v6.x.x/updateThemeConfig/__testfixtures__/identifierTheme.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const customTheme = { | ||
bazinga: 'kittens' | ||
} | ||
|
||
export default customTheme |
9 changes: 9 additions & 0 deletions
9
packages/codemods/src/codemods/v6.x.x/updateThemeConfig/__tests__/updateThemeConfig.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
describe('updateThemeConfig', () => { | ||
it('Converts from module.exports to export default ', async () => { | ||
await matchTransformSnapshot('updateThemeConfig', 'default') | ||
}) | ||
|
||
it('Handles when config is an identifier', async () => { | ||
await matchTransformSnapshot('updateThemeConfig', 'identifierTheme') | ||
}) | ||
}) |
49 changes: 49 additions & 0 deletions
49
packages/codemods/src/codemods/v6.x.x/updateThemeConfig/updateThemeConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { FileInfo, API } from 'jscodeshift' | ||
|
||
export default function transform(file: FileInfo, api: API) { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
// Find all module.exports assignments | ||
root | ||
.find(j.AssignmentExpression, { | ||
left: { | ||
type: 'MemberExpression', | ||
object: { type: 'Identifier', name: 'module' }, | ||
property: { type: 'Identifier', name: 'exports' }, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const configObject = path.value.right | ||
|
||
let themeObjectName = 'theme' | ||
|
||
if (j.Identifier.check(configObject)) { | ||
// If it already is an identifier, reuse it | ||
// modules.exports = theme -> export default theme | ||
// Note that export default statement is added outside this if statement | ||
themeObjectName = configObject.name | ||
|
||
// Remove module.exports assignment | ||
j(path).remove() | ||
} else { | ||
// Create const declaration with the exported object | ||
const declaration = j.variableDeclaration('const', [ | ||
j.variableDeclarator(j.identifier(themeObjectName), configObject), | ||
]) | ||
|
||
// Replace module.exports assignment with the const declaration | ||
// module.exports = {...} -> const theme = {...} | ||
j(path).replaceWith(declaration) | ||
} | ||
|
||
// Add export default statement | ||
const exportDefaultStatement = j.exportDefaultDeclaration( | ||
j.identifier(themeObjectName) | ||
) | ||
|
||
j(path.parentPath).insertAfter(exportDefaultStatement) | ||
}) | ||
|
||
return root.toSource() | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/codemods/src/codemods/v6.x.x/updateThemeConfig/updateThemeConfig.yargs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import path from 'path' | ||
|
||
import fg from 'fast-glob' | ||
import task, { TaskInnerAPI } from 'tasuku' | ||
|
||
import { getPaths } from '@redwoodjs/project-config' | ||
|
||
import runTransform from '../../../lib/runTransform' | ||
|
||
export const command = 'update-theme-config' | ||
export const description = | ||
'(v5.x.x->v6.x.x) Converts mantine and chakra UI configs to use ESM syntax' | ||
|
||
export const handler = () => { | ||
task('Update Theme Config', async ({ setOutput }: TaskInnerAPI) => { | ||
const targetPaths = fg.sync('{chakra,mantine}.config.{js,jsx,tsx,ts}', { | ||
cwd: getPaths().web.config, | ||
absolute: true, | ||
}) | ||
|
||
await runTransform({ | ||
transformPath: path.join(__dirname, 'updateThemeConfig.js'), | ||
targetPaths, | ||
}) | ||
|
||
setOutput('All done! Run `yarn rw lint --fix` to prettify your code') | ||
}) | ||
} |