![](/img/trans.png)
[英]Why index.js is necessary even if I change my main entry point to app.js in package.json?
[英]Why does webpack ignore the index.js although it's in the same directory of my app.js, which is compiled fine
我正在使用webpack構建一個帶有react的簡單表單。 我是webpack的新手,因此您可能會提出一個明顯的解決方案,但我無法弄清楚。
我的代碼結構(簡體):
root:
- server.js
- webpack.config.js
- src:
-- App.js
-- index.js
- public:
-- index.html
-- bundle.js
在我的app.js
中,只有一個組件。 我已經將ReactDOM.render
方法排除在文件index.js
。 自從我做完以后,它就不再工作了。 在此之前,它還不錯。
該應用程序不再呈現到我的index.html
。 我猜 webpack只編譯我的app.js
文件,而忽略我的index.js
。 但是我不確定。
當我將index.js
包含到App.js
一切正常。
// /src/App.js
import React, {Component} from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
form: 'firmenkontakt'
}
};
render() {
return (
<div className={this.state}>
<form method="post" id={this.state}>
...
</form>
</div>
)
}
}
export default App;
渲染文件如下所示:
// /src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('app'));
registerServiceWorker();
我想知道我的webpack.config.js
是否有問題
// /webpack.config.js
let path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/App.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public')
},
watch: true,
module: {
loaders: [
{
test:/\.js$/,
exclude: /node_module/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-1']
}
}
]
}
}
您的輸入文件應為src/index.js' and it must import
App.js`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.