Webpack已成功编译但未在浏览器中显示输出

时间:2017-06-13 07:54:08

标签: reactjs webpack webpack-dev-server jsx

我项目中的webpack成功运行,但是当我到达所需的端口时,它会显示目录文件。你可以看一下图像。

enter image description here

我的webpack.config.js

import { Loading, LoadingController } from 'ionic-angular';

export class ApnSearchPage {
   loading: Loading;
   constructor(private loadingCtrl: LoadingController) { }

   ionViewDidLoad() {
     this.createLoader();
   }

   createLoader(message: string = "Please wait...") { // Optional Parameter
     this.loading = this.loadingCtrl.create({
       content: message
     });
   }

   public searchClick() {
       this.createLoader();
       this.loading.present().then(() => {
       this.searchService.submitRequest(params, data)
            .subscribe(response => {
                this.loading.dismiss();
            }, error => {
              this.loading.dismiss();
              this.errorMessage = <any>error
            });
      });
   }
}

的package.json

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'
      }
    ]
  }
}

main.js

{
  "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"
  }
}

app.js

import React from 'react'
import ReactDOM from 'react-dom'
import App from './app'
ReactDOM.render(<App />, document.getElementById('root'));

输出==&GT;

enter image description here

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

编辑回答: 我能够让它发挥作用:

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

答案 1 :(得分:0)

将导入语句更改为import App from './app.js'