[英]How to import a Backbone/RequireJS module into a React/Webpack app
We have a legacy Backbone application that we are moving over to React.我们有一个遗留的 Backbone 应用程序,我们正在迁移到 React。 An interim step we are attempting is loading a bundled Backbone module into a React page, until we have time to fully rewrite it.
我们正在尝试的一个临时步骤是将捆绑的 Backbone 模块加载到 React 页面中,直到我们有时间完全重写它。 I am halfway there, I can bundle up the app and all its dependencies with r.js using a config like this:
我已经完成了一半,我可以使用如下配置将应用程序及其所有依赖项与 r.js 捆绑在一起:
({
...
baseUrl: './',
name: 'myapp',
paths: {
'myapp': './legacy/app'
},
out: 'src/appbuilt.js'
...
})
The module is set up like this:模块设置如下:
define(function(require) {
var $ = require('jquery'),
_ = require('underscore'),
...
templates = $(require('text!templates/app.html')),
app = {};
app.View = .....
app.Model = .....
return app;
});
That bundle works on the Backbone side.该捆绑包适用于 Backbone 端。 Next I need to turn that into something I can import into React and render.
接下来我需要把它变成我可以导入 React 并渲染的东西。 I am trying things like this:
我正在尝试这样的事情:
npx babel src/appbuilt.js --out-file src/appbuilt.es6.js --plugins=@babel/transform-modules-umd
Which works to give me a UMD module, but importing it like this:这可以给我一个 UMD 模块,但是像这样导入它:
import * as legacyapp from '../../appbuilt.es6';
Gives me warnings on the build like:给我关于构建的警告,例如:
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted关键依赖:要求 function 以无法静态提取依赖的方式使用
And errors on page load that are probably symptoms of something:页面加载错误可能是某些症状:
Uncaught TypeError: name.split is not a function未捕获的类型错误:name.split 不是 function
What is the secret sauce to get my module converted into something I can use?将我的模块转换为我可以使用的东西的秘诀是什么? I am open to modifying how the Backbone app does its imports, or making a wrapper of some kind that is more easily translatable.
我愿意修改 Backbone 应用程序的导入方式,或者制作某种更容易翻译的包装器。
I am not sure but I can guess that the format of your AMD modules is the problem here.我不确定,但我可以猜测您的 AMD 模块的格式是这里的问题。 Try converting them to regular AMD modules:
尝试将它们转换为常规 AMD 模块:
define(
['jquery', 'underscore', 'text!templates/app.html' /* add rest of dependencies here */],
function ($, underscore, templates /** add parameters for rest of dependencies here */)
{
var app = {};
// app.View = ...
// app.Model = ...
return app;
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.