forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) The legacy Node.js `global` object is still supported in Webpack for compatiblity reasons (src: https://webpack.js.org/api/module-variables/#global-nodejs), and there's a good chance that some projects are still using it because an old project of @Tobbe's was. Since this isn't supported in Vite, we should codemod it, especially since it's as simple as a find and replace which jscodeshift already has support for. Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/codemods/src/codemods/v6.x.x/changeGlobalToGlobalThis/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,5 @@ | ||
# Change Global To Global This | ||
|
||
The legacy Node.js `global` object is still supported in Webpack for compatiblity reasons (src: https://webpack.js.org/api/module-variables/#global-nodejs), and there's a good chance that some projects are still using it because an old project of @Tobbe's was. | ||
|
||
Since this isn't supported in Vite, we should codemod it, especially since it's as simple as a find and replace which jscodeshift already has support for. |
23 changes: 23 additions & 0 deletions
23
...s/codemods/src/codemods/v6.x.x/changeGlobalToGlobalThis/__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,23 @@ | ||
import { Link, routes } from '@redwoodjs/router' | ||
import { MetaTags } from '@redwoodjs/web' | ||
|
||
const HomePage = () => { | ||
console.log(global) | ||
|
||
return ( | ||
<> | ||
<MetaTags title="Home" description="Home page" /> | ||
|
||
<h1>HomePage</h1> | ||
<p> | ||
Find me in <code>./web/src/pages/HomePage/HomePage.tsx</code> | ||
</p> | ||
<p> | ||
My default route is named <code>home</code>, link to me with ` | ||
<Link to={routes.home()}>Home</Link>` | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
export default HomePage |
23 changes: 23 additions & 0 deletions
23
.../codemods/src/codemods/v6.x.x/changeGlobalToGlobalThis/__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,23 @@ | ||
import { Link, routes } from '@redwoodjs/router' | ||
import { MetaTags } from '@redwoodjs/web' | ||
|
||
const HomePage = () => { | ||
console.log(globalThis) | ||
|
||
return ( | ||
<> | ||
<MetaTags title="Home" description="Home page" /> | ||
|
||
<h1>HomePage</h1> | ||
<p> | ||
Find me in <code>./web/src/pages/HomePage/HomePage.tsx</code> | ||
</p> | ||
<p> | ||
My default route is named <code>home</code>, link to me with ` | ||
<Link to={routes.home()}>Home</Link>` | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
export default HomePage |
5 changes: 5 additions & 0 deletions
5
...s/src/codemods/v6.x.x/changeGlobalToGlobalThis/__tests__/changeGlobalToGlobalThis.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,5 @@ | ||
describe('changeGlobalToGlobalThis', () => { | ||
it('Converts global to globalThis', async () => { | ||
await matchTransformSnapshot('changeGlobalToGlobalThis', 'default') | ||
}) | ||
}) |
12 changes: 12 additions & 0 deletions
12
packages/codemods/src/codemods/v6.x.x/changeGlobalToGlobalThis/changeGlobalToGlobalThis.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,12 @@ | ||
import type { FileInfo, API } from 'jscodeshift' | ||
|
||
export default function transform(file: FileInfo, api: API) { | ||
const j = api.jscodeshift | ||
const ast = j(file.source) | ||
|
||
ast | ||
.find(j.Identifier, { name: 'global' }) | ||
.replaceWith(j.identifier('globalThis')) | ||
|
||
return ast.toSource() | ||
} |
25 changes: 25 additions & 0 deletions
25
...s/codemods/src/codemods/v6.x.x/changeGlobalToGlobalThis/changeGlobalToGlobalThis.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,25 @@ | ||
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 = 'change-global-to-global-this' | ||
export const description = '(v6.x.x->v6.x.x) Converts world to bazinga' | ||
|
||
export const handler = () => { | ||
task('Change Global To Global This', async ({ setOutput }: TaskInnerAPI) => { | ||
await runTransform({ | ||
transformPath: path.join(__dirname, 'changeGlobalToGlobalThis.js'), | ||
targetPaths: fg.sync('**/*.{js,jsx,tsx}', { | ||
cwd: getPaths().web.src, | ||
absolute: true, | ||
}), | ||
}) | ||
|
||
setOutput('All done! Run `yarn rw lint --fix` to prettify your code') | ||
}) | ||
} |