2017-06-13 249 views
0

我的项目中的webpack成功运行,但是当我到达所需的端口时,它会显示目录文件。你可以看到相同的图像。Webpack成功编译但在浏览器中不显示输出

enter image description here

我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 

输出==>

enter image description here

任何帮助,将不胜感激。

+0

您没有用于显示webpack-dev-server的'index.html'文件。添加一个'index.html'文件,并带有指向你的包的链接。 – nrgwsth

+0

我在App目录中有index.html文件。但是,我如何将它添加到捆绑包? –

+0

您需要在'index.html'中提供'bundle'脚本url – nrgwsth

回答

0

编辑答案: 我能得到它的工作:

1)的根文件夹 2)下创建实际的dist目录让你的输出在webpack.config.js是这样的:

output: { 
    path: path.resolve(__dirname, 'dist'), 
    filename: 'bundle.js', 
    publicPath: '/dist/' 
}, 

它会告诉webpack从哪里为您的应用程序提供服务。

3)将下列脚本添加到您的package.json

"build": "webpack -p" 

将建立bundle.js文件

还,您将需要一个index.html文件在你的dist目录,看起来像这样:

<!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服务器,它应该工作

+0

它还没有工作 –

+0

你真的有一个dist目录吗?你是否也可以发布构建输出? –

+0

不,它不会生成dist文件夹,但它说webpack编译成功。 –

相关问题