[英]Webpack successfully compiled but does not show output in browser
我的項目中的webpack成功運行,但是當我到達所需的端口時,它會顯示目錄文件。 你可以看一下圖像。
我的webpack.config.js
const path = require('path');
module.exports = {
entry: './app/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
devServer: {
inline: true,
port: 8100
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
}
}
的package.json
{
"name": "react_router",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server"
},
"author": "vinay",
"license": "ISC",
"dependencies": {
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-router": "^2.0.1"
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^2.6.1",
"webpack-dev-server": "^2.4.5"
}
}
main.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './app'
ReactDOM.render(<App />, document.getElementById('root'));
app.js
import React, {Component} from 'react';
import {Router, Route, Link, IndexRoute, hashHistory, browserHistory} from 'react-router';
const App = () => <h1>Hello World</h1>
export default App
輸出==>
任何幫助,將不勝感激。
編輯回答:我能夠讓它工作:
1)在根文件夾下創建實際的dist目錄2)使你的webpack.config.js中的輸出看起來像這樣:
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/dist/'
},
它將告訴webpack從哪里為您的應用程序提供服務。
3)將以下腳本添加到package.json中
"build": "webpack -p"
它將構建bundle.js文件
另外,您需要在dist目錄中使用index.html文件,如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Application<title>
</head>
<body>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>
然后,你運行你的構建腳本,然后你可以運行webpack服務器,它應該工作
更改import語句以import App from './app.js'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.