简体   繁体   English

如何使用create-react-app中公用文件夹中的脚本?

[英]How to use scripts from the public folder in create-react-app?

I have built some WASM (and some simple wrapper code) using rust and wasm-pack . 我已经使用rust和wasm-pack构建了一些WASM(以及一些简单的包装器代码)。 Unfortunately putting this code in the src folder causes it to be processed by webpack, which breaks. 不幸的是,将此代码放在src文件夹中会导致webpack对其进行处理,这会中断。 I'm using create-react-app and I don't want to eject (precisely because I don't want to learn how to work with or configure webpack). 我正在使用create-react-app,但我不想弹出(正是因为我不想学习如何使用或配置Webpack)。

There are many github issues devoted to solving this issue with more libraries, which seem to also not work (or work for some people but not others, or etc.). 有许多github问题致力于使用更多库来解决此问题,这些库似乎也不起作用(或对某些人有效,而对其他人无效,等等)。 The standard libraries don't seem to interact very well, and while they will probably get better with time, the fact is that in simple JS, we can do this simply. 标准库似乎不能很好地交互,尽管它们可能会随着时间的推移而变得更好,但事实是,在简单的JS中,我们可以简单地做到这一点。 For example in https://rustwasm.github.io/docs/book/game-of-life/hello-world.html we see that we can just use the generated code as an npm package on its own, no problem, so long as we're not using webpack. 例如,在https://rustwasm.github.io/docs/book/game-of-life/hello-world.html中,我们看到我们可以将生成的代码单独用作npm包,没问题,所以只要我们不使用webpack。

So my idea is to just put this code in the public folder, have it delivered to the client as-is without packing, and be done. 所以我的想法是将这段代码放在public文件夹中,将其按原样传递给客户端,而无需打包,然后完成。 However I can't figure out the syntax for actually importing code from those folders. 但是我无法弄清楚从这些文件夹中实际导入代码的语法。 How do I do it? 我该怎么做?

Note that the possible duplicates I found are about importing images or etc. from the public folder, using <link> or etc., but I don't believe that solves my use-case, since I need those javascript objects and their associated functions, to call in my code. 请注意,我发现的可能重复项与使用<link>等从公共文件夹中导入图像等有关,但是我不认为这可以解决我的用例,因为我需要那些javascript对象及其相关函数,以调用我的代码。

You can use rust-webpack-template . 您可以使用rust-webpack-template It is a kickstarter which embedded rust-wasm-pack with webpack. 这是一个将rust-wasm-pack嵌入webpack的kickstarter。

After that you can simply add react + babel in webpack by follow this tuto . 之后,您只需按照此tuto在Webpack中添加react + babel。 There are some minors changes in webpack.config.js to do. 在webpack.config.js中有一些小的更改要做。

I used this for my own litte project with rust + react. 我将此用于我自己的带有rust + react的小项目。 My repo . 我的回购

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

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