Skip to content

Commit

Permalink
fix(js): format .lib.swcrc file with nx format
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #10240
  • Loading branch information
nartc committed May 11, 2022
1 parent ecf88a6 commit d2d6a0b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lib --unit-test-runner jest --buildable should generate swcrc for swc 1`] = `
Object {
"overrides": Array [
Object {
"files": "**/*.swcrc",
"options": Object {
"parser": "json",
},
},
],
"singleQuote": true,
}
`;

exports[`lib --unit-test-runner jest should generate test configuration with swc and js 1`] = `
"/* eslint-disable */
const { readFileSync } = require('fs')
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ describe('lib', () => {
});

expect(tree.exists('libs/my-lib/.lib.swcrc')).toBeTruthy();
expect(readJson(tree, '.prettierrc')).toMatchSnapshot();
});

it('should setup jest project using swc', async () => {
Expand Down
43 changes: 41 additions & 2 deletions packages/js/src/utils/swc/add-swc-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO(chau): change back to 2015 when https://github.com/swc-project/swc/issues/1108 is solved
// target: 'es2015'
import { Tree } from '@nrwl/devkit';
import { logger, readJson, Tree, updateJson } from '@nrwl/devkit';
import { join } from 'path';

export const defaultExclude = [
Expand Down Expand Up @@ -39,7 +39,46 @@ const swcOptionsString = () => `{

export function addSwcConfig(tree: Tree, projectDir: string) {
const swcrcPath = join(projectDir, '.lib.swcrc');
updatePrettierRcForSwcrc(tree);
if (tree.exists(swcrcPath)) return;

tree.write(swcrcPath, swcOptionsString());
}

function updatePrettierRcForSwcrc(tree: Tree) {
const prettierrcPath = '.prettierrc';
const isExist = tree.exists(prettierrcPath);
if (!isExist) {
logger.info(`
For SWC, @nrwl/js:lib attempted to update Prettier Configuration for ".swcrc" file
but the root ".prettierrc" does not exist. Please consider adding parser support for ".swcrc" file
`);
return;
}

try {
const prettierRc = readJson(tree, prettierrcPath);
const hasSwcrcOverride = prettierRc.overrides?.some((override) => {
const files = Array.isArray(override.files)
? override.files
: [override.files];
return files.some((file) => file.includes('.swcrc'));
});

if (hasSwcrcOverride) return;
updateJson(tree, prettierrcPath, (json) => {
json.overrides = [
...(json.overrides ?? []),
{
files: '**/*.swcrc',
options: {
parser: 'json',
},
},
];

return json;
});
} catch (e) {
logger.error(e);
}
}
8 changes: 7 additions & 1 deletion packages/nx/src/command-line/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ async function getPatterns(
}

const p = parseFiles(args);

const supportedExtensions = prettier
.getSupportInfo()
.languages.flatMap((language) => language.extensions)
.filter((extension) => !!extension);
.filter((extension) => !!extension)
// Prettier supports ".swcrc" as a file instead of an extension
// So we add ".swcrc" as a supported extension manually
// which allows it to be considered for calculating "patterns"
.concat('.swcrc');

const patterns = p.files.filter(
(f) => fileExists(f) && supportedExtensions.includes(path.extname(f))
);
Expand Down

0 comments on commit d2d6a0b

Please sign in to comment.