简体   繁体   English

如何修复require.ensure错误Webpack,Babel6,React?

[英]How to fix require.ensure error Webpack, Babel6, React?

I trying to use require.ensure to create a chunk for Facility.js 我试图使用require.ensure为Facility.js创建一个块

import React, { Component } from 'react';
import { render } from 'react-dom';
class Facility extends Component {
    constructor(...args) {
        super(...args);
        this.state = {
              ....
        };
    }
    render() {
        return (
            <div>
                      ....
            </div>
        );
    }
}

export default Facility;
//Tried module.exports = Facility;

NonPatientSer.js NonPatientSer.js

var Facility;
require.ensure([], function(require) { Facility = require('./Facility'); }, 'facility');

Dev I can see facility.bundle.js in my sources but the call for it returns undefined and this errors 开发人员,我可以在源代码中看到facility.bundle.js,但对其的调用返回未定义的错误

Warning: React.createElement: type should not be null, undefined, boolean, or number. 警告:React.createElement:type不能为null,undefined,boolean或number。 It should be a string (for DOM elements) or a ReactClass (for composite components). 它应该是一个字符串(对于DOM元素)或一个ReactClass(对于复合组件)。 Check the render method of NonPatientSer 检查NonPatientSer的渲染方法

and

Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. 未捕获的不变违反:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 Check the render method of NonPatientSer . 检查NonPatientSer的渲染方法。

Production (run build) 生产 (运行构建)

Not allowed to load local resource: file:///C:/Users/.../facility.bundle.js 不允许加载本地资源:file:/// C:/Users/.../facility.bundle.js

Webpack config Webpack配置

output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath:  __dirname + '/',
    chunkFilename: '[name].bundle.js'
  },

First, you need to fix publicPath property in your webpack.config. 首先,您需要在webpack.config中修复publicPath属性。 You should use: 您应该使用:

publicPath:  '/dist/'

instead absolute path. 而是绝对路径。

At second, if you use es6 syntax for export module: 第二,如果对导出模块使用es6语法:

export default Facility;

you should write your import code like this: 您应该这样编写导入代码:

require.ensure([], function(require) {
  Facility = require('./Facility').default;
  ReactDOM.render(<Facility/>, document.getElementById('app'));
}, 'facility');

Or just to use commonjs syntax: 或者只是使用commonjs语法:

module.exports = Facility;

and then 接着

require.ensure([], function(require) {
  Facility = require('./Facility');
  ReactDOM.render(<Facility/>, document.getElementById('app'));
}, 'facility');

See also: http://www.2ality.com/2015/12/babel-commonjs.html 另请参阅: http : //www.2ality.com/2015/12/babel-commonjs.html

Update: You can set publicPath in your entry js file. 更新:您可以在条目js文件中设置publicPath。 For example: 例如:

entry.js entry.js

__webpack_public_path__ = '/publicPathToChunk/';

var chunk;
require.ensure([], function(require) { 
  chunk = require('./chunk'); 
}, 'chunk');

Also, you can use next trick: 另外,您可以使用下一个技巧:

var bundleSrc = document.querySelector('[src*="bundle"]').getAttribute("src");
__webpack_public_path__ = bundleSrc.substr(0, bundleSrc.lastIndexOf("/") + 1);

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

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