Section 20: JavaScript Modules - Writing Modular (=Multi-File) Code
The goals
๐ช๐ปWhat & Why?
โ๐ปUsage
Serve
sudo npm install -g serve
Module
๋ชจ๋์ด๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ฑํ๋ ๊ฐ๋ณ์ ์์๋ก์ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ฝ๋ ์กฐ๊ฐ์ ๋งํ๋ค. ์ผ๋ฐ์ ์ผ๋ก ๋ชจ๋์ ๊ธฐ๋ฅ์ ๊ธฐ์ค์ผ๋ก ํ์ผ ๋จ์๋ก ๋ถ๋ฆฌํ๋ค. ์ด๋ ๋ชจ๋์ด ์ฑ๋ฆฝํ๋ ค๋ฉด ๋ชจ๋์ ์์ ๋ง์ ํ์ผ ์ค์ฝํ(๋ชจ๋ ์ค์ฝํ)๋ฅผ ๊ฐ์ง ์ ์์ด์ผ ํ๋ค.
์์ ๋ง์ ํ์ผ ์ค์ฝํ๋ฅผ ๊ฐ๋ ๋ชจ๋์ ์์ฐ(๋ชจ๋์ ํฌํจ๋์ด ์๋ ๋ณ์, ํจ์, ๊ฐ์ฒด ๋ฑ)์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋น๊ณต๊ฐ ์ํ๋ค. ๋ค์ ๋งํด, ์์ ๋ง์ ํ์ผ์ค์ฝํ๋ฅผ ๊ฐ๋ ๋ชจ๋์ ๋ชจ๋ ์์ฐ์ ์บก์ํ๋์ด ๋ค๋ฅธ ๋ชจ๋์์ ์ ๊ทผํ ์ ์๋ค. ์ฆ, ๋ชจ๋์ ๊ฐ๋ณ์ ์กด์ฌ๋ก์ ์ ํ๋ฆฌ์ผ์ด์ ๊ณผ ๋ถ๋ฆฌ๋์ด ์กด์ฌํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์์ ์ง์ ํ ๋ณ์๋ค์ global scope๋ก ์ง์ ๋๊ธฐ ๋๋ฌธ์ ์ฌ์ค์ ํ๋์ ์คํฌ๋ฆฝํธ ํ์ผ์์ ์์ฑํ๋ ๊ฒ๊ณผ ๋์ผํ ํจ๊ณผ๋ฅผ ๊ฐ์ง๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ํ์ผ์์ ๋์ผํ ์ด๋ฆ์ ๋ณ์๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ๋ฎ์ด ์ฐ๋ ํจ๊ณผ๊ฐ ๋ํ๋๊ฒ ๋๋ค.
์ด๋ ๊ฒ ์ ์ฅ๋ ๋ณ์๋ค์ window์์ ์กฐํ ๊ฐ๋ฅํ๋ค.
export, importํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
module์ญ์ class์ ๊ฐ์ด strict mode์์ ์๋ํ๋ค.
globalThis
The global globalThis property contains the global this value, which is akin to the global object.
Search for the global across environments
Usually, the global object does not need to be explicitly specified — its properties are automatically accessible as global variables.
However, one case where one needs to explicitly access the global object is when writing to it, usually for the purpose of polyfills.
Prior to globalThis, the only reliable cross-platform way to get the global object for an environment was Function('return this')(). However, this causes CSP violations in some settings, so authors would use a piecewise definition like this (slightly adapted from the original core-js source):
With globalThis available, the additional search for the global across environments is not necessary anymore:
if (typeof globalThis.Intl === 'undefined') {
Object.defineProperty(globalThis, 'Intl', {
value: {
// Our Intl implementation
},
enumerable: false,
configurable: true,
writable: true,
});
}
By using globalThis, your code will work in window and non-window contexts without having to write additional checks or tests. In most environments, globalThis directly refers to the global object of that environment. In browsers, however, a proxy is used internally to take into account iframe and cross-window security. In practice, it doesn’t change the way you write your code, though.
Generally, when you’re not sure in what environment your code will be used, or when you want to make your code executable in different environments, the globalThis property comes in very handy. You’ll have to use a polyfill to implement the feature on older browsers that do not support it, however.
On the other hand, if you’re certain what environment your code is going to be used in, then use one of the existing ways of referencing the environment’s global object and save yourself from the need to include a polyfill for globalThis.
quiz
์ฌ๋ฌ <script src="...">๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ๋ณด๋ค JavaScript ๋ชจ๋์ด ๋ ๋์ ์ด์ ๋ ๋ฌด์์ผ๊น์?
-
์ฐจ์ด ์์ต๋๋ค - ๋ ๊ฐ์ง ์ ๊ทผ ๋ฐฉ์ ์ค ์๋ฌด๊ฑฐ๋ ์ธ ์ ์์ต๋๋ค.
-
๋ชจ๋์ ์ฑ๋ฅ์ด ๋ ์ข์ต๋๋ค - ์ด๊ฒ์ด ๋ชจ๋์ ์จ์ผ ํ๋ ์ด์ ์ ๋๋ค.
-
๊ฐ ํ์ผ์ด ์ข ์์ฑ์ ์ง์ ํ๋ฏ๋ก ์ฌ๋ฐ๋ฅธ ์์๋ฅผ ์๋์ผ๋ก ์ผ์ผ์ด ๊ด๋ฆฌํ ํ์๊ฐ ์์ต๋๋ค.
์๋์ export ๋ฌธ์ ๋ํด ์ณ์ ๊ฒ์ ๋ฌด์์ผ๊น์?
- export const myName = 'Max';
-
ํ์ผ์ ์ ์ผํ export์ฒ๋ผ ๋ณด์ด๋ ์ด๋ฅธ๋ฐ “๊ธฐ๋ณธ export”์ ๋๋ค.
-
์ด๊ฒ์ ์์ "๋ช ๋ช ๋ export"์ ๋๋ค - myName์ด๋ผ๋ ์ด๋ฆ ์๋์ ๊ฐ์ด 'Max'์ธ ์์๋ฅผ ๋ด๋ณด๋ด๋ ๊ฒ๋๋ค.
-
์ ํจํ์ง ์์ ๊ตฌ๋ฌธ์ด ์๋ ์์ ๋๋ค - const ํค์๋๋ฅผ ์ ๊ฑฐํด์ผ ํฉ๋๋ค.
๋ค์์ export๋ฅผ ์ด๋ป๊ฒ ์ํฌํธ ํด์ผ ํ ๊น์?
export const myName = 'Max';
export function greet() { ... }
-
- import personData from './file.js';
-
- import { myName } from './file.js';
-
- import * as personData from './file.js';
์ด๋ ํ์ผ์ ๋ชจ๋ export๋ฅผ ํ๋์ ๊ฐ์ฒด๋ก ๋ฌถ์ต๋๋ค. ์ด(๋ฌถ๋ ๊ธฐ์ )๋ฅผ ์ฌ์ฉํ์ง ์๋ ๋์์ ์ํฌํธํ๋ ค๋ ๋ชจ๋ ๋ช ๋ช ๋ export๋ฅผ ๋์์ผ๋ก ์ง์ ํ๋ ๊ฒ๋๋ค: "import { Greeting, myName } from './file.js'".
๋ชจ๋์์ ๋ด๋ณด๋ด์ง ์์ ์ฝ๋๋ ์ธ์ , ์ผ๋ง๋ ์์ฃผ ์คํ๋ ๊น์?
-
๋ชจ๋์ด ๋ค๋ฅธ ํ์ผ์์ ์ํฌํธ๋ ๋ ํญ์.
-
๋ชจ๋์ด ๋ค๋ฅธ ํ์ผ์์ ์ฒ์์ผ๋ก ์ํฌํธ๋ ๋.
-
์ ๋ ์คํ๋์ง ์์ต๋๋ค
sources
https://www.npmjs.com/package/serve
serve
Static file serving and directory listing. Latest version: 14.0.1, last published: 4 months ago. Start using serve in your project by running `npm i serve`. There are 653 other projects in the npm registry using serve.
www.npmjs.com
๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive
์ด์ ๋ชจ ์ผ๋ณธ์์ ์ปดํจํฐ๊ณตํ์ ์ ๊ณตํ ํ ์ผ๋ณธ์ ์๋์ฐจ ์ฐ๊ตฌ์ ๊ณต์ฉ ์น ํ๋ ์์ํฌ ๊ฐ๋ฐ ํ๋ก์ ํธ๋ฅผ ์์์ผ๋ก ํ๋ก๊ทธ๋๋ฐ ์ธ๊ณ์ ๋ฐ์ ๋ค์ฌ ๋์๋ค. ์ดํ ์ธ๊ตญ๊ณ IT ๊ธฐ์ ์์ ์ํํธ์จ์ด ์ปจ์ค
books.google.co.jp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
globalThis - JavaScript | MDN
The global globalThis property contains the global this value, which is akin to the global object.
developer.mozilla.org
https://blog.logrocket.com/what-is-globalthis-why-use-it/
What is globalThis, and why should you start using it? - LogRocket Blog
The globalThis proposal, now in stage 4, aims to consolidate the increasingly fragmented ways of accessing the global object in JavaScript.
blog.logrocket.com