繁体   English   中英

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

[英]React standalone bundle without react library inside

我已经创建了独立的包,我将其包含在React App中。 App本身对加载的Extension Bundle(下一个XB)一无所知,但XB确实了解App及其在内部使用的库。 如何使与XB一起编译的App共享库软件包? 我的意思是,如果该应用程序使用React和React-dom库进行编译,以使XB排除使用此类库进行编译,而是从该应用程序中使用它们?

我放入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'});

是的,这使我在退出时不带React库的Bundle(因此其大小从190KB减少到10KB)。 但是,如果我尝试加载它,则会出现错误:

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

没有外部,一切正常。

index.js?7afc:1是

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

import React from 'react';里面import React from 'react'; 作为module.exports = require('react');

我不明白如何使要求工作。 似乎对于全球空间是不可见的。

更新:我试图更改任何可用选项上的libraryTarget:'umd','commonjs','this'。 结果是一样的。

我从webpack知道commonChunkPlugin ,但是我有几个来自不同来源的项目,它们是由不同的人编译的。

从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