Created
March 18, 2018 06:06
-
-
Save kuju63/cfb0aab71e40480223e9c1eac3d640e5 to your computer and use it in GitHub Desktop.
TypeScriptでReact & Webpackを使う ref: https://qiita.com/KuriharaJun/items/fcd5527df710c77c8b9b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir proj | |
cd proj | |
mkdir src | |
cd src | |
mkdir components | |
cd .. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proj/ | |
└─ src/ | |
└─ components/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install -g typescript webpack webpack-cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install -g webpack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install --save react react-dom @types/react @types/react-dom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install --save-dev typescript awesome-typescript-loader source-map-loader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
export interface HelloProps { compiler: string; framework: string; } | |
export const Hello = (props: HelloProps) => <h1>Hello from {props.compiler} and {props.framework}!</h1>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Hello React!</title> | |
</head> | |
<body> | |
<div id="example"></div> | |
<!-- Dependencies --> | |
<script src="./node_modules/react/umd/react.development.js"></script> | |
<script src="./node_modules/react-dom/umd/react-dom.development.js"></script> | |
<!-- Main --> | |
<script src="./dist/bundle.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import * as ReactDOM from "react-dom"; | |
import { Hello } from "./components/Hello"; | |
ReactDOM.render( | |
<Hello compiler="TypeScript" framework="React" />, | |
document.getElementById("example") | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
//ここから | |
"build": "webpack", | |
"watch": "webpack -w" | |
//ここまでを追加する | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"compilerOptions": { | |
"sourceMap": true, | |
// TSはECMAScript 5に変換 | |
"target": "es5", | |
// TSのモジュールはES Modulesとして出力 | |
"module": "es2015", | |
// JSXの書式を有効に設定 | |
"jsx": "react", | |
"moduleResolution": "node", | |
"lib": [ | |
"es2017", | |
"dom" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
entry: "./src/index.tsx", | |
output: { | |
filename: "bundle.js", | |
path: __dirname + "/dist" | |
}, | |
// Enable sourcemaps for debugging webpack's output. | |
devtool: "source-map", | |
resolve: { | |
// Add '.ts' and '.tsx' as resolvable extensions. | |
extensions: [".ts", ".tsx", ".js", ".json"] | |
}, | |
module: { | |
rules: [ | |
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'. | |
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" }, | |
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. | |
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" } | |
] | |
}, | |
// When importing a module whose path matches one of the following, just | |
// assume a corresponding global variable exists and use that instead. | |
// This is important because it allows us to avoid bundling all of our | |
// dependencies, which allows browsers to cache those libraries between builds. | |
externals: { | |
"react": "React", | |
"react-dom": "ReactDOM" | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment