简体   繁体   中英

Rails 5: How to include JS global functions with webpacker?

Let's say I have to load a js function with turbolinks:

document.addEventListener("turbolinks:load", function() {
    myFunction();
  }
);

But the function is very large and I wanted to avoid to added it to application.js . Thus I succeded to import it with exports-loader , for example:

import { myFunction } from 'exports-loader?exports=myFunction!./file.js';

But how to import a global function like window.myFunction ?

This worked for me (in webpack.config.js )

loaders: [{
    test: /\/myFunction$/,
    loader: 'exports?window.myFunction'
}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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