Skip to content

Commit

Permalink
fix(js): ignore tsx and .test. files with buildable swc libraries (#9540
Browse files Browse the repository at this point in the history
)

include update to simplify existing patterns

ISSUES CLOSED: #9442

Co-authored-by: Caleb Ukle <caleb@Calebs-MBP-2.localdomain>
  • Loading branch information
2 people authored and FrozenPandaz committed Apr 13, 2022
1 parent a3b032c commit 0528b03
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/js/migrations.json
Expand Up @@ -11,6 +11,12 @@
"version": "13.8.5-beta.1",
"description": "Adjust .swcrc to .lib.swcrc",
"factory": "./src/migrations/update-13-8-5/update-swcrc"
},
"update-swcrc-exclude": {
"cli": "nx",
"version": "13.10.1-beta.1",
"description": "Update .lib.swcrc to exclude missing test files",
"factory": "./src/migrations/update-13-10-1/update-lib-swcrc-exclude"
}
},
"packageJsonUpdates": {}
Expand Down
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Update .lib.swcrc exclude should update the exclude pattern 1`] = `
"{
\\"jsc\\": {
\\"target\\": \\"es2017\\",
\\"parser\\": {
\\"syntax\\": \\"typescript\\",
\\"decorators\\": true,
\\"dynamicImport\\": true
},
\\"transform\\": {
\\"decoratorMetadata\\": true,
\\"legacyDecorator\\": true
},
\\"keepClassNames\\": true,
\\"externalHelpers\\": true,
\\"loose\\": true
},
\\"module\\": {
\\"type\\": \\"commonjs\\",
\\"strict\\": true,
\\"noInterop\\": true
},
\\"sourceMaps\\": true,
\\"exclude\\": [
\\".*.spec.tsx?$\\",
\\".*.test.tsx?$\\",
\\"./src/jest-setup.ts$\\",
\\"./**/jest-setup.ts$\\",
\\".*.js$\\"
]
}
"
`;
@@ -0,0 +1,90 @@
import {
addProjectConfiguration,
ProjectConfiguration,
readJson,
Tree,
updateJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import updateSwcRcExclude from './update-lib-swcrc-exclude';

const projectConfig: ProjectConfiguration = {
root: 'libs/swc-lib',
sourceRoot: 'libs/swc-lib/src',
targets: {
build: {
executor: '@nrwl/js:swc',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/libs/swc-lib',
main: 'libs/swc-lib/src/index.ts',
tsConfig: 'libs/swc-lib/tsconfig.lib.json',
assets: ['libs/swc-lib/*.md'],
},
},
},
};
const oldSwcRc = {
jsc: {
target: 'es2017',
parser: {
syntax: 'typescript',
decorators: true,
dynamicImport: true,
},
transform: {
decoratorMetadata: true,
legacyDecorator: true,
},
keepClassNames: true,
externalHelpers: true,
loose: true,
},
module: {
type: 'commonjs',
strict: true,
noInterop: true,
},
sourceMaps: true,
exclude: [
'./src/**/.*.spec.ts$',
'./**/.*.spec.ts$',
'./src/**/jest-setup.ts$',
'./**/jest-setup.ts$',
'./**/.*.js$',
],
};
describe('Update .lib.swcrc exclude', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
addProjectConfiguration(tree, 'swc-lib', projectConfig);

tree.write('libs/swc-lib/.lib.swcrc', JSON.stringify(oldSwcRc));
});

it('should update the exclude pattern', () => {
updateSwcRcExclude(tree);
expect(tree.read('libs/swc-lib/.lib.swcrc', 'utf-8')).toMatchSnapshot();
});

it('should NOT update the exclude pattern if not present', () => {
updateJson(tree, 'libs/swc-lib/.lib.swcrc', (json) => {
delete json.exclude;
return json;
});

const before = readJson(tree, 'libs/swc-lib/.lib.swcrc');
updateSwcRcExclude(tree);
const after = readJson(tree, 'libs/swc-lib/.lib.swcrc');

expect(after.exclude).toBeFalsy();
expect(after).toEqual(before);
});

it('should do nothing if .lib.swcrc doest not exist', () => {
tree.delete('libs/swc-lib/.lib-swcrc');

expect(() => updateSwcRcExclude(tree)).not.toThrowError();
});
});
@@ -0,0 +1,41 @@
import { readProjectConfiguration, Tree, updateJson } from '@nrwl/devkit';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
import { join } from 'path';
import { SwcExecutorOptions } from '../../utils/schema';
import { defaultExclude } from '../../utils/swc/add-swc-config';

export default function updateSwcRcExclude(tree: Tree) {
forEachExecutorOptions(
tree,
'@nrwl/js:swc',
(config: SwcExecutorOptions, projectName) => {
const projectConfig = readProjectConfiguration(tree, projectName);
const libSwcPath = join(projectConfig.root, '.lib.swcrc');

if (!tree.exists(libSwcPath)) return;

updateJson(
tree,
libSwcPath,
(json) => {
if (json.exclude) {
const excludePatterns = new Set([
...defaultExclude,
...json.exclude,
]);
// remove old patterns that are duplicate for new patterns
// defined in defaultExclude
excludePatterns.delete('./**/.*.spec.ts$');
excludePatterns.delete('./src/**/.*.spec.ts$');
excludePatterns.delete('./**/.*.js$');
excludePatterns.delete('./src/**/jest-setup.ts$');

json.exclude = [...excludePatterns];
}
return json;
},
{ expectComments: true }
);
}
);
}
8 changes: 4 additions & 4 deletions packages/js/src/utils/swc/add-swc-config.ts
Expand Up @@ -4,11 +4,11 @@ import { Tree } from '@nrwl/devkit';
import { join } from 'path';

export const defaultExclude = [
'./src/**/.*.spec.ts$',
'./**/.*.spec.ts$',
'./src/**/jest-setup.ts$',
'.*.spec.tsx?$',
'.*.test.tsx?$',
'./src/jest-setup.ts$',
'./**/jest-setup.ts$',
'./**/.*.js$',
'.*.js$',
];

const swcOptionsString = () => `{
Expand Down

0 comments on commit 0528b03

Please sign in to comment.