Angular: Bazel์—์„œ tree-shakable ์ œ๊ณต์ž๊ฐ€ ์ž‘์„ฑํ•œ ์ž˜๋ชป๋œ ๊ฐ€์ ธ์˜ค๊ธฐ

์— ๋งŒ๋“  2018๋…„ 05์›” 15์ผ  ยท  1๋…ผํ‰  ยท  ์ถœ์ฒ˜: angular/angular

Tree-shakable ๊ณต๊ธ‰์ž๋Š” Angular ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•˜์—ฌ ์ด์ „์—๋Š” ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž ์ฝ”๋“œ์— import ๋ฌธ์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ๋””์Šคํฌ์˜ ๋ชจ๋“ˆ ๊ฒฝ๋กœ๋ฅผ ๋ชจ๋“ˆ ์ด๋ฆ„๊ณผ ๊ฒฐํ•ฉํ•˜๋Š” ์œ„์น˜๊ฐ€ ์žˆ์Œ์ด ๋ฐํ˜€์กŒ์Šต๋‹ˆ๋‹ค(์ด์ „ ๋ฒ„์ „์˜ ์ปดํŒŒ์ผ๋Ÿฌ์—๋Š” ์ด๋Ÿฌํ•œ ๊ตฌ๋ถ„์ด ์—†์—ˆ์Šต๋‹ˆ๋‹ค).
๋”ฐ๋ผ์„œ import {} from '@pkg_name/path/to/thing' ์—ฌ์•ผ ํ•˜๋Š” import {} from 'wksp/packages/path/to/thing' ์™€ ๊ฐ™์€ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ํ•œ, ์ด ๋ฌธ์ œ๋Š” ngc ๋กœ ์ปดํŒŒ์ผํ•˜๊ณ  tree-shakable ๊ณต๊ธ‰์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” 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 ๋“ฑ๊ธ‰