简体   繁体   English

有没有统一的方式在 Rails 6 中安装 JS 第三方库(比如 QuillJS)?

[英]Is there a unified way to install JS third party libraries (like QuillJS) in Rails 6?

I installed Rails 6 RC1 and I wanted to create a new project with it, however it turn out to a nightmare because of the webpack default feature, am facing a hard time installing third party JS libraries, if you want to add jQuery and bootstrap for example you should do some weird setup in your environment.js like this我安装了 Rails 6 RC1,我想用它创建一个新项目,但是由于 webpack 默认功能,它变成了一场噩梦,如果你想为例如你应该像这样在你的environment.js做一些奇怪的设置

environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
        $: 'jquery',
        JQuery: 'jquery',
        jquery: 'jquery',
        Popper: ['popper.js', 'default'], // for Bootstrap 4
    })
)

and if you want to add jQuery-ui you have to search and hopefully you can find some link that show you how like this one如果你想添加jQuery-ui,你必须搜索,希望你能找到一些链接,向你展示这个链接

// jquery
import $ from 'jquery';

global.$ = $
global.jQuery = $


require('jquery-ui');

// jquery-ui theme
require.context('file-loader?name=[path][name].[ext]&context=node_modules/jquery-ui-dist!jquery-ui-dist', true,    /jquery-ui\.css/ );
require.context('file-loader?name=[path][name].[ext]&context=node_modules/jquery-ui-dist!jquery-ui-dist', true,    /jquery-ui\.theme\.css/ );

Now what if you want to add QuillJS ?现在如果你想添加 QuillJS 怎么办? you might do a research like me and end up in theQuillJS doc which tells you to add :您可能会像我一样进行研究并最终在QuillJS 文档中告诉您添加:

import Quill from 'quill/core';

import Toolbar from 'quill/modules/toolbar';
import Snow from 'quill/themes/snow';

import Bold from 'quill/formats/bold';
import Italic from 'quill/formats/italic';
import Header from 'quill/formats/header';


Quill.register({
  'modules/toolbar': Toolbar,
  'themes/snow': Snow,
  'formats/bold': Bold,
  'formats/italic': Italic,
  'formats/header': Header
});


export default Quill;

Just to find out that the QuillJs UI doesn't show up !!只是为了发现 QuillJs UI 没有出现!

As you see a lot of code just to install 2 or 3 libraries, then I spent a lot of time just searching how to a add things, or how to make the code you already found/added working...正如你看到很多代码只是为了安装 2 或 3 个库,然后我花了很多时间只是搜索如何添加东西,或者如何使你已经找到/添加的代码工作......

The asset pipeline was a lot easier and fast, why complicate our lives with webpack just because it's the new thing in the JS world ??资产管道变得更加容易和快速,为什么仅仅因为 webpack 是 JS 世界中的新事物就用 webpack 使我们的生活复杂化?

Is there any way to just install JS libraries easily ?有什么方法可以轻松安装 JS 库吗? I prefer to add them manually than to use this webpack thing!我更喜欢手动添加它们而不是使用这个 webpack 东西!

To answer my question... there is no thing such as a "unified way", you can surely create some kind of folder within your javascript folder and download the third-party library you want then import it in your application.js and that's will work fine, But if you are doing this then why using a package manager (Yarn) or webpacker at all?要回答我的问题......没有诸如“统一方式”之类的东西,您当然可以在您的 javascript 文件夹中创建某种文件夹并下载您想要的第三方库,然后将其导入您的 application.js 中,那就是会正常工作,但如果你这样做,那么为什么要使用包管理器(Yarn)或 webpacker 呢?

I wrote an article that explains some few tips about webpack on rails and I explained how you can install and use QuillJS for example... here is the link:我写了一篇文章,解释了一些关于 Rails 上 webpack 的一些技巧,并解释了如何安装和使用 QuillJS 例如……这里是链接:

https://medium.com/@technoblogueur/rails-6-and-webpacker-what-you-need-to-know-e3534fded7ff https://medium.com/@technoblogueur/rails-6-and-webpacker-what-you-need-to-know-e3534fded7ff

I will try to find some time and get back to this answer to update it with more code and examples but in the meantime the article above can save your day :)我会尽量找一些时间回到这个答案,用更多的代码和示例更新它,但与此同时,上面的文章可以节省你的一天:)

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

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