fix(localize): install `@angular/localize` in `devDependencies` by default (#38680)
Previously this package was installed in the default `dependencies` section of `package.json`, but this meant that its own dependencies are treated as dependencies of the main project: Babel, for example. Generally, $localize` is not used at runtime - it is compiled out by the translation tooling, so there is no need for it to be a full dependency. In fact, even if it is used at runtime, the package itself is only used at dev-time since the runtime bits will be bundled into a distributable. So putting this package in `devDependencies` would only prevent libraries from bringing the package into application projects that used them. This is probably good in itself, since it should be up to the downstream project to decide if it wants to include `@angular/localize` at runtime. This commit changes the default location of the package to be the `devDependencies` section, but gives an option `useAtRuntime` to choose otherwise. Fixes #38329 PR Close #38680
This commit is contained in:
parent
fdea1804d6
commit
50f4d8a1ce
|
@ -16,6 +16,9 @@
|
|||
"ng-update": {
|
||||
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
|
||||
},
|
||||
"ng-add": {
|
||||
"save": "devDependencies"
|
||||
},
|
||||
"sideEffects": [
|
||||
"**/init/index.js",
|
||||
"**/init.js",
|
||||
|
|
|
@ -4,3 +4,7 @@ This schematic will be executed when an Angular CLI user runs `ng add @angular/l
|
|||
|
||||
It will search their `angular.json` file, and find polyfills and main files for server builders.
|
||||
Then it will add the `@angular/localize/init` polyfill that `@angular/localize` needs to work.
|
||||
|
||||
If the user specifies that they want to use `$localize` at runtime then the dependency will be
|
||||
added to the `depdendencies` section of `package.json` rather than in the `devDependencies` which
|
||||
is the default.
|
|
@ -9,7 +9,9 @@
|
|||
*/
|
||||
|
||||
import {virtualFs, workspaces} from '@angular-devkit/core';
|
||||
import {chain, Rule, SchematicsException, Tree} from '@angular-devkit/schematics';
|
||||
import {chain, noop, Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
|
||||
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
|
||||
import {addPackageJsonDependency, NodeDependencyType, removePackageJsonDependency} from '@schematics/angular/utility/dependencies';
|
||||
import {getWorkspace} from '@schematics/angular/utility/workspace';
|
||||
import {Builders} from '@schematics/angular/utility/workspace-models';
|
||||
|
||||
|
@ -95,6 +97,22 @@ function prependToTargetFiles(
|
|||
};
|
||||
}
|
||||
|
||||
function moveToDependencies(host: Tree, context: SchematicContext) {
|
||||
if (host.exists('package.json')) {
|
||||
// Remove the previous dependency and add in a new one under the desired type.
|
||||
removePackageJsonDependency(host, '@angular/localize');
|
||||
addPackageJsonDependency(host, {
|
||||
name: '@angular/localize',
|
||||
type: NodeDependencyType.Default,
|
||||
version: `~0.0.0-PLACEHOLDER`
|
||||
});
|
||||
|
||||
// Add a task to run the package manager. This is necessary because we updated
|
||||
// "package.json" and we want lock files to reflect this.
|
||||
context.addTask(new NodePackageInstallTask());
|
||||
}
|
||||
}
|
||||
|
||||
export default function(options: Schema): Rule {
|
||||
return async (host: Tree) => {
|
||||
if (!options.name) {
|
||||
|
@ -117,6 +135,9 @@ ${localizePolyfill}
|
|||
return chain([
|
||||
prependToTargetFiles(project, Builders.Browser, 'polyfills', localizeStr),
|
||||
prependToTargetFiles(project, Builders.Server, 'main', localizeStr),
|
||||
// If `$localize` will be used at runtime then must install `@angular/localize`
|
||||
// into `dependencies`, rather than the default of `devDependencies`.
|
||||
options.useAtRuntime ? moveToDependencies : noop()
|
||||
]);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -33,6 +33,13 @@ export { renderModule, renderModuleFactory } from '@angular/platform-server';`;
|
|||
|
||||
beforeEach(() => {
|
||||
host = new UnitTestTree(new HostTree());
|
||||
host.create('package.json', JSON.stringify({
|
||||
'devDependencies': {
|
||||
// The default (according to `ng-add` in its package.json) is for `@angular/localize` to be
|
||||
// saved to `devDependencies`.
|
||||
'@angular/localize': '~0.0.0-PLACEHOLDER',
|
||||
}
|
||||
}));
|
||||
host.create('src/polyfills.ts', polyfillsContent);
|
||||
host.create('src/another-polyfills.ts', polyfillsContent);
|
||||
host.create('src/unrelated-polyfills.ts', polyfillsContent);
|
||||
|
@ -166,4 +173,22 @@ export { renderModule, renderModuleFactory } from '@angular/platform-server';`;
|
|||
}));
|
||||
await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
|
||||
});
|
||||
|
||||
it('should add package to `devDependencies` by default', async () => {
|
||||
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
|
||||
const packageJsonText = host.readContent('/package.json');
|
||||
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize'])
|
||||
.toBe('~0.0.0-PLACEHOLDER');
|
||||
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should add package to `dependencies` if `useAtRuntime` is `true`', async () => {
|
||||
host = await schematicRunner
|
||||
.runSchematicAsync('ng-add', {...defaultOptions, useAtRuntime: true}, host)
|
||||
.toPromise();
|
||||
const packageJsonText = host.readContent('/package.json');
|
||||
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize'])
|
||||
.toBe('~0.0.0-PLACEHOLDER');
|
||||
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize']).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -11,4 +11,11 @@ export interface Schema {
|
|||
* The name of the project.
|
||||
*/
|
||||
name?: string;
|
||||
/**
|
||||
* Will this project use $localize at runtime?
|
||||
*
|
||||
* If true then the dependency is included in the `dependencies` section of packge.json, rather
|
||||
* than `devDependencies`.
|
||||
*/
|
||||
useAtRuntime?: boolean;
|
||||
}
|
||||
|
|
|
@ -10,8 +10,12 @@
|
|||
"$default": {
|
||||
"$source": "projectName"
|
||||
}
|
||||
},
|
||||
"useAtRuntime": {
|
||||
"type": "boolean",
|
||||
"description": "If set then `@angular/localize` is included in the `dependencies` section of `package.json`, rather than `devDependencies`, which is the default.",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
]
|
||||
"required": []
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue