Giter Site home page Giter Site logo

Comments (3)

seanpdoyle avatar seanpdoyle commented on July 28, 2024 6

This engine is built to leverage ECMAScript Modules.

If your project has an external dependency, you have several options.

First, declare the import statement in the module that needs it: import day from "dayjs".

Next, configure your server so that it knows how to resolve the dayjs package name. You have several options, including:

  • downloading the source as a vendored package or a gem that bundles its assets, configuring how to resolve the name in your importmap.json with a project-relative path (e.g. "dayjs": asset_path("dayjs/dayjs.min.js")), then serving it from the Rails application
  • downloading the source as a node_modules package, configuring how to resolve the name in your importmap.json with a project-relative Webpacker path (e.g. "dayjs": asset_pack_path("dayjs/dayjs.min.js")), then serving it from the Rails application
  • configuring how to resolve the name in your importmap.json with an absolute URL (e.g. "dayjs": "https://cdn.skypack.dev/dayjs")
  • declaring the import statement directly in your module with an absolute URL (e.g. import day from "https://cdn.skypack.dev/dayjs")

Have you tried any of these other alternatives?

from stimulus-rails.

turgs avatar turgs commented on July 28, 2024 6

Ok i've finally figured out 👏 how to import a library. Maybe this is common knowledge for others but ES6 is a brave new world for me. I'm generally stuck back in Year 2000 Javascript.

I didn't want to use a gem that bundled the library for the asset pipeline, and I didn't want to call skypack or a remote CDN. I wanted to serve it locally.

  1. I went to Skypack to get their browser-optimized packaged version of Dayjs: https://cdn.skypack.dev/dayjs and saved the content of that to /app/assets/javascripts/libraries/dayjs/dayjs.js.erb (note .erb). That file looked like this on Skypack.

    export * from '/-/[email protected]/dist=es2020,mode=imports/optimized/dayjs.js';
    export {default} from '/-/[email protected]/dist=es2020,mode=imports/optimized/dayjs.js';

    which I changed to the following, using asset_path:

    export * from '<%= asset_path "libraries/dayjs/dayjs.export.js" %>';
    export {default} from '<%= asset_path "libraries/dayjs/dayjs.export.js" %>';
  2. That originally referenced https://cdn.skypack.dev//-/[email protected]/dist=es2020,mode=imports/optimized/dayjs.js file, I saved unchanged to /app/assets/javascripts/libraries/dayjs/dayjs.export.js

  3. In my importmap.json.erb I added:

     {
       "imports": { 
         "turbo": "<%= asset_path "turbo" %>",
         <%= importmap_list_with_stimulus_from "app/assets/javascripts/controllers", "app/assets/javascripts/libraries" %>,
    +    "dayjs": "<%= asset_path "libraries/dayjs/dayjs.js" %>"
       }
     }
  4. In my Stimulus controller, I added:

      import { Controller } from "stimulus"
    + import dayjs from 'dayjs';

Thanks for a prod that led me in the right direction 🥳

from stimulus-rails.

turgs avatar turgs commented on July 28, 2024 6

Better yet, I've just realised that I don't need to do this required "date logic" and calculations in javascript anymore, because I should now be able to use a turbo frame and keep the logic in Rails 🎉

from stimulus-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.