2016-08-04 15 views
0

これで、react、es6、webpack/babelを学習しています。私は私のwebpack.configは、以下の設定だ:ES6モジュール、babel/webpack。インポート/エクスポートステートメントが機能しない

const path = require('path'); 
const HtmlWebpackPlugin = require('html-webpack-plugin'); 
const merge = require('webpack-merge'); 
const validate = require('webpack-validator'); 
const parts = require('./config/webpack-parts'); 

const PATHS = { 
    app: path.join(__dirname, 'app'), 
    build: path.join(__dirname, 'build') 
}; 

const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({ 
    template: PATHS.app + '/index.html', 
    filename: 'index.html', 
    inject: 'body' 
}); 

const common = { 
    entry: { 
     app: PATHS.app 

    }, 

    output: { 
     path: PATHS.build, 
     filename: '[name].js' 

    }, 

    module: { 
     loaders: [ 
      { 
       test: /\.jsx?$/, 
       exclude: /node_modules/, 
       loader: "babel-loader" 
      } 
     ] 
    }, 

    plugins: [HTMLWebpackPluginConfig] 

}; 

var config; 

switch(process.env.npm_lifecycle_event) { 
    case 'build': 
     config = merge(
      common, 
      { 
       devtool: 'source-map', 
       output: { 
        path: PATHS.build, 
        filename: '[name].[chunkhash].js', 
        // This is used for require.ensure. The setup 
        // will work without but this is useful to set. 
        chunkFilename: '[chunkhash].js' 
       } 
      }, 
      parts.clean(PATHS.build), 
      parts.extractBundle({ 
       name: 'vendor', 
       entries: ['react'] 
      }), 

      parts.minify(), 
      parts.extractCSS(PATHS.app) 
     ); 
     break; 
    default: 
     config = merge(
      common, 
      parts.setupCSS(PATHS.app), 
      { 
       devtool: 'eval-source-map' 
      }, 
      parts.devServer({ 
       // Customize host/port here if needed 
       host: process.env.HOST, 
       port: process.env.PORT 
      }) 
     ); 
     break; 
} 

module.exports = validate(config); 

「設定/ WebPACKの部品」ファイルがちょうど質問のために必要ないくつかの余分なモジュール/プラグインとではありません。これをプロジェクト全体で再利用可能にすることを目指していました。私はまた、すべてのnesscaryバベル/ WebPACKのプラグインは私のpackage.jsonファイル内に設置されてしまっている私も、設定バベルを得た、と.babelrcファイルが

{ 
    "presets": [ 
    "es2015", 
    "react" 
    ] 
} 

を下回っている:

"devDependencies": { 
    "babel-core": "^6.11.4", 
    "babel-loader": "^6.2.4", 
    "babel-preset-es2015": "^6.9.0", 
    "babel-preset-react": "^6.11.1", 
    "clean-webpack-plugin": "^0.1.10", 
    "css-loader": "^0.23.1", 
    "extract-text-webpack-plugin": "^1.0.1", 
    "html-webpack-plugin": "^2.22.0", 
    "style-loader": "^0.13.1", 
    "uglify-loader": "^1.3.0", 
    "webpack": "^1.13.1", 
    "webpack-dev-server": "^1.14.1", 
    "webpack-merge": "^0.14.1", 
    "webpack-validator": "^2.2.7" 
    }, 
    "dependencies": { 
    "react": "^15.3.0", 
    "react-dom": "^15.3.0" 
    } 

だから今問題。私はこのすべてが動作していることをテストしており、es6 +反応コードをes5に変えています。私は、テスト 'hello.js'と 'world.js'ファイルを設定していて、それらをエントリ/メインファイル 'index .js '。これがエラーの原因です。

hello.js

import React, {Component} from 'react'; 

export class Hello extends Component { 

    render() { 
     return (
      <h1>Hello</h1> 
     ) 
    } 
} 

world.js

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Weather App</title> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
</head> 
<body> 
    <div id="hello"></div> 
    <div id="world"></div> 
</body> 
</html> 
index.htmlを

import React, {Component} from 'react'; 

export class World extends Component { 
    render() { 
     return (
      <h1>World</h1> 
     ) 
    } 
} 

index.js

import React, {Component} from 'react'; 
import ReactDOM from 'react-dom'; 
import Hello from 'hello'; 
import World from 'world'; 

ReactDOM.render(<Hello/>, document.getElementById('hello')); 
ReactDOM.render(<World/>, document.getElementById('world')); 

前ウェブパック

ウェブパックを実行してそのことを行うと、次のエラーが発生します。

ERROR in ./app/index.js 
Module not found: Error: Cannot resolve module 'hello' in /Users/Foysal/Google Drive/Learning Projects/ReactJS-Tutorial/weather-app/app 
@ ./app/index.js 11:13-29 

「world.js」ファイルでも同様のエラーが表示されますが、インポート/エクスポート文に何が問題なのかわかりません。私はes6モジュールの新機能ですが、私が知っている限り、正しく輸出して輸入しています。助けていただければ幸いです。ありがとうございます。

+0

これらのファイルは、インデックスとの関連でどこにありますか? – aw04

+0

以下の正解を確認しました。それは、ファイルがインデックスに関連していた場所と関係していました。 – Foysal94

答えて

4

ファイルを必要としているときには、例えば、相対パスを使用する必要があります。あなたの例では

import React, {Component} from 'react'; 
import ReactDOM from 'react-dom'; 
import Hello from '../components/hello'; 
import World from '../components/world'; 

を、node_modulesディレクトリ内helloworldモジュールを探しているノード。

+0

あなたはそれをどのように伝えることができますか?そしてそれが問題でした。 hello.js、world.js、index.html、index.jsはすべて同じフォルダ内にあります。なぜ、デフォルトでは同じディレクトリ内にないのですか?モジュールがes6で動作する方法ですか?ありがとう – Foysal94

+0

@ Foysal94、はい、デフォルトでは、ポイントパスでなければ、モジュールは 'node_modules'ディレクトリで検索されます。ファイルが必要な場合は、常にパスを提供する必要があります。すべてのファイルが同じディレクトリにある場合は、 'import Hello from '/ hello''を使います。 – 1ven

関連する問題