Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js): add --includeBabelRc flag for @nrwl/js:library generator (#8793, #8600) #10055

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' }]],
nartc marked this conversation as resolved.
Show resolved Hide resolved
};

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