Angular: Bazelの下でツリーを揺るがすプロバイダーによって書かれた間違ったインポート

作成日 2018年05月15日  ·  1コメント  ·  ソース: angular/angular

ツリーを揺るがすプロバイダーは、Angularコンパイラーをトリガーして、ユーザーのコードにインポートステートメントを導入します。これは以前は行いませんでした。 ディスク上のモジュールパスをモジュール名と統合する場所があることがわかりました(以前のバージョンのコンパイラにはこの区別がありませんでした)
などの輸入書き込み終わる我々はそうimport {} from 'wksp/packages/path/to/thing'されている必要がありますimport {} from '@pkg_name/path/to/thing'

私たちの知る限り、この問題はngcでコンパイルし、ツリーを揺るがすプロバイダーを使用するBazelユーザーにのみ影響します。 これまでのところ、この問題はAngularの内部でのみ確認されています。

これは#20030の一部として見つかりました

再現するには、ツリーを揺るがすプロバイダーを追加します。たとえば、 packages/common/src/common_module.ts貼り付けます。

import {Injectable, PLATFORM_ID} from '@angular/core';
import {DOCUMENT, isPlatformBrowser} from '@angular/common';

export function viewportScrollerFactory(platformId: string, document: any) {
  return isPlatformBrowser(platformId) ? null: null;
}

@Injectable(
    {providedIn: 'root', useFactory: viewportScrollerFactory, deps: [PLATFORM_ID, DOCUMENT]})
export abstract class ViewportScroller {}

次に、npmパッケージをビルドします

bazel build //packages/common:npm_package

そして、出力で文字列angular/packagesを探します。

$ export bin=$(bazel info bazel-bin)
$ find $bin -type f -exec fgrep -l angular/packages {} \; | grep -v ngsummary | grep -v ngfactory

ngsummaryファイルは常にモジュール識別子としてディスク上にパスを持っているため、削除します。
ngfactoryファイルはパッケージに同梱されていないため、これらもおそらく無害です。

興味深いことに、ES2015の出力に不適切なインポートが見つかりました。

bin/packages/common/src/common_module.closure.js

/**
 * <strong i="28">@fileoverview</strong> added by tsickle
 * <strong i="29">@suppress</strong> {checkTypes} checked by tsc
 */
/**
 * <strong i="30">@license</strong>
 * 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 { COMMON_DIRECTIVES } from './directives/index';
import { DEPRECATED_PLURAL_FN, NgLocaleLocalization, NgLocalization, getPluralCase } from './i18n/localization';
import { COMMON_DEPRECATED_I18N_PIPES } from './pipes/deprecated/index';
import { COMMON_PIPES } from './pipes/index';
import { Injectable, PLATFORM_ID } from '@angular/core';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import * as i0 from "@angular/core";
import * as i1 from "@angular/core/src/application_tokens";
import * as i2 from "angular/packages/common/src/dom_tokens";  // <-------------------------

ES5出力にはありません

bin/packages/common/src/common_module.js

(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define("@angular/common/src/common_module", ["require", "exports", "tslib", "@angular/core", "@angular/common/src/directives/index", "@angular/common/src/i18n/localization", "@angular/common/src/pipes/deprecated/index", "@angular/common/src/pipes/index", "@angular/core", "@angular/common", "@angular/core", "@angular/core/src/application_tokens", "@angular/common/src/dom_tokens"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var core_1 = require("@angular/core");
    var index_1 = require("@angular/common/src/directives/index");
    var localization_1 = require("@angular/common/src/i18n/localization");
    var index_2 = require("@angular/common/src/pipes/deprecated/index");
    var index_3 = require("@angular/common/src/pipes/index");
    var core_2 = require("@angular/core");
    var common_1 = require("@angular/common");
    var i0 = require("@angular/core");
    var i1 = require("@angular/core/src/application_tokens");
    var i2 = require("@angular/common/src/dom_tokens");
P3 bazel low bufix

最も参考になるコメント

現在、この問題が発生しています。

コンパイラAPIを内部的にモノリポジトリで呼び出します。これにより、内部的には次のようなインポートパスも発生します。

import * as i1 from "@our-group/our-package/src/some-service";

これはES5出力でも発生しますが、根本的な原因は似ていると思います。
私たちの回避策は、@ Injectableでファクトリを自分で定義することです。 これにより、Angularがファクトリを生成して、誤ったインポートを生成するのを防ぎます。

より多くの情報/再現性を提供する必要があるかどうかわからない:

  • これはプロプライエタリコードの組み合わせであるため、そうすることは非常に困難です。
  • 原因はすでにご存知だと思いますが、現在のところ、当然のことながら、Angularの内部でのみ発生するという仮定に基づいて優先順位を付けないでください。

>すべてのコメント

現在、この問題が発生しています。

コンパイラAPIを内部的にモノリポジトリで呼び出します。これにより、内部的には次のようなインポートパスも発生します。

import * as i1 from "@our-group/our-package/src/some-service";

これはES5出力でも発生しますが、根本的な原因は似ていると思います。
私たちの回避策は、@ Injectableでファクトリを自分で定義することです。 これにより、Angularがファクトリを生成して、誤ったインポートを生成するのを防ぎます。

より多くの情報/再現性を提供する必要があるかどうかわからない:

  • これはプロプライエタリコードの組み合わせであるため、そうすることは非常に困難です。
  • 原因はすでにご存知だと思いますが、現在のところ、当然のことながら、Angularの内部でのみ発生するという仮定に基づいて優先順位を付けないでください。
このページは役に立ちましたか?
0 / 5 - 0 評価