Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afd99b2

Browse files
authored
fix(misc): allow --js flag to be passed to @nrwl/web:lib schematic (#3230)
* fix(misc): allow --js flag to be passed to @nrwl/web:lib schematic creates js files instead of ts files s ISSUES CLOSED: #2985 * fix(misc): add tests for --js flag in workspace
1 parent 4e06d83 commit afd99b2

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

docs/angular/api-workspace/schematics/library.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ Type: `string`
4848

4949
The library name used to import it, like @myorg/my-awesome-lib
5050

51+
### js
52+
53+
Default: `false`
54+
55+
Type: `boolean`
56+
57+
Generate JavaScript files rather than TypeScript files
58+
5159
### linter
5260

5361
Default: `eslint`

docs/react/api-workspace/schematics/library.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ Type: `string`
4848

4949
The library name used to import it, like @myorg/my-awesome-lib
5050

51+
### js
52+
53+
Default: `false`
54+
55+
Type: `boolean`
56+
57+
Generate JavaScript files rather than TypeScript files
58+
5159
### linter
5260

5361
Default: `eslint`

packages/workspace/src/schematics/library/library.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,43 @@ describe('lib', () => {
302302
expect.assertions(1);
303303
});
304304
});
305+
306+
describe('--js flag', () => {
307+
it('should generate js files instead of ts files', async () => {
308+
const tree = await runSchematic(
309+
'lib',
310+
{ name: 'myLib', js: true },
311+
appTree
312+
);
313+
expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy();
314+
expect(tree.exists('libs/my-lib/src/index.js')).toBeTruthy();
315+
expect(tree.exists('libs/my-lib/src/lib/my-lib.js')).toBeTruthy();
316+
});
317+
318+
it('should update root tsconfig.json with a js file path', async () => {
319+
const tree = await runSchematic(
320+
'lib',
321+
{ name: 'myLib', js: true },
322+
appTree
323+
);
324+
const tsconfigJson = readJsonInTree(tree, '/tsconfig.base.json');
325+
expect(tsconfigJson.compilerOptions.paths['@proj/my-lib']).toEqual([
326+
'libs/my-lib/src/index.js',
327+
]);
328+
});
329+
330+
it('should generate js files for nested libs as well', async () => {
331+
const tree = await runSchematic(
332+
'lib',
333+
{ name: 'myLib', directory: 'myDir', js: true },
334+
appTree
335+
);
336+
expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy();
337+
expect(tree.exists('libs/my-dir/my-lib/src/index.js')).toBeTruthy();
338+
expect(
339+
tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.js')
340+
).toBeTruthy();
341+
expect(tree.exists('libs/my-dir/my-lib/src/index.js')).toBeTruthy();
342+
});
343+
});
305344
});

packages/workspace/src/schematics/library/library.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import {
1515
import { join, normalize } from '@angular-devkit/core';
1616
import { Schema } from './schema';
1717

18-
import { NxJson, updateWorkspaceInTree, getNpmScope } from '@nrwl/workspace';
19-
import { updateJsonInTree, readJsonInTree } from '@nrwl/workspace';
18+
import { updateWorkspaceInTree, getNpmScope } from '@nrwl/workspace';
19+
import { updateJsonInTree } from '@nrwl/workspace';
2020
import { toFileName, names } from '@nrwl/workspace';
2121
import { formatFiles } from '@nrwl/workspace';
2222
import { offsetFromRoot } from '@nrwl/workspace';
23+
2324
import { generateProjectLint, addLintFiles } from '../../utils/lint';
2425
import { addProjectToNxJsonInTree, libsDir } from '../../utils/ast-utils';
2526
import { cliCommand } from '../../core/file-utils';
27+
import { toJS } from '../../utils/rules/to-js';
2628

2729
export interface NormalizedSchema extends Schema {
2830
name: string;
@@ -68,7 +70,10 @@ function updateTsConfig(options: NormalizedSchema): Rule {
6870
}
6971

7072
c.paths[options.importPath] = [
71-
`${libsDir(host)}/${options.projectDirectory}/src/index.ts`,
73+
maybeJs(
74+
options,
75+
`${libsDir(host)}/${options.projectDirectory}/src/index.ts`
76+
),
7277
];
7378

7479
return json;
@@ -87,6 +92,7 @@ function createFiles(options: NormalizedSchema): Rule {
8792
hasUnitTestRunner: options.unitTestRunner !== 'none',
8893
}),
8994
move(options.projectRoot),
95+
options.js ? toJS() : noop(),
9096
])
9197
);
9298
}
@@ -148,3 +154,9 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
148154
importPath,
149155
};
150156
}
157+
158+
function maybeJs(options: NormalizedSchema, path: string): string {
159+
return options.js && (path.endsWith('.ts') || path.endsWith('.tsx'))
160+
? path.replace(/\.tsx?$/, '.js')
161+
: path;
162+
}

packages/workspace/src/schematics/library/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export interface Schema {
1111
linter: Linter;
1212
testEnvironment: 'jsdom' | 'node';
1313
importPath?: string;
14+
js?: boolean;
1415
}

packages/workspace/src/schematics/library/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959
"importPath": {
6060
"type": "string",
6161
"description": "The library name used to import it, like @myorg/my-awesome-lib"
62+
},
63+
"js": {
64+
"type": "boolean",
65+
"description": "Generate JavaScript files rather than TypeScript files",
66+
"default": false
6267
}
6368
},
6469
"required": ["name"]

0 commit comments

Comments
 (0)