2017-01-18 4 views
0

私はes6/react jsとwebpackに新しく、過去6日間から私はes6とwebpackの反応アプリのstartkitを作成しようとしています。 .js、私は正常にWeb Packのdevのサーバーを構成することができます。私のアプリはhttp:/ localhost:8080/webpack-dev-serverで動作しています。 npm buildを実行してbundle.jsを生成しているとき。私のアプリケーションがlocalhost:8080だけを使用している場合、私のアプリはChromeで実行されていますが、mozillaでエラーが発生します(r.renderは機能しません)。 Webpackは非常に混乱しています...ファイルbundle.jsをfile:// serverでローカルに実行できますか?そのbundle.jsファイルをインクルードすると、正常に動作するはずです。あなたはそれを自分でリンクする必要はありません静的なjsを使用して、es3と反応するアプリのためのwebcpackを生成しました

マイwebpack.config.js

var path = require('path'); 
var webpack = require('webpack'); 
//var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js'); 
var mainPath = path.join(__dirname, 'app', 'index.js'); 
var buildPath = path.join(__dirname, 'dist/assets/'); 
var ExtractTextPlugin = require('extract-text-webpack-plugin'); 
var autoprefixer = require('autoprefixer') 
var sassLoaders = [ 
    'css-loader?sourceMap', 
    'postcss-loader', 
    'sass-loader?sourceMap&includePaths[]=' + path.join(__dirname, './app') 
] 

module.exports = { 
    // Makes sure errors in console map to the correct file 
    // and line number 
    devtool: 'cheap-module-source-map', 
    entry: { 
     'vendor': ['react','react-dom'], 
     "bundle":mainPath 
    }, 
    module: { 
     loaders: [ 
     { 
      test: [/\.js$/], 
      loader: 'babel-loader', 
      exclude: /node_modules/, 
      query: { 
      presets: ['es2015', 'react'] 
      } 
     }, 
     // //{ test: /\.less$/, loader: 'style-loader!css-loader!less-loader' }, // use ! to chain loaders 
     // { test: /\.less$/, loader: 'style-loader!css-loader!less-loader' }, // use ! to chain loaders 
     // { test: /\.s?css$/, loaders: ['style', 'css', 'sass','css?sourceMap', 'sass?sourceMap'] } 
     { test: /\.scss$/, 
      loader: ExtractTextPlugin.extract('style-loader', sassLoaders.join('!')) 
     }, 
     { test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, loader: 'file-loader?name=/fonts/[name].[ext]' } 
     ] 
    }, 
    output: { 
     // We need to give Webpack a path. It does not actually need it, 
     // because files are kept in memory in webpack-dev-server, but an 
     // error will occur if nothing is specified. We use the buildPath 
     // as that points to where the files will eventually be bundled 
     // in production 
     path: buildPath, 
     filename: '[name].js', 
     publicPath: 'http://localhost:8080/assets' 
    }, 
    plugins: [ 
     // Pro-tip: Order matters here. 
     new ExtractTextPlugin('[name].css'), new webpack.optimize.CommonsChunkPlugin(['bundle', 'vendor'], '[name].js') 
    ], 
    postcss: [ 
     autoprefixer({ 
     browsers: ['last 2 versions'], 
     //path: "./dist", 
     filename: '[name].js', 
     // Everything related to Webpack should go through a build path, 
     // localhost:8080/build. That makes proxying easier to handle 
     publicPath: '/dist/' 
     }) 
    ], 
    resolve: { 
     extensions: ['', '.js', '.jsx','.sass','.woff','.ttf','.eot','.svg'], 
     root: [path.join(__dirname, './app')] 
    }, 
    watch:true 
}; 

私のindex.html

<!DOCTYPE html> 
<html> 
<head> 
    <title>React Home Page</title> 
    <link rel="stylesheet" href="assets/bundle.css" /> 
</head> 
<body> 
<div id="react-app"></div> 
<script type="text/javascript" src="assets/vendor.js"></script> 
<script type="text/javascript" src="assets/bundle.js"></script> 
</body> 
</html> 

答えて

1

、WebPACKのはあなたのためにそれを行います。

HtmlWebpackPluginは、バンドルが設定したファイルにリンクされていることを確認します。

var path = require("path"); 
var HtmlWebpackPlugin = require("html-webpack-plugin"); 

module.exports = { 
    entry: "./app/index.js", 
    output: { 
    path: path.resolve(__dirname, "dist"), 
    filename: "index_bundle.js" 
    }, 
    module: { 
    rules: [ 
     { test: /\.(js)$/, use: "babel-loader" }, 
     { test: /\.css$/, use: ["style-loader", "css-loader"] } 
    ] 
    }, 
    plugins: [ 
    new HtmlWebpackPlugin({ 
     template: "app/index.html" 
    }) 
    ] 
}; 
関連する問題