webpack.config.js
client.jsx
WordRelay.jsx
index.html
npm init
원하는 디렉토리에 해당 커맨드를 치면 "package.json" 파일이 생성된다.
npm i react react-dom
리액트와 리액트돔을 설치해준다
npm i -D webpack webpack-cli
"-D"는 서비스를 위한게 아닌 개발용 프로그램이란 의미다.
npm run dev
npx webpack
npm i -D react-hot-loader
수정사항을 감지한다
npm i -D webpack-dev-server
수정사항이 발견됐을 때 자동으로 프로그램을 재실행해준다
`"webpack.config.js" 파일이 변경된 경우는 수동으로 재실행해줘야 한다.
npm run dev
은 "http://localhost:8080"이라는 서버 주소에 프로그램을 실행시킨다 "scripts: {
"dev": "webpack-dev-server --hot"
}
npx webpack
은 그대로 "localhost" 주소를 이용하면 된다웹팩은 jsx파일을 읽을 수 없기에 Babel을 설치해줘야한다.
npm i -D @babel/core
기본 바벨
필요한 기능에 맞게 바벨의 모듈들을 설치해준다.
npm i -D @babel/preset-env
최신 JavaScript를 사용가능하게 해준다
npm i -D @babel/preset-react
jsx를 사용가능하게 해준다
npm i -D babel-loader
바벨과 웹팩을 연결해준다
npm i -D @babel/plugin-proposal-class-properties
state를 사용가능하게 해준다 (setState)
const path = require('path'); // Import Node.js' path controller
module.exports = {
name: 'word-relay-setting', // Any name. Unnecessary option
mode: 'development', // For real service:production
devtool: 'eval', // Fast. "hidden-source-map" for real service
resolve: {
extensions: ['.js', '.jsx'] // No need to write .js or .jsx for files under entry
},
// Important part
entry: {
app: ['./client'], // Files to make the output file. If files are imported in another file, they don't need to be written down here. 'WordRelay.jsx' is imported in 'client.jsx'
},
module: { //These modules are applied to the entries to produce outputs
rules: [{
test: /\.jsx?$/, //Regular expression. Apply rules to js and jsx file
loader: 'babel-loader', // specifies which rule to apply
options: { // Options of the module (i.e. babel)
presets: [
['@babel/preset-env', {
targets: {
browsers: ['> 5% in KR', 'last 2 chrome versions'],
},
debug: true,
}],
'@babel/preset-react',
],
plugins: [
'@babel/plugin-proposal-class-properties',
'react-hot-loader/babel',
],
},
}],
},
plugins: [ // extra extensions
new.webpack.LoaderOptionsPlugin({ debug: true }), // applies "debug: true" to all the options of the loader
],
output: {
path: path.join(__dirname, 'dist'), // Set's the path to <current directory>/dist
filename: 'app.js' //output file
},
};
const React = require('react');
const ReactDom = require('react-dom');
const { hot } = require('react-hot-loader/root');
const WordRelay = require('./WordRelay');
const Hot = hot(WordRelay);
ReactDom.render(<Hot />, document.querySelector('#root'));
const React = require('react');
const { useState, useRef } = React;
const WordRelay = () => {
}
module.exports = WordRelay;
<html>
<head>
<meta charset="UTF-8" />
<title>끝말잇기</title>
</head>
<body>
<div id="root"></div>
<script src="./app.js"></script> // bring app.js
</body>
</html>
npm i
"package.json"에 있는 것들을 전부 설치해준다
npm i
로 위의 모든 설치와 설정들을 완료할 수 있다.