ES6 module

  • module(export, import) ์ดํ•ด - babel ์„ค์ •์ด ํ•„์š”ํ•จ

// dist/bundle.js
import { log } from './myLogger' // myLogger ํŒŒ์ผ์ด ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ๋„˜์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— destructing ํ˜•ํƒœ๋กœ log func๋งŒ ๋ฝ‘์•„์„œ ์“ด๋‹ค

const root = document.querySelector('#root')
root.innerHTML = `<p>Hello world!</p>`

log()

// myLogger.js
export default function log(data) { // export default๋กœ export. default๋Š” ์ด ํŒŒ์ผ ๋‚ด์— ์œ ์ผํ•œ export
	console.log(data)
}
  • module(export, import) ๊ธฐ๋ฐ˜ ์„œ๋น„์Šค ๊ตฌํ˜„

// bundle.js
import log, { getTime, getCurrentHour, MyLogger, _ } from './myLogger'

const root = document.querySelector('#root')
root.innerHTML = `<p>Hello world!</p>`

log('test 1234')
log(`now time is ${getTime()}`)
log(`current hour is ${getCurrentHour()}`)

const logger = new MyLogger()
log(`my lectures are ${logger.getLectures()}`)
_.log('hihi')
// myLogger.js
// export default function log(data) {
// 	console.log(data)
// }

export const getTime = () => {
	return Date.now()
}

export const getCurrentHour = () => {
	return (new Date).getHours()
}

export default class MyLogger {
	constructor(props) {
		this.lectures = ['java', 'IOS']
	}

	getLectures() {
		return this.lectures
	}
}

/* utility */
export const _ = {
	log(data) {
		if(window.console) console.log(data)
	}
}

Last updated