I'm enqueuing scripts with dependency in functions.php
file like:
wp_enqueue_script('ads', get_template_directory_uri() . '/js/ads.js', array( ), '1.6', true); wp_enqueue_script('banners', get_template_directory_uri() . '/js/banners.js', array( 'ads-REKLAMY' ), $timestamp = filemtime( plugin_dir_path( __FILE__ ) . '/js/banners.js' ), true);
banners.js
is a script which uses imported arrays from ads.js
. I need to update ads.js
file automatically whenever I make changes there.
For now I use version parameter like "1.6"
. So I need to manually change it to, for example, "1.7"
and change import line in banners.js
file.
Of course banners.js
updates automatically with timestamp. Is there a way to do that with dependancy file as well?
And do I do that with import in file using dependency? Basically I need ads.js
, the dependency file with exported arrays, to update automatically whenever I make changes there. Any solution will do except Webpack.
array( 'ads-REKLAMY' )
, why is it notarray( 'ads' )
? Is there another file with handleads-REKLAMY
?ads-REKLAMY
to get the change fromads.js
. That is not necessary. Please check my answer and let me know if it works for you.array( 'ads' )
i was just changing names a bit and forgot to change that one too and yea you understood my problem correctly but what about import in banners.js file should it be justimport { selectADS, sidebarADS, mobileADS } from "./ads.js";
?wp_enqueue_script
cannot be used as a ES6 module by default. WordPress doesn't support that. I recommend you addselectADS, sidebarADS, mobileADS
as global JavaScript variables inads.js
, and then access them frombanners.js
normally. If you must use them as modules (withimport
statement), then there is workaround for that, like this link explains.selectADS, sidebarADS, mobileADS
are functions not variables and when I try to use them in another file without import it doesnt work even tho file with themads.js
is loaded first.