-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): add typings to types instead of files
- Loading branch information
1 parent
543e9e3
commit 1d2bf95
Showing
10 changed files
with
140 additions
and
49 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
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
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
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
48 changes: 48 additions & 0 deletions
48
packages/react/src/migrations/update-16-7-0-add-typings/update-16-7-0-add-typings.spec.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,48 @@ | ||
import { Tree, addProjectConfiguration } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports'; | ||
import addTypings from './update-16-7-0-add-typings'; | ||
|
||
describe('Add typings to react projects', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); | ||
}); | ||
|
||
it('should update tsconfig to include react typings', async () => { | ||
tree.write( | ||
'package.json', | ||
JSON.stringify({ | ||
dependencies: {}, | ||
devDependencies: {}, | ||
}) | ||
); | ||
|
||
addProjectConfiguration(tree, 'myapp', { | ||
root: 'myapp', | ||
targets: { | ||
build: { | ||
executor: '@nx/webpack:webpack', | ||
}, | ||
}, | ||
}); | ||
tree.write( | ||
'myapp/tsconfig.app.json', | ||
JSON.stringify({ | ||
compilerOptions: {}, | ||
}) | ||
); | ||
|
||
await addTypings(tree); | ||
const tsconfigTypes = JSON.parse( | ||
tree.read('myapp/tsconfig.app.json', 'utf-8') | ||
); | ||
|
||
expect(tsconfigTypes.compilerOptions.types).toContain( | ||
'@nx/react/typings/cssmodule.d.ts' | ||
); | ||
expect(tsconfigTypes.compilerOptions.types).toContain( | ||
'@nx/react/typings/image.d.ts' | ||
); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/react/src/migrations/update-16-7-0-add-typings/update-16-7-0-add-typings.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,51 @@ | ||
import { | ||
Tree, | ||
formatFiles, | ||
getProjects, | ||
joinPathFragments, | ||
updateJson, | ||
} from '@nx/devkit'; | ||
|
||
export default async function addTypings(tree: Tree) { | ||
const projects = getProjects(tree); | ||
const buildExecutors = [ | ||
'@nx/webpack:webpack', | ||
'@nx/vite:build', | ||
'@nx/rspack:rspack', | ||
]; | ||
const relatedTsConfigs = [ | ||
'tsconfig.app.json', | ||
'tsconfig.lib.json', | ||
'tsconfig.spec.json', | ||
]; | ||
|
||
const typesToAdd = [ | ||
'@nx/react/typings/cssmodule.d.ts', | ||
'@nx/react/typings/image.d.ts', | ||
]; | ||
|
||
for (const [, config] of projects) { | ||
if (buildExecutors.includes(config?.targets?.build?.executor)) { | ||
const rootPath = config.root; | ||
relatedTsConfigs.forEach((tsConfig) => { | ||
const tsConfigPath = joinPathFragments(rootPath, tsConfig); | ||
if (tree.exists(tsConfigPath)) { | ||
updateJson(tree, tsConfigPath, (json) => { | ||
const compilerOptions = json.compilerOptions || {}; | ||
compilerOptions.types = [ | ||
...new Set([...(compilerOptions.types || []), ...typesToAdd]), | ||
]; | ||
if (json.files?.length > 0) { | ||
json.files = json.files.filter( | ||
(file: string) => | ||
!['cssmodule.d.ts', 'image.d.ts'].includes(file) | ||
); | ||
} | ||
return { ...json, compilerOptions }; | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
await formatFiles(tree); | ||
} |