【发布时间】:2018-05-10 14:45:43
【问题描述】:
我正在使用 webpack 和 babel 来构建我的应用,并且应用在 bundle.js 文件中成功构建。
我有包含此代码的 index.jsx 文件
import React from 'react';
import { render } from 'react-dom';
import App from './App';
render(<App />, document.getElementById('app'));
App 组件加载了一堆其他组件。
我在同一个应用程序中有 index.html 文件。
<html>
<head>
<meta charset="utf-8">
<title>My App</title>
</head>
<body>
<div id="app" />
<script src="public/bundle.js" type="text/javascript"></script>
</body>
</html>
我正在从系统目录打开 index.html 文件,直接在浏览器中,应用程序运行良好。
我想要做的是使用 bundle.js 文件并将我的脚本加载到另一个 html/javascript 应用程序中,该应用程序在其中运行相同的应用程序。
我发现了这个Writing embeddable Javascript plugin with React & Webpack 链接,它显示了他是如何设法使用它的。
他使用的地方
export const init = (config) => {
ReactDOM.render(<MyReactApp config={config} />, someSelector);
}
如何转换我的 index.jsx 文件,以便可以在另一个应用程序中使用,就像它在回答给出的链接时使用的方式一样。
【问题讨论】:
-
我已将其添加到 webpack.config.js 文件中作为输出:{ path: BUILD_DIR, filename: 'bundle.js', library: 'MyApp', libraryTarget: 'umd', umdNamedDefine: true , },
标签: javascript reactjs webpack babeljs