49장 Babel과 Webpack을 이용한 ES6+/ES.NEXT 개발 환경 구축
2022-05-10
이번장은 설치가 많아서 자세하게는 작성하지 않는다
49.0 Babel과 Webpack을 이용한 ES6+/ES.NEXT 개발 환경 구축
트랜스파일러인 Babel과 모듈 번들러인 Webpack을 이용하여 ES6+/ES.NEXT 개발 환경을 구축해 보고, Webpack을 통해 Babel을 로드하여 ES6+/ES.NEXT 사양의 소스코드를 IE같은 구형 브라우저에서도 동작하도록 ES5사양의 소스코드로 트랜스파일링하는 방법도 알아 보자
49.1 Babel(49-01)
[ 1 , 2 , 3 ]. map ( n => n ** n );
49-02
IE 같은 구형 브라우저에서 지원이 안될수도 있으니 Babel을 사용하면 ES5 사양으로 변환가능
" use strict " ;
[ 1 , 2 , 3 ]. map ( function ( n ) {
return Math . pow ( n , n );
});
49.1.1 Babel 설치(49-03)
{
"name" : "esnext-project" ,
"version" : "1.0.0" ,
"devDependencies" : {
"@babel/cli" : "^7.10.3" ,
"@babel/core" : "^7.10.3"
}
}
49.1.2 Babel 프리셋 설치와 babel.config.json 설정 파일 작성(49-04)
{
"name" : "esnext-project" ,
"version" : "1.0.0" ,
"devDependencies" : {
"@babel/cli" : "^7.10.3" ,
"@babel/core" : "^7.10.3" ,
"@babel/preset-env" : "^7.10.3"
}
}
49-05
{
"presets" : [ "@babel/preset-env" ]
}
49-06
{
"name" : "esnext-project" ,
"version" : "1.0.0" ,
"scripts" : {
"build" : "babel src/js -w -d dist/js"
},
"devDependencies" : {
"@babel/cli" : "^7.10.3" ,
"@babel/core" : "^7.10.3" ,
"@babel/preset-env" : "^7.10.3"
}
}
49-07
// src/js/lib.js
export const pi = Math . PI ; // ES6 모듈
export function power ( x , y ) {
return x ** y ; // ES7: 지수 연산자
}
// ES6 클래스
export class Foo {
### private = 10 ; // stage 3: 클래스 필드 정의 제안
foo () {
// stage 4: 객체 Rest/Spread 프로퍼티 제안
const { a , b , ... x } = { ...{ a : 1 , b : 2 }, c : 3 , d : 4 };
return { a , b , x };
}
bar () {
return this . ### private ;
}
}
49-08
// src/js/main.js
import { pi , power , Foo } from ' ./lib ' ;
console . log ( pi );
console . log ( power ( pi , pi ));
const f = new Foo ();
console . log ( f . foo ());
console . log ( f . bar ());
49-09
{
"name" : "esnext-project" ,
"version" : "1.0.0" ,
"scripts" : {
"build" : "babel src/js -w -d dist/js"
},
"devDependencies" : {
"@babel/cli" : "^7.10.3" ,
"@babel/core" : "^7.10.3" ,
"@babel/plugin-proposal-class-properties" : "^7.10.1" ,
"@babel/preset-env" : "^7.10.3"
}
}
49-10
{
"presets" : [ "@babel/preset-env" ],
"plugins" : [ "@babel/plugin-proposal-class-properties" ]
}
49-11
// dist/js/main.js
" use strict " ;
var _lib = require ( " ./lib " );
// src/js/main.js
console . log ( _lib . pi );
console . log (( 0 , _lib . power )( _lib . pi , _lib . pi ));
var f = new _lib . Foo ();
console . log ( f . foo ());
console . log ( f . bar ());
49-12
<!DOCTYPE html>
<html>
<body>
<script src= "dist/js/lib.js" ></script>
<script src= "dist/js/main.js" ></script>
</body>
</html>
49-13
{
"name" : "esnext-project" ,
"version" : "1.0.0" ,
"scripts" : {
"build" : "babel src/js -w -d dist/js"
},
"devDependencies" : {
"@babel/cli" : "^7.10.3" ,
"@babel/core" : "^7.10.3" ,
"@babel/plugin-proposal-class-properties" : "^7.10.1" ,
"@babel/preset-env" : "^7.10.3" ,
"webpack" : "^4.43.0" ,
"webpack-cli" : "^3.3.12"
}
}
49-14
{
"name" : "esnext-project" ,
"version" : "1.0.0" ,
"scripts" : {
"build" : "webpack -w"
},
"devDependencies" : {
"@babel/cli" : "^7.10.3" ,
"@babel/core" : "^7.10.3" ,
"@babel/plugin-proposal-class-properties" : "^7.10.1" ,
"@babel/preset-env" : "^7.10.3" ,
"babel-loader" : "^8.1.0" ,
"webpack" : "^4.43.0" ,
"webpack-cli" : "^3.3.12"
}
}
49-15
const path = require ( ' path ' );
module . exports = {
// entry file
// https://webpack.js.org/configuration/entry-context/#entry
entry : ' ./src/js/main.js ' ,
// 번들링된 js 파일의 이름(filename)과 저장될 경로(path)를 지정
// https://webpack.js.org/configuration/output/#outputpath
// https://webpack.js.org/configuration/output/#outputfilename
output : {
path : path . resolve ( __dirname , ' dist ' ),
filename : ' js/bundle.js '
},
// https://webpack.js.org/configuration/module
module : {
rules : [
{
test : / \. js$/ ,
include : [
path . resolve ( __dirname , ' src/js ' )
],
exclude : /node_modules/ ,
use : {
loader : ' babel-loader ' ,
options : {
presets : [ ' @babel/preset-env ' ],
plugins : [ ' @babel/plugin-proposal-class-properties ' ]
}
}
}
]
},
devtool : ' source-map ' ,
// https://webpack.js.org/configuration/mode
mode : ' development '
};
49-16
<!DOCTYPE html>
<html>
<body>
<script src= "./dist/js/bundle.js" ></script>
</body>
</html>
49-17
// src/js/main.js
import { pi , power , Foo } from ' ./lib ' ;
console . log ( pi );
console . log ( power ( pi , pi ));
const f = new Foo ();
console . log ( f . foo ());
console . log ( f . bar ());
// polyfill이 필요한 코드
console . log ( new Promise (( resolve , reject ) => {
setTimeout (() => resolve ( 1 ), 100 );
}));
// polyfill이 필요한 코드
console . log ( Object . assign ({}, { x : 1 }, { y : 2 }));
// polyfill이 필요한 코드
console . log ( Array . from ([ 1 , 2 , 3 ], v => v + v ));
49-18
...
// 190 line
console . log ( new Promise ( function ( resolve , reject ) {
setTimeout ( function () {
return resolve ( 1 );
}, 100 );
})); // polyfill이 필요한 코드
console . log ( Object . assign ({}, {
x : 1
}, {
y : 2
})); // polyfill이 필요한 코드
console . log ( Array . from ([ 1 , 2 , 3 ], function ( v ) {
return v + v ;
}));
...
49-19
{
"name" : "esnext-project" ,
"version" : "1.0.0" ,
"scripts" : {
"build" : "webpack -w"
},
"devDependencies" : {
"@babel/cli" : "^7.10.3" ,
"@babel/core" : "^7.10.3" ,
"@babel/plugin-proposal-class-properties" : "^7.10.1" ,
"@babel/preset-env" : "^7.10.3" ,
"babel-loader" : "^8.1.0" ,
"webpack" : "^4.43.0" ,
"webpack-cli" : "^3.3.12"
},
"dependencies" : {
"@babel/polyfill" : "^7.10.1"
}
}
49-20
// src/js/main.js
import " @babel/polyfill " ;
import { pi , power , Foo } from ' ./lib ' ;
...
49-21
const path = require ( ' path ' );
module . exports = {
// entry file
// https://webpack.js.org/configuration/entry-context/#entry
entry : [ ' @babel/polyfill ' , ' ./src/js/main.js ' ],
...
댓글남기기