feat(react): add strict option to react application generator (#5248)

This commit is contained in:
Noriyuki Shinpuku 2021-05-03 11:01:43 +09:00 committed by GitHub
parent 111a7cfeaf
commit 6cb1dc65d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 121 additions and 8 deletions

View File

@ -142,6 +142,14 @@ Type: `boolean`
Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style). Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style).
### strict
Default: `false`
Type: `boolean`
Creates an application with stricter type checking and build optimization options.
### style ### style
Alias(es): s Alias(es): s

View File

@ -142,6 +142,14 @@ Type: `boolean`
Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style). Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style).
### strict
Default: `false`
Type: `boolean`
Creates an application with stricter type checking and build optimization options.
### style ### style
Alias(es): s Alias(es): s

View File

@ -142,6 +142,14 @@ Type: `boolean`
Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style). Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style).
### strict
Default: `false`
Type: `boolean`
Creates an application with stricter type checking and build optimization options.
### style ### style
Alias(es): s Alias(es): s

View File

@ -20,6 +20,7 @@ describe('app', () => {
name: 'myApp', name: 'myApp',
linter: Linter.EsLint, linter: Linter.EsLint,
style: 'css', style: 'css',
strict: false,
}; };
beforeEach(() => { beforeEach(() => {
@ -75,6 +76,14 @@ describe('app', () => {
path: './tsconfig.spec.json', path: './tsconfig.spec.json',
}, },
]); ]);
expect(tsconfig.compilerOptions.strict).not.toBeDefined();
expect(
tsconfig.compilerOptions.forceConsistentCasingInFileNames
).not.toBeDefined();
expect(tsconfig.compilerOptions.noImplicitReturns).not.toBeDefined();
expect(
tsconfig.compilerOptions.noFallthroughCasesInSwitch
).not.toBeDefined();
const tsconfigApp = JSON.parse( const tsconfigApp = JSON.parse(
stripJsonComments( stripJsonComments(
@ -669,4 +678,40 @@ describe('app', () => {
expect(appTree.exists('/apps/my-app/src/main.js')).toBe(true); expect(appTree.exists('/apps/my-app/src/main.js')).toBe(true);
}); });
}); });
describe('--strict', () => {
it('should update tsconfig.json', async () => {
await applicationGenerator(appTree, {
...schema,
strict: true,
});
const tsconfigJson = readJson(appTree, '/apps/my-app/tsconfig.json');
expect(tsconfigJson.compilerOptions.strict).toBeTruthy();
expect(
tsconfigJson.compilerOptions.forceConsistentCasingInFileNames
).toBeTruthy();
expect(tsconfigJson.compilerOptions.noImplicitReturns).toBeTruthy();
expect(
tsconfigJson.compilerOptions.noFallthroughCasesInSwitch
).toBeTruthy();
});
it('should update budgets in workspace.json', async () => {
await applicationGenerator(appTree, {
...schema,
strict: true,
});
const workspaceJson = getProjects(appTree);
const targetConfig = workspaceJson.get('my-app').targets;
expect(targetConfig.build.configurations.production.budgets).toEqual([
{
type: 'initial',
maximumWarning: '500kb',
maximumError: '1mb',
},
]);
});
});
}); });

View File

@ -87,13 +87,21 @@ function createBuildTarget(options: NormalizedSchema): TargetConfiguration {
namedChunks: false, namedChunks: false,
extractLicenses: true, extractLicenses: true,
vendorChunk: false, vendorChunk: false,
budgets: [ budgets: options.strict
{ ? [
type: 'initial', {
maximumWarning: '2mb', type: 'initial',
maximumError: '5mb', maximumWarning: '500kb',
}, maximumError: '1mb',
], },
]
: [
{
type: 'initial',
maximumWarning: '2mb',
maximumError: '5mb',
},
],
}, },
}, },
}; };

View File

@ -1,7 +1,35 @@
import { NormalizedSchema } from '../schema'; import { NormalizedSchema } from '../schema';
import { names, offsetFromRoot, Tree, toJS, generateFiles } from '@nrwl/devkit'; import {
names,
offsetFromRoot,
Tree,
toJS,
generateFiles,
joinPathFragments,
updateJson,
} from '@nrwl/devkit';
import { join } from 'path'; import { join } from 'path';
function updateTsConfig(host: Tree, options: NormalizedSchema) {
updateJson(
host,
joinPathFragments(options.appProjectRoot, 'tsconfig.json'),
(json) => {
if (options.strict) {
json.compilerOptions = {
...json.compilerOptions,
forceConsistentCasingInFileNames: true,
strict: true,
noImplicitReturns: true,
noFallthroughCasesInSwitch: true,
};
}
return json;
}
);
}
export function createApplicationFiles(host: Tree, options: NormalizedSchema) { export function createApplicationFiles(host: Tree, options: NormalizedSchema) {
let styleSolutionSpecificAppFiles: string; let styleSolutionSpecificAppFiles: string;
if (options.styledModule && options.style !== 'styled-jsx') { if (options.styledModule && options.style !== 'styled-jsx') {
@ -45,4 +73,6 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) {
if (options.js) { if (options.js) {
toJS(host); toJS(host);
} }
updateTsConfig(host, options);
} }

View File

@ -17,6 +17,7 @@ export interface Schema {
skipWorkspaceJson?: boolean; skipWorkspaceJson?: boolean;
js?: boolean; js?: boolean;
globalCss?: boolean; globalCss?: boolean;
strict?: boolean;
} }
export interface NormalizedSchema extends Schema { export interface NormalizedSchema extends Schema {

View File

@ -135,6 +135,11 @@
"type": "boolean", "type": "boolean",
"description": "Default is false. When true, the component is generated with *.css/*.scss instead of *.module.css/*.module.scss", "description": "Default is false. When true, the component is generated with *.css/*.scss instead of *.module.css/*.module.scss",
"default": false "default": false
},
"strict": {
"type": "boolean",
"description": "Creates an application with stricter type checking and build optimization options.",
"default": false
} }
}, },
"required": [] "required": []