Skip to content

Commit 152d99e

Browse files
jasonadenbenlesh
authored andcommitted
feat(common): add @angular/common/upgrade package for $location-related APIs (#30055)
AngularJS's `$location` service doesn't have a direct counterpart in Angular. This is largely because the `Location` service in Angular was pulled out of the `Router`, but was not purpose-built to stand on its own. This commit adds a new `@angular/common/upgrade` package with the beginnings of a new `LocationUpgradeService`. This service will more closely match the API of AngularJS and provide a way to replace the `$location` service from AngularJS. PR Close #30055
1 parent b635fe8 commit 152d99e

File tree

15 files changed

+229
-0
lines changed

15 files changed

+229
-0
lines changed

packages/common/src/location/platform_location.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export abstract class PlatformLocation {
3535
abstract onPopState(fn: LocationChangeListener): void;
3636
abstract onHashChange(fn: LocationChangeListener): void;
3737

38+
abstract get href(): string;
3839
abstract get protocol(): string;
3940
abstract get hostname(): string;
4041
abstract get port(): string;

packages/common/upgrade/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
exports_files(["package.json"])
4+
5+
load("//tools:defaults.bzl", "ng_module")
6+
7+
ng_module(
8+
name = "upgrade",
9+
srcs = glob(
10+
[
11+
"*.ts",
12+
"src/**/*.ts",
13+
],
14+
),
15+
deps = [
16+
"//packages/common",
17+
"//packages/core",
18+
"//packages/upgrade/static",
19+
],
20+
)

packages/common/upgrade/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// This file is not used to build this module. It is only used during editing
10+
// by the TypeScript language service and during build for verification. `ngc`
11+
// replaces this file with production index.ts when it rewrites private symbol
12+
// names.
13+
14+
export * from './public_api';

packages/common/upgrade/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@angular/common/upgrade",
3+
"typings": "./upgrade.d.ts",
4+
"main": "../bundles/common-upgrade.umd.js",
5+
"module": "../fesm5/upgrade.js",
6+
"es2015": "../fesm2015/upgrade.js",
7+
"esm5": "../esm5/upgrade/upgrade.js",
8+
"esm2015": "../esm2015/upgrade/upgrade.js",
9+
"fesm5": "../fesm5/upgrade.js",
10+
"fesm2015": "../fesm2015/upgrade.js",
11+
"sideEffects": false
12+
}

packages/common/upgrade/public_api.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* @module
11+
* @description
12+
* Entry point for all public APIs of this package.
13+
*/
14+
export * from './src/index';
15+
16+
// This file only reexports content of the `src` folder. Keep it that way.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
const resolve = require('rollup-plugin-node-resolve');
10+
const sourcemaps = require('rollup-plugin-sourcemaps');
11+
12+
const globals = {
13+
'@angular/core': 'ng.core',
14+
'@angular/common': 'ng.common',
15+
'@angular/upgrade/static': 'ng.upgrade.static'
16+
};
17+
18+
19+
module.exports = {
20+
entry: '../../../dist/packages-dist/common/fesm5/upgrade.js',
21+
dest: '../../../dist/packages-dist/common/bundles/common-upgrade.umd.js',
22+
format: 'umd',
23+
exports: 'named',
24+
amd: {id: '@angular/common/upgrade'},
25+
moduleName: 'ng.common.upgrade',
26+
plugins: [resolve(), sourcemaps()],
27+
external: Object.keys(globals),
28+
globals: globals
29+
};

packages/common/upgrade/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './location';
10+
export * from './location_module';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Injectable} from '@angular/core';
10+
11+
/**
12+
* A Location service that provides properties and methods to match AngularJS's `$location`
13+
* service. It is recommended that this LocationUpgradeService be used in place of
14+
* `$location` in any hybrid Angular/AngularJS applications.
15+
*/
16+
@Injectable()
17+
export class LocationUpgradeService {
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {NgModule} from '@angular/core';
10+
import {LocationUpgradeService} from './location';
11+
12+
/**
13+
* Module used for configuring Angular's LocationUpgradeService.
14+
*/
15+
@NgModule({providers: [LocationUpgradeService]})
16+
export class LocationUpgradeModule {
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("//tools:defaults.bzl", "jasmine_node_test", "ts_library", "ts_web_test_suite")
2+
3+
ts_library(
4+
name = "test_lib",
5+
testonly = True,
6+
srcs = glob(["**/*.ts"]),
7+
deps = [
8+
"//packages/common",
9+
"//packages/common/upgrade",
10+
"//packages/common/testing",
11+
"//packages/core/testing",
12+
"//packages/upgrade/static",
13+
],
14+
)
15+
16+
jasmine_node_test(
17+
name = "test",
18+
bootstrap = ["angular/tools/testing/init_node_spec.js"],
19+
deps = [
20+
":test_lib",
21+
"//tools/testing:node",
22+
],
23+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {LocationUpgradeModule, LocationUpgradeService} from '@angular/common/upgrade';
10+
import {TestBed, inject} from '@angular/core/testing';
11+
import {UpgradeModule} from '@angular/upgrade/static';
12+
13+
describe('LocationUpgradeService', () => {
14+
let upgradeModule: UpgradeModule;
15+
16+
beforeEach(() => {
17+
TestBed.configureTestingModule({
18+
imports: [LocationUpgradeModule],
19+
providers: [UpgradeModule],
20+
});
21+
22+
upgradeModule = TestBed.get(UpgradeModule);
23+
upgradeModule.$injector = {
24+
get: jasmine.createSpy('$injector.get').and.returnValue({'$on': () => undefined})
25+
};
26+
});
27+
28+
it('should instantiate LocationUpgradeService',
29+
inject([LocationUpgradeService], (location: LocationUpgradeService) => {
30+
expect(location).toBeDefined();
31+
expect(location instanceof LocationUpgradeService).toBe(true);
32+
}));
33+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"extends": "../tsconfig-build.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"rootDir": "../",
6+
"paths": {
7+
"@angular/common": [
8+
"../../../dist/packages/common"
9+
],
10+
"@angular/core": [
11+
"../../../dist/packages/core"
12+
],
13+
"@angular/platform-browser": [
14+
"../../../dist/packages/platform-browser"
15+
],
16+
"@angular/upgrade/static": [
17+
"../../../dist/packages/upgrade/static"
18+
]
19+
},
20+
"outDir": "../../../dist/packages/common"
21+
},
22+
"files": [
23+
"public_api.ts"
24+
],
25+
"angularCompilerOptions": {
26+
"annotateForClosureCompiler": true,
27+
"strictMetadataEmit": false,
28+
"skipTemplateCodegen": true,
29+
"flatModuleOutFile": "upgrade.js",
30+
"flatModuleId": "@angular/common/upgrade"
31+
}
32+
}

packages/platform-browser/src/browser/location/browser_platform_location.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class BrowserPlatformLocation extends PlatformLocation {
4949
getDOM().getGlobalEventTarget(this._doc, 'window').addEventListener('hashchange', fn, false);
5050
}
5151

52+
get href(): string { return this.location.href; }
5253
get protocol(): string { return this.location.protocol; }
5354
get hostname(): string { return this.location.hostname; }
5455
get port(): string { return this.location.port; }

packages/platform-server/src/location.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function parseUrl(urlStr: string) {
3232
*/
3333
@Injectable()
3434
export class ServerPlatformLocation implements PlatformLocation {
35+
public readonly href: string = '/';
3536
public readonly hostname: string = '/';
3637
public readonly protocol: string = '/';
3738
public readonly port: string = '/';

packages/platform-webworker/src/web_workers/worker/platform_location.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export class WebWorkerPlatformLocation extends PlatformLocation {
7474

7575
onHashChange(fn: LocationChangeListener): void { this._hashChangeListeners.push(fn); }
7676

77+
get href(): string { return this._location ? this._location.href ! : '<unknown>'; }
78+
7779
get hostname(): string { return this._location ? this._location.host ! : '<unknown>'; }
7880

7981
get port(): string { return this._location ? this._location.port ! : '<unknown>'; }

0 commit comments

Comments
 (0)