Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): fix external option for @emotion/react (#7870)
- Loading branch information
Showing
5 changed files
with
124 additions
and
3 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
64 changes: 64 additions & 0 deletions
64
packages/react/src/migrations/update-14-1-0/update-external-emotion-jsx-runtime.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,64 @@ | ||
import { | ||
addProjectConfiguration, | ||
readProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
|
||
import { updateExternalEmotionJsxRuntime } from './update-external-emotion-jsx-runtime'; | ||
|
||
describe('updateExternalEmotionJsxRuntime', () => { | ||
it('should update external for Emotion', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(2); | ||
addProjectConfiguration(tree, 'components', { | ||
root: 'libs/components', | ||
targets: { | ||
build: { | ||
executor: '@nrwl/web:rollup', | ||
options: { | ||
external: ['@emotion/styled/base', 'react/jsx-runtime'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
tree.write( | ||
'libs/components/.babelrc', | ||
JSON.stringify({ | ||
presets: [ | ||
[ | ||
'@nrwl/react/babel', | ||
{ | ||
runtime: 'automatic', | ||
importSource: '@emotion/react', | ||
}, | ||
], | ||
], | ||
plugins: ['@emotion/babel-plugin'], | ||
}) | ||
); | ||
|
||
// ACT | ||
await updateExternalEmotionJsxRuntime(tree); | ||
|
||
// ASSERT | ||
const { targets } = readProjectConfiguration(tree, 'components'); | ||
expect(targets.build.options.external).toEqual([ | ||
'@emotion/react/jsx-runtime', | ||
]); | ||
}); | ||
|
||
it('should not fail for projects with no targets', async () => { | ||
// ARRANGE | ||
const tree = createTreeWithEmptyWorkspace(2); | ||
addProjectConfiguration(tree, 'components', { | ||
root: 'apps/components', | ||
}); | ||
|
||
// ACT | ||
await updateExternalEmotionJsxRuntime(tree); | ||
|
||
// ASSERT | ||
const { targets } = readProjectConfiguration(tree, 'components'); | ||
expect(targets).toBeUndefined(); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/react/src/migrations/update-14-1-0/update-external-emotion-jsx-runtime.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,50 @@ | ||
import { | ||
Tree, | ||
readProjectConfiguration, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { WebRollupOptions } from '@nrwl/web/src/executors/rollup/schema'; | ||
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; | ||
|
||
export async function updateExternalEmotionJsxRuntime(tree: Tree) { | ||
forEachExecutorOptions<WebRollupOptions>( | ||
tree, | ||
'@nrwl/web:rollup', | ||
(options: any, projectName, targetName, configurationName) => { | ||
const projectConfiguration = readProjectConfiguration(tree, projectName); | ||
const config = configurationName | ||
? projectConfiguration.targets[targetName].configurations[ | ||
configurationName | ||
] | ||
: projectConfiguration.targets[targetName].options; | ||
|
||
if (config.external && config.external.length > 0) { | ||
const hasEmotionStyledBase = config.external.includes( | ||
'@emotion/styled/base' | ||
); | ||
const hasReactJsxRuntime = | ||
config.external.includes('react/jsx-runtime'); | ||
|
||
if (hasEmotionStyledBase && hasReactJsxRuntime) { | ||
// Replace 'react/jsx-runtime' with '@emotion/react/jsx-runtime' | ||
config.external.forEach((value, index) => { | ||
if (value === 'react/jsx-runtime') { | ||
config.external.splice(index, 1, '@emotion/react/jsx-runtime'); | ||
} | ||
}); | ||
|
||
// Remove '@emotion/styled/base' | ||
config.external.forEach((value, index) => { | ||
if (value === '@emotion/styled/base') { | ||
config.external.splice(index, 1); | ||
} | ||
}); | ||
} | ||
|
||
updateProjectConfiguration(tree, projectName, projectConfiguration); | ||
} | ||
} | ||
); | ||
} | ||
|
||
export default updateExternalEmotionJsxRuntime; |