简体   繁体   English

React独立捆绑包,内部没有React库

[英]React standalone bundle without react library inside

I have created standalone bundle which I include in React App. 我已经创建了独立的包,我将其包含在React App中。 The App itself does not anything know about Extension Bundle (next XB) it load, but the XB does know about App and libraries it use inside. App本身对加载的Extension Bundle(下一个XB)一无所知,但XB确实了解App及其在内部使用的库。 How to make App share library packages it compiled with to XB? 如何使与XB一起编译的App共享库软件包? I mean if the App compiled with React and React-dom libraries to make XB exclude from compile with such libraries and use them from the App instead? 我的意思是,如果该应用程序使用React和React-dom库进行编译,以使XB排除使用此类库进行编译,而是从该应用程序中使用它们?

I put into webpack config to exclude React from compiling: 我放入webpack配置中以将React排除在编译之外:

var config = {
//....
    output: {
        library: 'videoEditor',
        libraryTarget: 'umd',
        path: BUILD_PATH+"./../bundles/",
       filename: '[name].bundle.js'
   },
// ...
}

// .... later just extending config if compiling as standalone
config.externals = Object.assign(config.externals,{'react':'React'});

Yes, that gives me on exit the Bundle without React library (so it size reduces from 190KB to 10KB). 是的,这使我在退出时不带React库的Bundle(因此其大小从190KB减少到10KB)。 But if I try to load it I get an error: 但是,如果我尝试加载它,则会出现错误:

index.js?7afc:1 Uncaught ReferenceError: require is not defined
    at eval (index.js?7afc:1)
    at Object.<anonymous> (video-editor.bundle.js:80)
    at __webpack_require__ (video-editor.bundle.js:30)
    at video-editor.bundle.js:73
    at video-editor.bundle.js:76
    at webpackUniversalModuleDefinition (video-editor.bundle.js:9)
    at video-editor.bundle.js:10

Without externals all is working ok. 没有外部,一切正常。

index.js?7afc:1 is index.js?7afc:1是

import React from 'react';
import AppLayout from 'layouts/app';

There inside import React from 'react'; import React from 'react';里面import React from 'react'; is coming as module.exports = require('react'); 作为module.exports = require('react');

I do not understand how to make require work. 我不明白如何使要求工作。 Seems it is invisible for global space. 似乎对于全球空间是不可见的。

update: I have tried to change libraryTarget on any available option: 'umd', 'commonjs', 'this'. 更新:我试图更改任何可用选项上的libraryTarget:'umd','commonjs','this'。 The result is the same. 结果是一样的。

I know about commonChunkPlugin from webpack but I have several separated projects from different sources they compile by different people. 我从webpack知道commonChunkPlugin ,但是我有几个来自不同来源的项目,它们是由不同的人编译的。

Along with React, you also need to add ReactDOM in your externals from React v0.14.x onwards 从React v0.14.x开始,您还需要与React一起在外部中添加ReactDOM

externals: {
    // Use external version of React
    "react": "React",
    "react-dom": "ReactDOM"
},

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

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