studye / typescript

타입스크립트는 자바스크립트랑 다른 언어인가요?
7 stars 0 forks source link

[질문] import 하는 파일은 어떤거지? #9

Open denzels opened 7 years ago

denzels commented 7 years ago

import { ... } from 'library'

sshyun commented 7 years ago

npm에서 설치 할때 package.json에 있는 main함수에서 노출하는 JS 파일이고, 해당 JS에서 export 자체를 각각 한경우 {} 연산자로 따로 가져올 수 있습니다.

React package.json

{
    "homepage": "https://facebook.github.io/react/",
  "keywords": [
    "react"
  ],
  "license": "BSD-3-Clause",
  "main": "react.js",
}

react.js

'use strict';

module.exports = require('./lib/React');
denzels commented 7 years ago

@sshyun 오~ 자발적인 답변 좋아~ package.json에 안 적으면 어떻게 되는지 index.js 를 이용하는 경우는 다른 점이 있는지 적어주세요.

sshyun commented 7 years ago

package.json에 안적어도 에러는 나지 않지만, import 할때 디렉토리 경로를 일일히 적어 주어야 합니다. 이렇게 되면 package에 여러 파일을 import 할때 무지 불편 하지요..

import React from "react/lib/React";

index.js를 사용하면 export 자체를 제어 할수 있게되어 import 할때 깔끔 하게 처리가 가능 하다는 이점이 있지요..

utils/index.js

import HtmlUtil from "./src/html-utils";
import StringUtil from "./src/string-utils";

export {
    HtmlUtil,
    StringUtil
}

import 예시

import { StringUtil, HtmlUtil }  from "utils";
import * as Utils  from "utils";

Utils.HtmlUtil;
Utils.StringUtil;
sculove commented 7 years ago

main을 기본으로 찾고, 예전에 공유드렸던, es2015는 es6을 찾아요. http://2ality.com/2017/04/setting-up-multi-platform-packages.html?utm_source=nodeweekly&utm_medium=email

denzels commented 7 years ago

@sshyun @sculove Good!