我可以从 CDN 制作 Reason+React 导入反应模块吗? [英] Can I make Reason+React import react module from CDN?

查看:33
本文介绍了我可以从 CDN 制作 Reason+React 导入反应模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Reason 和 React 构建组件总是会为我提供react"的模块导入语句,如果从 CDN 中包含 React,则无法找到该语句.有解决方案吗?我试图在 index.html 中定义 window.react = React 没有成功.es6-global 设置不会改变任何东西.

Building a component with Reason and React always gives me an module import statement for "react", which cannot be found if React is included from a CDN. Is there a solution for this? I've tried to define window.react = React in index.html without success. es6-global setting does not change anything.

我没有使用像 webpack 这样的打包程序.

I'm not using a bundling program like webpack.

可能来自 Reason 论坛的相关主题:https://reasonml.chat/t/can-one-load-reasonml-es6-modules-without-a-bundler/2219

Possibly relevant thread from Reason forum: https://reasonml.chat/t/can-one-load-reasonml-es6-modules-without-a-bundler/2219

类似问题(未解决):可以一个无需打包器即可加载 reasonml es6 模块

importmap(尚未在浏览器中实现)可能是另一个解决方案:在没有 Transpiler/Bundler 步骤的情况下使用 ES6 模块

importmap (not yet implemented in browsers) could be another solution for this: Using ES6 Modules without a Transpiler/Bundler step

推荐答案

从技术上讲,是的,你可以,但它不会像使用 npm 流程和使用打包器那么容易.

Technically, yes you can, but it's not going to be as easy as going with the npm flow and using a bundler.

ReasonReact 绑定的编写方式可以生成导入模块的输出 JavaScript,例如:

The ReasonReact bindings are written in a way that produces output JavaScript that imports modules like:

import * as React from "react";

(如果使用 ES6 模块样式.)

(If using ES6 module style.)

如果使用 CDN,您可能需要如下所示的输出:

If using a CDN you would probably want an output that looks like this:

import * as React from "https://some.cdn/react";

语法(来自 ReasonReasonRea>) 控制输出JS的是:

The syntax (from the ReasonReact repo) that controls the output JS is:

[@bs.module "react"]
external createElement: (component('props), 'props) => element = "createElement";

如果您将其更改为:

[@bs.module "https://some.cdn/react"]
external createElement: (component('props), 'props) => element = "createElement";

...然后你会得到想要的输出.但问题是您需要更改源……即为该 CDN 维护或查找 React 的分叉绑定.或者设置一些代码自动化,用 [@bs.module "https://some.cnd 查找和替换 [@bs.module "react"]/反应"].所以无论哪种方式,都不像使用打包器那么简单.

...then you'd get the desired output. But the problem is then you need to change the sources ... i.e. maintain or find forked bindings for React for that CDN. Or set up some code automation that does a find-and-replace of [@bs.module "react"] with [@bs.module "https://some.cnd/react"]. So either way, it's not as simple as using a bundler.

这篇关于我可以从 CDN 制作 Reason+React 导入反应模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆