我的意图是拥有这样的目录结构:
- /my-project/
-- /src/ (here are all .tsx files located)
-- /dist/
- index.html
- /build/
-bundle.js
-- /node_modules/
-- package.json
-- tsconfig.json
-- webpack.config.js
所以,我希望在 /dist 子目录下手动创建我的 index.html,其中包含 /build 子目录,这是 webpack 生成的 app.js 所在的位置。
当我保存一些位于 /src 目录中的 .tsx 文件时,我希望 webpack 自动重建 app.js,并且 webpack-dev-server 自动反映这些更改。
我的 package.json
如下所示:
{
"name": "my-project",
"version": "1.0.0",
"description": "My first React App",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --content-base ./dist --hot --inline --colors --port 3000 --open",
"build": "webpack --config webpack.config.js --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "AndreyKo",
"license": "ISC",
"devDependencies": {
"@types/react": "^0.14.54",
"@types/react-dom": "^0.14.19",
"jquery": "^3.1.1",
"source-map-loader": "^0.1.5",
"ts-loader": "^1.3.0",
"typescript": "^2.1.4",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"react": "^15.4.1",
"react-dom": "^15.4.1"
}
}
我的webpack.config.js
文件:
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "app.js",
publicPath: "/dist/",
path: "./dist/build/"
},
dev_tool: "source-map",
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
loaders: [
{test: /\.tsx?$/, loader: 'ts-loader'}
],
preloaders: [
{test: /\.js$/, loader: 'source-map-loader'}
]
}
}
我的 index.html
文件:
<!DOCTYPE html>
<html>
<body>
<div id="app-container"></div>
</body>
<script src="./build/app.js"></script>
</html>
而我的 index.tsx
文件看起来像这样:
import * as jQuery from 'jquery';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function appOutput() {
ReactDOM.render(<div>Hello, we are talking from React!</div>, document.getElementById("app-container"));
}
appOutput();
我已经在终端执行了命令:
npm run build
npm run start
所以,app.js已被创建并启动了webpack-dev-server,页面在浏览器中打开,到目前为止一切都很好。但是当我更改index.tsx文件中的文本并保存此文件时,没有任何变化。我尝试刷新浏览器页面,文本仍然保持不变,app.js也没有重新构建。为了使文本更改生效,我必须手动从终端发出“npm run build”命令重新构建app.js。如何改进我的工作流程,以便在/src文件夹中进行的更改能够自动反映在dist/build/app.js文件中,而无需手动重新构建它?