mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-25 16:57:51 +08:00
With this change we enable stricter type checking and optimization effective coding rules when using the `--strict` option. Changes in schematics - `ng-new`: A prompt for the `--strict` option was added. This option is a proxy and will be passed to the application and workspace schematics. - `application`: A `package.json` was added in the `app` folder, to tell the bundlers whether the application is free from side-effect code. When `strict` is `true`. the `sideEffects` will be set `false`. - `workspace` When `strict` is true, we add stricter TypeScript and Angular type-checking options. Note: AIO is already using these strict TypeScript compiler settings. PR to enable `strictTemplates` https://github.com/angular/angular/pull/36391 Reference: TOOL-1366
46 lines
2.0 KiB
TypeScript
46 lines
2.0 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import { join, normalize, strings } from '@angular-devkit/core';
|
|
import { Rule } from '@angular-devkit/schematics';
|
|
import { getWorkspace } from '../../utility/workspace';
|
|
import { ProjectType } from '../../utility/workspace-models';
|
|
|
|
export default function (): Rule {
|
|
return async (host, context) => {
|
|
const workspace = await getWorkspace(host);
|
|
const logger = context.logger;
|
|
|
|
for (const [projectName, project] of workspace.projects) {
|
|
if (project.extensions.projectType !== ProjectType.Application) {
|
|
// Only interested in application projects
|
|
continue;
|
|
}
|
|
|
|
const appDir = join(normalize(project.sourceRoot || ''), 'app');
|
|
const { subdirs, subfiles } = host.getDir(appDir);
|
|
if (!subdirs.length && !subfiles.length) {
|
|
logger.error(`Application directory '${appDir}' for project '${projectName}' doesn't exist.`);
|
|
continue;
|
|
}
|
|
|
|
const pkgJson = join(appDir, 'package.json');
|
|
if (!host.exists(pkgJson)) {
|
|
const pkgJsonContent = {
|
|
name: strings.dasherize(projectName),
|
|
private: true,
|
|
description: `This is a special package.json file that is not used by package managers. It is however used to tell the tools and bundlers whether the code under this directory is free of code with non-local side-effect. Any code that does have non-local side-effects can't be well optimized (tree-shaken) and will result in unnecessary increased payload size. It should be safe to set this option to 'false' for new applications, but existing code bases could be broken when built with the production config if the application code does contain non-local side-effects that the application depends on.`,
|
|
sideEffects: true,
|
|
};
|
|
|
|
host.create(pkgJson, JSON.stringify(pkgJsonContent, undefined, 2));
|
|
}
|
|
}
|
|
};
|
|
}
|