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");
์ฐ๋ฆฌ๋ ์ง๊ธ ์ด ๋ฌธ์ ์ ์ง๋ฉดํด ์์ต๋๋ค.
์ฐ๋ฆฌ๋ ๋ด๋ถ์ ์ผ๋ก ๋ชจ๋ ธ ๋ฆฌํฌ์งํ ๋ฆฌ์์ ์ปดํ์ผ๋ฌ API๋ฅผ ํธ์ถํฉ๋๋ค. ์ด๋ ๋ด๋ถ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ ๊ฒฝ๋ก์์๋ ๋ฐ์ํฉ๋๋ค.
import * as i1 from "@our-group/our-package/src/some-service";
์ด๊ฒ์ ES5 ์ถ๋ ฅ์์ โโ๋ฐ์ํ์ง๋ง ๊ทผ๋ณธ ์์ธ์ด ๋น์ทํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ์ @Injectable์์ ์ง์ ํฉํ ๋ฆฌ๋ฅผ ์ ์ํ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฌ๋ฉด Angular๊ฐ ํฉํ ๋ฆฌ๋ฅผ ์์ฑํ์ฌ ์๋ชป๋ ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์์ฑํ์ง ๋ชปํ๊ฒ ๋ฉ๋๋ค.
์ถ๊ฐ ์ ๋ณด/์ฌํ ๊ฐ๋ฅํ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ผ ํ๋์ง ํ์คํ์ง ์์:
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ฐ๋ฆฌ๋ ์ง๊ธ ์ด ๋ฌธ์ ์ ์ง๋ฉดํด ์์ต๋๋ค.
์ฐ๋ฆฌ๋ ๋ด๋ถ์ ์ผ๋ก ๋ชจ๋ ธ ๋ฆฌํฌ์งํ ๋ฆฌ์์ ์ปดํ์ผ๋ฌ API๋ฅผ ํธ์ถํฉ๋๋ค. ์ด๋ ๋ด๋ถ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ ๊ฒฝ๋ก์์๋ ๋ฐ์ํฉ๋๋ค.
์ด๊ฒ์ ES5 ์ถ๋ ฅ์์ โโ๋ฐ์ํ์ง๋ง ๊ทผ๋ณธ ์์ธ์ด ๋น์ทํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ์ @Injectable์์ ์ง์ ํฉํ ๋ฆฌ๋ฅผ ์ ์ํ๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ฌ๋ฉด Angular๊ฐ ํฉํ ๋ฆฌ๋ฅผ ์์ฑํ์ฌ ์๋ชป๋ ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์์ฑํ์ง ๋ชปํ๊ฒ ๋ฉ๋๋ค.
์ถ๊ฐ ์ ๋ณด/์ฌํ ๊ฐ๋ฅํ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ผ ํ๋์ง ํ์คํ์ง ์์: