Skip to content

Commit

Permalink
fix(js): add swc/helpers to js packages on converting to swc (#9467)
Browse files Browse the repository at this point in the history
* fix(js): add swc/helpers to js packages on converting to swc

* chore(js): update convert to swc unit test

* chore(js): format

Co-authored-by: Chau Tran <ctran@Chaus-MacBook-Pro.local>
  • Loading branch information
2 people authored and FrozenPandaz committed Mar 25, 2022
1 parent 01f7e00 commit 1e978f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Expand Up @@ -47,5 +47,8 @@ describe('convert to swc', () => {
)
).toEqual(true);
expect(tree.read('package.json', 'utf-8')).toContain('@swc/core');
expect(tree.read('libs/tsc-lib/package.json', 'utf-8')).toContain(
'@swc/helpers'
);
});
});
26 changes: 24 additions & 2 deletions packages/js/src/generators/convert-to-swc/convert-to-swc.ts
@@ -1,4 +1,5 @@
import {
addDependenciesToPackageJson,
convertNxGenerator,
installPackagesTask,
ProjectConfiguration,
Expand All @@ -10,6 +11,7 @@ import {
import { join } from 'path';
import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { addSwcDependencies } from '../../utils/swc/add-swc-dependencies';
import { swcHelpersVersion } from '../../utils/versions';
import { ConvertToSwcGeneratorSchema } from './schema';

export async function convertToSwcGenerator(
Expand All @@ -25,7 +27,6 @@ export async function convertToSwcGenerator(
options.project,
options.targets
);

return checkSwcDependencies(tree, projectConfiguration);
}

Expand Down Expand Up @@ -66,10 +67,20 @@ function checkSwcDependencies(
);

const packageJson = readJson(tree, 'package.json');
const projectPackageJsonPath = join(
projectConfiguration.root,
'package.json'
);
const projectPackageJson = readJson(tree, projectPackageJsonPath);

const hasSwcDependency =
packageJson.dependencies && packageJson.dependencies['@swc/core'];

if (isSwcrcPresent && hasSwcDependency) return;
const hasSwcHelpers =
projectPackageJson.dependencies &&
projectPackageJson.dependencies['@swc/helpers'];

if (isSwcrcPresent && hasSwcDependency && hasSwcHelpers) return;

if (!isSwcrcPresent) {
addSwcConfig(tree, projectConfiguration.root);
Expand All @@ -79,6 +90,17 @@ function checkSwcDependencies(
addSwcDependencies(tree);
}

if (!hasSwcHelpers) {
addDependenciesToPackageJson(
tree,
{
'@swc/helpers': swcHelpersVersion,
},
{},
projectPackageJsonPath
);
}

return () => {
if (!hasSwcDependency) {
installPackagesTask(tree);
Expand Down

0 comments on commit 1e978f4

Please sign in to comment.