简体   繁体   English

带Webpack和Babel的ES6模块加载

[英]ES6 Module loading with Webpack and Babel Issue

My compiled jsx module scripts are not compiling for some reason. 由于某些原因,我的已编译jsx模块脚本未编译。

Here is the directory structure 这是目录结构

  • dist dist
    • bundle.js bundle.js
  • node_modules node_modules
  • scripts 剧本
    • helloWorldComponent helloWorldComponent
      • helloWorldBox.jsx helloWorldBox.jsx
      • helloWorldDisplay.jsx helloWorldDisplay.jsx
    • main.js main.js
  • index.html index.html
  • package.json package.json
  • webpack.config.js webpack.config.js

Here are my 2 jsx files 这是我的2个jsx文件

  • helloWorldBox.jsx helloWorldBox.jsx

 import React from 'react'; import ReactDOM from 'react-dom'; import helloWorldDisplay from './helloWorldDisplay.jsx'; var helloWorldBox = React.createClass({ render : function(){ return ( <div> <helloWorldDisplay/> </div> ); } }); ReactDOM.render(<helloWorldBox/>, document.getElementById('output')); 

  • helloWorldDisplay.jsx helloWorldDisplay.jsx

 import React from 'react'; var helloWorldDisplay = React.createClass({ render : function(){ return ( <div> Hello World </div> ); } }); 

  • main.js file main.js文件

 import helloWorldBox from './helloWorldComponent/helloWorldBox.jsx'; import helloWorldDisplay from './helloWorldComponent/helloWorldDisplay.jsx'; 

When my bundle.js gets created by webpack it looks like this 当我的bundle.js由webpack创建时,它看起来像这样

 /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports) { import helloWorldBox from './helloWorldComponent/helloWorldBox.jsx'; import helloWorldDisplay from './helloWorldComponent/helloWorldDisplay.jsx'; /***/ } /******/ ]); 

Here is the webpack.config.js file 这是webpack.config.js文件

 var path = require('path'); var webpack = require('webpack'); module.exports = { entry: './scripts/main.js', output: { path: __dirname + '/dist', filename: 'bundle.js' }, module: { loaders: [ { test: /\\.jsx$/, loader: 'babel-loader', exclude: /(node_modules|bower_components)/, query: { presets: ['es2015', 'react'] } } ] }, }; 

import React from 'react';

var helloWorldDisplay = React.createClass({
    render : function(){
        return (
            <div>
               Hello World
            </div>
        );
    }
});

export default helloWorldDisplay

Add the export helloworldDisplay file 添加导出helloworldDisplay文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM