admin管理员组文章数量:1313817
I'm new to webpacker and yarn. I installed successfully the package with:
yarn add moment
EDIT2:
This is my import
# app/javascript/packs/application.js
import moment from 'moment/moment'
import 'moment/locale/de-ch'
Problem: I can't use the "moment" package in my old JS asset files
First works, the other not:
# in: app/javascript/packs/application.js
console.log('Log: ' + moment([2007, 0, 29]).toNow()) #=> Log: in 10 Jahren
# in app/*assets*/javascripts/application.js
console.log('Log2: ' + moment([2007, 0, 29]).toNow()) #=> Uncaught ReferenceError: moment is not defined
EDIT1:
Here the key points of the installation process of webpacker:
- in gemfile: gem 'webpacker', github: 'rails/webpacker'
- Add this line to assets.rb: Rails.application.config.assets.paths << Rails.root.join('node_modules')
- Add in %head of application.html.haml this: = javascript_pack_tag 'application'
- Restart Rails server and start webpacker
Note: I upgraded my app from Rails 4.2 to 5.0 and later to 5.1; maybe something is missing in my app
I'm new to webpacker and yarn. I installed successfully the package with:
yarn add moment
EDIT2:
This is my import
# app/javascript/packs/application.js
import moment from 'moment/moment'
import 'moment/locale/de-ch'
Problem: I can't use the "moment" package in my old JS asset files
First works, the other not:
# in: app/javascript/packs/application.js
console.log('Log: ' + moment([2007, 0, 29]).toNow()) #=> Log: in 10 Jahren
# in app/*assets*/javascripts/application.js
console.log('Log2: ' + moment([2007, 0, 29]).toNow()) #=> Uncaught ReferenceError: moment is not defined
EDIT1:
Here the key points of the installation process of webpacker:
- in gemfile: gem 'webpacker', github: 'rails/webpacker'
- Add this line to assets.rb: Rails.application.config.assets.paths << Rails.root.join('node_modules')
- Add in %head of application.html.haml this: = javascript_pack_tag 'application'
- Restart Rails server and start webpacker
Note: I upgraded my app from Rails 4.2 to 5.0 and later to 5.1; maybe something is missing in my app
Share Improve this question edited Jul 26, 2017 at 23:02 Pascal asked Jul 26, 2017 at 21:14 PascalPascal 1,17817 silver badges21 bronze badges 6- I use moment.js in a regular asset JS-file: app/assets/javascript/<my_file>.coffee – Pascal Commented Jul 26, 2017 at 21:17
-
Did you try
import { moment } from 'moment/moment'
? – Sebastián Palma Commented Jul 26, 2017 at 21:19 - @palma I did now; still the error – Pascal Commented Jul 26, 2017 at 21:23
- Do I have to include the old "asset"-tree to webpacker? I installed webpacker today and it's not an new rails app – Pascal Commented Jul 26, 2017 at 21:25
- Can you import any other ponent, library? How are you testing the import of moment, I've tried your second way and it works. – Sebastián Palma Commented Jul 26, 2017 at 21:30
2 Answers
Reset to default 11I has a similar issue that momentjs was being used throughout the app/assets/javascript files and it was included via gem (actually, even worse, rails-assets). Looks like we both needed moment object to be available globally, not just inside the pack. So, the following fixed that for me and I'll keep it around until I get rid of app/assets/javascript files.
import moment from 'moment'
window.moment = moment
First I thought this is a solution, but it's not: https://github./rails/webpacker#resolved-paths
In "/config/webpacker.yml" I added this:
# /config/webpacker.yml
resolved_paths: ['app/assets']
An here I imported the files that use "moment.js":
# /app/javascript/packs/application.js
import 'javascripts/<folder>/<some_file_uses_moment>.coffee'
=> Still the error
Later I fount this article: http://samuelmullen./articles/embracing-change-rails51-adopts-yarn-webpack-and-the-js-ecosystem/
What happened to the Asset Pipeline? Long story short: nothing. You can still use the asset pipeline the way you always have – ”If you like your asset pipeline, you can keep your asset pipeline”. That includes the javascripts/ directory. You can even mix packaged files with “pipelined” files in your views. You just can’t include your packaged files into your “pipelined” files; well, not easily.
It seems I can't mix the "moment" package with "old" asset JS (?)
版权声明:本文标题:javascript - Rails 5.1 Webpacker – added moment.js via yarn – how to use package in old asset JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741932228a2405642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论