简体   繁体   English

使用Webpacker将Lodash添加到新的Rails 5.2应用程序

[英]Adding Lodash to a new Rails 5.2 app with webpacker

So, using Rails 5.2beta ( gem install rails --pre ), if you create a new app via rails new myapp --webpack=react ... how would I go about incorporating Lodash into my flow from there? 因此,使用Rails 5.2beta( gem install rails --pre ),如果您通过rails new myapp --webpack=react创建新的应用程序rails new myapp --webpack=react ...我该如何将Lodash从那里合并到我的流程中?

I've mucked around with babel-plugin-lodash and lodash-webpack-plugin to no avail. 我用babel-plugin-lodashlodash-webpack-plugin搞乱了 ,但无济于事。

The compile doesn't fail if I do something like import { _pick, _map } from 'lodash/array'; 如果我import { _pick, _map } from 'lodash/array';执行import { _pick, _map } from 'lodash/array';编译不会失败import { _pick, _map } from 'lodash/array'; ... but those variables are undefined when trying to use them. ...但是这些变量在尝试使用时是不确定的。

I'm a bit lost as I'm both new to webpacker & webpack, and a lot of existing examples seem to target an older version of webpack? 我有点迷茫,因为我都是webpacker和webpack的新手,并且很多现有示例似乎都针对旧版本的webpack?

Anyway, thank you for any help... 无论如何,谢谢您的帮助...

UPDATE: 更新:

Ok, looks like you can just do import map from "lodash/map" 好的,看来您可以直接import map from "lodash/map"

So where are you trying to use lodash from? 那么,您在哪里尝试使用lodash? By this I mean are you sure the files are getting compiled / processed by Webpacker, or are they getting processed via the Asset Pipeline? 我的意思是,您确定文件是由Webpacker编译/处理的,还是通过Asset Pipeline处理的?

On my webpacker project I realized that there's a problem: that yes, regular old Javascript compiled with the Asset Pipeline won't know about the NPM Modules included via Webpack. 我的webpacker项目中,我意识到存在一个问题:是的,使用Asset Pipeline编译的常规旧Javascript不会知道Webpack包含的NPM模块。 (or at least the require statements wouldn't work). (或至少require语句不起作用)。

Because of this I made a hard rule: all javascript goes where webpacker expect it ( app/javascript ) and no Javascript goes in app/assets . 因此,我制定了一个严格的规则:所有javascript都在webpacker期望的位置( app/javascript )出现,而Javascript 不在 app/assets We only use the asset pipeline for CSS (which works great in our case, as I still don't think React - our frontend framework of choice - has a good story around site wide CSS). 我们仅使用CSS的资产管道(在我们的案例中效果很好,因为我仍然不认为React-我们选择的前端框架-在整个站点范围内都有很好的故事)。

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

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