Skip to content

Commit

Permalink
fix(react): fix external option for @emotion/react (#7870)
Browse files Browse the repository at this point in the history
  • Loading branch information
puku0x committed May 30, 2022
1 parent 6793e14 commit a2c22eb
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/react/migrations.json
Expand Up @@ -53,6 +53,11 @@
"version": "14.0.0-beta.0",
"description": "Add a default development configuration for build and serve targets.",
"factory": "./src/migrations/update-14-0-0/add-default-development-configurations"
},
"update-external-emotion-jsx-runtime-14.1.0": {
"version": "14.1.0-beta.0",
"description": "Update external option in projects for Emotion",
"factory": "./src/migrations/update-14-1-0/update-external-emotion-jsx-runtime"
}
},
"packageJsonUpdates": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/generators/library/library.spec.ts
Expand Up @@ -560,7 +560,7 @@ describe('lib', () => {

expect(workspaceJson.projects['my-lib'].architect.build).toMatchObject({
options: {
external: ['react/jsx-runtime', '@emotion/styled/base'],
external: ['@emotion/react/jsx-runtime'],
},
});
expect(babelrc.plugins).toEqual(['@emotion/babel-plugin']);
Expand Down
6 changes: 4 additions & 2 deletions packages/react/src/generators/library/library.ts
Expand Up @@ -179,10 +179,12 @@ function addProject(host: Tree, options: NormalizedSchema) {

if (options.publishable || options.buildable) {
const { libsDir } = getWorkspaceLayout(host);
const external = ['react/jsx-runtime'];
const external: string[] = [];

if (options.style === '@emotion/styled') {
external.push('@emotion/styled/base');
external.push('@emotion/react/jsx-runtime');
} else {
external.push('react/jsx-runtime');
}

targets.build = {
Expand Down
@@ -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();
});
});
@@ -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;

0 comments on commit a2c22eb

Please sign in to comment.