rails / jsbundling-rails

Bundle and transpile JavaScript in Rails with esbuild, rollup.js, or Webpack.
MIT License
831 stars 143 forks source link

Refer asset path inside of javascript file #163

Open rafaeldev opened 1 year ago

rafaeldev commented 1 year ago

I have a servicer worker where I have a list of files to be cached. Previous, I ran using .erb extension to get that, like this:

const CACHE_VERSION = 'v2';
const CACHE_NAME = CACHE_VERSION + ':sw-cache-';

function onInstall(event) {
  console.log('[Serviceworker]', "Installing!", event);

  event.waitUntil(
    caches.open(CACHE_NAME).then(function prefill(cache) {
      return cache.addAll([
        '<%= asset_path "application.js" %>',

        '<%= asset_path "application.css" %>',

        '/offline.html',
      ]);
    })
  );
}

Now I decided to move sw.js to esbuild build flow but I don't know how can I asset application.js file link there.

Thanks!

risen commented 2 months ago

I also would like a way to get the asset_path in javascript.

For example, when I want to use customize the icon library for shoelace.style, I can do this:

registerIconLibrary("default", { resolver: (name) => `https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/icons/${name}.svg` })

… but I can't refer to local assets and do something like:

registerIconLibrary("default", { resolver: (name) => asset_path(`icons/${name}.svg`) })