-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented the codemod along with some tests
- Loading branch information
Showing
13 changed files
with
164 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ dist/ | |
lib/ | ||
build/ | ||
/website/.cache/ | ||
/website/public/ | ||
/website/public/ | ||
__testfixtures__/ |
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ CHANGELOG.md | |
package.json | ||
lerna.json | ||
/website/.cache/ | ||
/website/public/ | ||
/website/public/ | ||
__testfixtures__/ |
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,11 @@ | ||
# @loadable/codemod | ||
|
||
This package is a collection of codemod that can be used to help making big changes easier to a project, for example: migrating from `react-loadable` to `@loadable/component` | ||
|
||
## Notes about `react-loadable-to-loadable-component` transform | ||
`react-loadable-to-loadable-component` transform will help codemod all of your `Loadable()` declaration to `loadable()` with mostly equivalent params, barring some behavior that do not exist in `@loadable/component` such as `Loadable.Map()`, `timeout`, `delay`, etc. | ||
|
||
After running the codemod, you will still need to update some of your code manually, namely: | ||
1. Using `loadableReady` to hydrate your app on the client side. | ||
2. Updating your webpack configuration to use `@loadable` | ||
3. Updating your server side rendering code to use `ChunkExtractor` |
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 |
---|---|---|
@@ -1,68 +1,70 @@ | ||
#!/usr/bin/env node | ||
|
||
/* eslint-disable no-console */ | ||
const yargs = require('yargs'); | ||
const execa = require('execa'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const CodemodError = require('./utils/CodemodError'); | ||
const yargs = require('yargs') | ||
const execa = require('execa') | ||
const path = require('path') | ||
const fs = require('fs') | ||
const CodemodError = require('./utils/CodemodError') | ||
|
||
const jscodeshiftExecutable = require.resolve('.bin/jscodeshift'); | ||
const transformsDir = path.resolve(__dirname, '../transforms'); | ||
const jscodeshiftExecutable = require.resolve('.bin/jscodeshift') | ||
const transformsDir = path.resolve(__dirname, '../transforms') | ||
|
||
const { argv } = yargs; | ||
const { argv } = yargs | ||
|
||
try { | ||
const selectedCodemod = argv._[0]; | ||
const directoryToApplyTo = argv._[1]; | ||
const selectedCodemod = argv._[0] | ||
const directoryToApplyTo = argv._[1] | ||
|
||
if (!selectedCodemod || !directoryToApplyTo) { | ||
throw new CodemodError({ | ||
type: 'Invalid params' | ||
}); | ||
type: 'Invalid params', | ||
}) | ||
} | ||
|
||
const availableTransforms = fs.readdirSync(transformsDir); | ||
const availableTransforms = fs | ||
.readdirSync(transformsDir) | ||
.filter(v => v !== '__tests__' && v !== '__testfixtures__') | ||
.map(v => v.replace('.js', '')) | ||
|
||
if (!availableTransforms.some(t => t === selectedCodemod)) { | ||
throw new CodemodError({ | ||
type: 'Unrecognised transform', | ||
payload: selectedCodemod, | ||
}); | ||
}) | ||
} | ||
/** | ||
* We need to run the following | ||
* | ||
* jscodeshift --parser babylon -t ./packages/lite-codegen/codemod/upgrade-loadable-component/jscodeshift.js | ||
*/ | ||
|
||
const result = execa.sync(jscodeshiftExecutable, [ | ||
'--parser babylon', | ||
`-t ${transformsDir}/${selectedCodemod}/index.js`, | ||
], { | ||
stdio: 'inherit', | ||
stripEof: false | ||
}); | ||
const result = execa.sync( | ||
jscodeshiftExecutable, | ||
['--parser babylon', `-t ${transformsDir}/${selectedCodemod}.js`], | ||
{ | ||
stdio: 'inherit', | ||
stripEof: false, | ||
}, | ||
) | ||
|
||
if (result.error) { | ||
throw result.error; | ||
throw result.error | ||
} | ||
|
||
} catch (err) { | ||
if (err.type === 'Invalid params') { | ||
console.error('Invalid params passed!'); | ||
console.error('@loadable/codemod requires 2 params to be passed, the name of the codemod, and a directory to apply the codemod to.'); | ||
console.error('Example: npx run @loadable/codemod react-loadable-to-loadable-component ./src/client'); | ||
console.error('Invalid params passed!') | ||
console.error( | ||
'@loadable/codemod requires 2 params to be passed, the name of the codemod, and a directory to apply the codemod to.', | ||
) | ||
console.error( | ||
'Example: npx @loadable/codemod react-loadable-to-loadable-component ./src/client', | ||
) | ||
|
||
process.exit(1); | ||
} | ||
process.exit(1) | ||
} | ||
|
||
if (err.type === 'Unrecognised transform') { | ||
console.error(`Unrecognised transform passed: '${err.payload}'`); | ||
console.error(`Unrecognised transform passed: '${err.payload}'`) | ||
|
||
process.exit(1); | ||
} | ||
process.exit(1) | ||
} | ||
|
||
// For other errors, just re-throw it | ||
throw err; | ||
throw err | ||
} |
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"repository": "[email protected]:smooth-code/loadable-components.git", | ||
"author": "Jacky Efendi <[email protected]>", | ||
"bin": { | ||
"@loadable/codemod": "./bin/main.js" | ||
"loadable-codemod": "./bin/main.js" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
|
9 changes: 9 additions & 0 deletions
9
...transforms/__testfixtures__/react-loadable-to-loadable-component_arrow-no-params.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,9 @@ | ||
/* eslint-disable */ | ||
import Loadable from 'react-loadable' | ||
|
||
const CustomLinkLoadable = Loadable({ | ||
loader: () => | ||
import(/* webpackChunkName: "custom-link" */ '@components/CustomLink/Link'), | ||
loading: () => <div>loading...</div>, | ||
delay: 0, | ||
}) |
7 changes: 7 additions & 0 deletions
7
...ransforms/__testfixtures__/react-loadable-to-loadable-component_arrow-no-params.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,7 @@ | ||
/* eslint-disable */ | ||
import loadable from '@loadable/component' | ||
|
||
const CustomLinkLoadable = loadable(() => | ||
import(/* webpackChunkName: "custom-link" */ '@components/CustomLink/Link'), { | ||
fallback: (() => <div>loading...</div>)(), | ||
}) |
15 changes: 15 additions & 0 deletions
15
.../transforms/__testfixtures__/react-loadable-to-loadable-component_arrow-w-params.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,15 @@ | ||
/* eslint-disable */ | ||
import Loadable from 'react-loadable' | ||
|
||
const CustomLinkLoadable = Loadable({ | ||
loader: () => | ||
import(/* webpackChunkName: "custom-link" */ '@components/CustomLink/Link'), | ||
loading: (props) => { | ||
if (props.error || props.timedOut) { | ||
throw new Error('Failed to load custom link chunk') | ||
} else if (props.loading) { | ||
return <div>loading...</div>; | ||
} | ||
}, | ||
delay: 0, | ||
}) |
7 changes: 7 additions & 0 deletions
7
...transforms/__testfixtures__/react-loadable-to-loadable-component_arrow-w-params.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,7 @@ | ||
/* eslint-disable */ | ||
import loadable from '@loadable/component' | ||
|
||
const CustomLinkLoadable = loadable(() => | ||
import(/* webpackChunkName: "custom-link" */ '@components/CustomLink/Link'), { | ||
fallback: null, | ||
}) |
17 changes: 17 additions & 0 deletions
17
...es/codemod/transforms/__testfixtures__/react-loadable-to-loadable-component_expr.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,17 @@ | ||
/* eslint-disable */ | ||
import Loadable from 'react-loadable' | ||
|
||
const Loading = props => { | ||
if (props.error || props.timedOut) { | ||
throw new Error('Failed to load custom link chunk') | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
const CustomLinkLoadable = Loadable({ | ||
loader: () => | ||
import(/* webpackChunkName: "custom-link" */ '@components/CustomLink/Link'), | ||
loading: Loading, | ||
delay: 0, | ||
}) |
15 changes: 15 additions & 0 deletions
15
...s/codemod/transforms/__testfixtures__/react-loadable-to-loadable-component_expr.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,15 @@ | ||
/* eslint-disable */ | ||
import loadable from '@loadable/component' | ||
|
||
const Loading = props => { | ||
if (props.error || props.timedOut) { | ||
throw new Error('Failed to load custom link chunk') | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
const CustomLinkLoadable = loadable(() => | ||
import(/* webpackChunkName: "custom-link" */ '@components/CustomLink/Link'), { | ||
fallback: null, | ||
}) |
7 changes: 7 additions & 0 deletions
7
packages/codemod/transforms/__tests__/react-loadable-to-loadable-component-test.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,7 @@ | ||
jest.autoMockOff(); | ||
|
||
const { defineTest } = require('jscodeshift/dist/testUtils'); | ||
|
||
defineTest(__dirname, 'react-loadable-to-loadable-component', null, 'react-loadable-to-loadable-component_expr'); | ||
defineTest(__dirname, 'react-loadable-to-loadable-component', null, 'react-loadable-to-loadable-component_arrow-no-params'); | ||
defineTest(__dirname, 'react-loadable-to-loadable-component', null, 'react-loadable-to-loadable-component_arrow-w-params'); |
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