Skip to content

Commit

Permalink
fix(js): fix(js): adding missing babelrc for tsc compiled libraries
Browse files Browse the repository at this point in the history
ISSUES CLOSED: nrwl#8600, nrwl#8793

ISSUES CLOSED: nrwl#8600, nrwl#8793
  • Loading branch information
Daniele committed Apr 29, 2022
1 parent fa81e3b commit 5c5eb4d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions e2e/js/src/js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ describe('js e2e', () => {
'match the cache'
);

const babelRc = readJson(`libs/${lib}/.babelrc`);
expect(babelRc.plugins).toBeUndefined();
expect(babelRc.presets).toStrictEqual([
['@nrwl/web/babel', { useBuiltIns: 'usage' }],
]);

expect(runCLI(`build ${lib}`)).toContain('Done compiling TypeScript files');
checkFilesExist(
`dist/libs/${lib}/README.md`,
Expand Down
33 changes: 33 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ describe('lib', () => {
`);
});

it('should create a local .babelrc configuration', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'myLib',
});

const babelRc = readJson(tree, 'libs/my-lib/.babelrc');
expect(babelRc).toMatchInlineSnapshot(`
Object {
"presets": Array [
Array [
"@nrwl/web/babel",
Object {
"useBuiltIns": "usage",
},
],
],
}
`);
});

it('should extend from root tsconfig.json when no tsconfig.base.json', async () => {
tree.rename('tsconfig.base.json', 'tsconfig.json');

Expand Down Expand Up @@ -222,6 +243,7 @@ describe('lib', () => {
expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy();
expect(tree.exists(`libs/my-dir/my-lib/.eslintrc.json`)).toBeTruthy();
expect(tree.exists(`libs/my-dir/my-lib/package.json`)).toBeFalsy();
expect(tree.exists(`libs/my-dir/my-lib/.babelrc`)).toBeTruthy();
});

it('should update workspace.json', async () => {
Expand Down Expand Up @@ -771,6 +793,17 @@ describe('lib', () => {
expect(tree.exists('libs/my-lib/.lib.swcrc')).toBeTruthy();
});

it('should not generate a .babelrc for swc', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'myLib',
buildable: true,
compiler: 'swc',
});

expect(tree.exists('libs/my-lib/.babelrc')).toBeFalsy();
});

it('should setup jest project using swc', async () => {
await libraryGenerator(tree, {
...defaultOptions,
Expand Down
16 changes: 16 additions & 0 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ function updateTsConfig(tree: Tree, options: NormalizedSchema) {
});
}

function addBabelConfig(tree: Tree, options: NormalizedSchema) {
const filename = '.babelrc';
const babelrc = {
presets: [['@nrwl/web/babel', { useBuiltIns: 'usage' }]],
};

tree.write(
join(options.projectRoot, filename),
JSON.stringify(babelrc, null, 2)
);
}

function createFiles(tree: Tree, options: NormalizedSchema, filesDir: string) {
const { className, name, propertyName } = names(options.name);

Expand All @@ -201,6 +213,10 @@ function createFiles(tree: Tree, options: NormalizedSchema, filesDir: string) {
addSwcConfig(tree, options.projectRoot);
}

if (options.compiler !== 'swc') {
addBabelConfig(tree, options);
}

if (options.unitTestRunner === 'none') {
tree.delete(
join(options.projectRoot, 'src/lib', `${options.fileName}.spec.ts`)
Expand Down

0 comments on commit 5c5eb4d

Please sign in to comment.