From 152d99eef0478255861d3ca2d749227bc9e1befe Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Mon, 11 Feb 2019 14:18:29 -0800 Subject: [PATCH] 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 --- .../common/src/location/platform_location.ts | 1 + packages/common/upgrade/BUILD.bazel | 20 +++++++++++ packages/common/upgrade/index.ts | 14 ++++++++ packages/common/upgrade/package.json | 12 +++++++ packages/common/upgrade/public_api.ts | 16 +++++++++ packages/common/upgrade/rollup.config.js | 29 ++++++++++++++++ packages/common/upgrade/src/index.ts | 10 ++++++ packages/common/upgrade/src/location.ts | 18 ++++++++++ .../common/upgrade/src/location_module.ts | 17 ++++++++++ packages/common/upgrade/test/BUILD.bazel | 23 +++++++++++++ packages/common/upgrade/test/upgrade.spec.ts | 33 +++++++++++++++++++ packages/common/upgrade/tsconfig-build.json | 32 ++++++++++++++++++ .../location/browser_platform_location.ts | 1 + packages/platform-server/src/location.ts | 1 + .../web_workers/worker/platform_location.ts | 2 ++ 15 files changed, 229 insertions(+) create mode 100644 packages/common/upgrade/BUILD.bazel create mode 100644 packages/common/upgrade/index.ts create mode 100644 packages/common/upgrade/package.json create mode 100644 packages/common/upgrade/public_api.ts create mode 100644 packages/common/upgrade/rollup.config.js create mode 100644 packages/common/upgrade/src/index.ts create mode 100644 packages/common/upgrade/src/location.ts create mode 100644 packages/common/upgrade/src/location_module.ts create mode 100644 packages/common/upgrade/test/BUILD.bazel create mode 100644 packages/common/upgrade/test/upgrade.spec.ts create mode 100644 packages/common/upgrade/tsconfig-build.json diff --git a/packages/common/src/location/platform_location.ts b/packages/common/src/location/platform_location.ts index 0b0472dfb6..8a95db1a90 100644 --- a/packages/common/src/location/platform_location.ts +++ b/packages/common/src/location/platform_location.ts @@ -35,6 +35,7 @@ export abstract class PlatformLocation { abstract onPopState(fn: LocationChangeListener): void; abstract onHashChange(fn: LocationChangeListener): void; + abstract get href(): string; abstract get protocol(): string; abstract get hostname(): string; abstract get port(): string; diff --git a/packages/common/upgrade/BUILD.bazel b/packages/common/upgrade/BUILD.bazel new file mode 100644 index 0000000000..01f378daa1 --- /dev/null +++ b/packages/common/upgrade/BUILD.bazel @@ -0,0 +1,20 @@ +package(default_visibility = ["//visibility:public"]) + +exports_files(["package.json"]) + +load("//tools:defaults.bzl", "ng_module") + +ng_module( + name = "upgrade", + srcs = glob( + [ + "*.ts", + "src/**/*.ts", + ], + ), + deps = [ + "//packages/common", + "//packages/core", + "//packages/upgrade/static", + ], +) diff --git a/packages/common/upgrade/index.ts b/packages/common/upgrade/index.ts new file mode 100644 index 0000000000..e727e2e8a7 --- /dev/null +++ b/packages/common/upgrade/index.ts @@ -0,0 +1,14 @@ +/** + * @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 + */ + +// This file is not used to build this module. It is only used during editing +// by the TypeScript language service and during build for verification. `ngc` +// replaces this file with production index.ts when it rewrites private symbol +// names. + +export * from './public_api'; diff --git a/packages/common/upgrade/package.json b/packages/common/upgrade/package.json new file mode 100644 index 0000000000..769ba43d5d --- /dev/null +++ b/packages/common/upgrade/package.json @@ -0,0 +1,12 @@ +{ + "name": "@angular/common/upgrade", + "typings": "./upgrade.d.ts", + "main": "../bundles/common-upgrade.umd.js", + "module": "../fesm5/upgrade.js", + "es2015": "../fesm2015/upgrade.js", + "esm5": "../esm5/upgrade/upgrade.js", + "esm2015": "../esm2015/upgrade/upgrade.js", + "fesm5": "../fesm5/upgrade.js", + "fesm2015": "../fesm2015/upgrade.js", + "sideEffects": false +} \ No newline at end of file diff --git a/packages/common/upgrade/public_api.ts b/packages/common/upgrade/public_api.ts new file mode 100644 index 0000000000..778427176d --- /dev/null +++ b/packages/common/upgrade/public_api.ts @@ -0,0 +1,16 @@ +/** + * @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 + */ + +/** + * @module + * @description + * Entry point for all public APIs of this package. + */ +export * from './src/index'; + +// This file only reexports content of the `src` folder. Keep it that way. diff --git a/packages/common/upgrade/rollup.config.js b/packages/common/upgrade/rollup.config.js new file mode 100644 index 0000000000..edaa99b4d9 --- /dev/null +++ b/packages/common/upgrade/rollup.config.js @@ -0,0 +1,29 @@ +/** + * @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 + */ + +const resolve = require('rollup-plugin-node-resolve'); +const sourcemaps = require('rollup-plugin-sourcemaps'); + +const globals = { + '@angular/core': 'ng.core', + '@angular/common': 'ng.common', + '@angular/upgrade/static': 'ng.upgrade.static' +}; + + +module.exports = { + entry: '../../../dist/packages-dist/common/fesm5/upgrade.js', + dest: '../../../dist/packages-dist/common/bundles/common-upgrade.umd.js', + format: 'umd', + exports: 'named', + amd: {id: '@angular/common/upgrade'}, + moduleName: 'ng.common.upgrade', + plugins: [resolve(), sourcemaps()], + external: Object.keys(globals), + globals: globals +}; diff --git a/packages/common/upgrade/src/index.ts b/packages/common/upgrade/src/index.ts new file mode 100644 index 0000000000..2c7f54209f --- /dev/null +++ b/packages/common/upgrade/src/index.ts @@ -0,0 +1,10 @@ +/** + * @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 + */ + +export * from './location'; +export * from './location_module'; \ No newline at end of file diff --git a/packages/common/upgrade/src/location.ts b/packages/common/upgrade/src/location.ts new file mode 100644 index 0000000000..a4b5984cce --- /dev/null +++ b/packages/common/upgrade/src/location.ts @@ -0,0 +1,18 @@ +/** + * @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 {Injectable} from '@angular/core'; + +/** + * A Location service that provides properties and methods to match AngularJS's `$location` + * service. It is recommended that this LocationUpgradeService be used in place of + * `$location` in any hybrid Angular/AngularJS applications. + */ +@Injectable() +export class LocationUpgradeService { +} diff --git a/packages/common/upgrade/src/location_module.ts b/packages/common/upgrade/src/location_module.ts new file mode 100644 index 0000000000..8f131b94ee --- /dev/null +++ b/packages/common/upgrade/src/location_module.ts @@ -0,0 +1,17 @@ +/** + * @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 {NgModule} from '@angular/core'; +import {LocationUpgradeService} from './location'; + +/** + * Module used for configuring Angular's LocationUpgradeService. + */ +@NgModule({providers: [LocationUpgradeService]}) +export class LocationUpgradeModule { +} diff --git a/packages/common/upgrade/test/BUILD.bazel b/packages/common/upgrade/test/BUILD.bazel new file mode 100644 index 0000000000..1cd1cb834a --- /dev/null +++ b/packages/common/upgrade/test/BUILD.bazel @@ -0,0 +1,23 @@ +load("//tools:defaults.bzl", "jasmine_node_test", "ts_library", "ts_web_test_suite") + +ts_library( + name = "test_lib", + testonly = True, + srcs = glob(["**/*.ts"]), + deps = [ + "//packages/common", + "//packages/common/upgrade", + "//packages/common/testing", + "//packages/core/testing", + "//packages/upgrade/static", + ], +) + +jasmine_node_test( + name = "test", + bootstrap = ["angular/tools/testing/init_node_spec.js"], + deps = [ + ":test_lib", + "//tools/testing:node", + ], +) diff --git a/packages/common/upgrade/test/upgrade.spec.ts b/packages/common/upgrade/test/upgrade.spec.ts new file mode 100644 index 0000000000..a360da774e --- /dev/null +++ b/packages/common/upgrade/test/upgrade.spec.ts @@ -0,0 +1,33 @@ +/** + * @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 {LocationUpgradeModule, LocationUpgradeService} from '@angular/common/upgrade'; +import {TestBed, inject} from '@angular/core/testing'; +import {UpgradeModule} from '@angular/upgrade/static'; + +describe('LocationUpgradeService', () => { + let upgradeModule: UpgradeModule; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [LocationUpgradeModule], + providers: [UpgradeModule], + }); + + upgradeModule = TestBed.get(UpgradeModule); + upgradeModule.$injector = { + get: jasmine.createSpy('$injector.get').and.returnValue({'$on': () => undefined}) + }; + }); + + it('should instantiate LocationUpgradeService', + inject([LocationUpgradeService], (location: LocationUpgradeService) => { + expect(location).toBeDefined(); + expect(location instanceof LocationUpgradeService).toBe(true); + })); +}); \ No newline at end of file diff --git a/packages/common/upgrade/tsconfig-build.json b/packages/common/upgrade/tsconfig-build.json new file mode 100644 index 0000000000..70e507ee6c --- /dev/null +++ b/packages/common/upgrade/tsconfig-build.json @@ -0,0 +1,32 @@ +{ + "extends": "../tsconfig-build.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": "../", + "paths": { + "@angular/common": [ + "../../../dist/packages/common" + ], + "@angular/core": [ + "../../../dist/packages/core" + ], + "@angular/platform-browser": [ + "../../../dist/packages/platform-browser" + ], + "@angular/upgrade/static": [ + "../../../dist/packages/upgrade/static" + ] + }, + "outDir": "../../../dist/packages/common" + }, + "files": [ + "public_api.ts" + ], + "angularCompilerOptions": { + "annotateForClosureCompiler": true, + "strictMetadataEmit": false, + "skipTemplateCodegen": true, + "flatModuleOutFile": "upgrade.js", + "flatModuleId": "@angular/common/upgrade" + } +} \ No newline at end of file diff --git a/packages/platform-browser/src/browser/location/browser_platform_location.ts b/packages/platform-browser/src/browser/location/browser_platform_location.ts index 82e7a69b90..7171e3995b 100644 --- a/packages/platform-browser/src/browser/location/browser_platform_location.ts +++ b/packages/platform-browser/src/browser/location/browser_platform_location.ts @@ -49,6 +49,7 @@ export class BrowserPlatformLocation extends PlatformLocation { getDOM().getGlobalEventTarget(this._doc, 'window').addEventListener('hashchange', fn, false); } + get href(): string { return this.location.href; } get protocol(): string { return this.location.protocol; } get hostname(): string { return this.location.hostname; } get port(): string { return this.location.port; } diff --git a/packages/platform-server/src/location.ts b/packages/platform-server/src/location.ts index 98525a7f98..7f633be669 100644 --- a/packages/platform-server/src/location.ts +++ b/packages/platform-server/src/location.ts @@ -32,6 +32,7 @@ function parseUrl(urlStr: string) { */ @Injectable() export class ServerPlatformLocation implements PlatformLocation { + public readonly href: string = '/'; public readonly hostname: string = '/'; public readonly protocol: string = '/'; public readonly port: string = '/'; diff --git a/packages/platform-webworker/src/web_workers/worker/platform_location.ts b/packages/platform-webworker/src/web_workers/worker/platform_location.ts index d63085a673..f7dce6c665 100644 --- a/packages/platform-webworker/src/web_workers/worker/platform_location.ts +++ b/packages/platform-webworker/src/web_workers/worker/platform_location.ts @@ -74,6 +74,8 @@ export class WebWorkerPlatformLocation extends PlatformLocation { onHashChange(fn: LocationChangeListener): void { this._hashChangeListeners.push(fn); } + get href(): string { return this._location ? this._location.href ! : ''; } + get hostname(): string { return this._location ? this._location.host ! : ''; } get port(): string { return this._location ? this._location.port ! : ''; }