Skip to content

files ending in _index.js have index replaced with an empty string #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mylons opened this issue Dec 29, 2023 · 3 comments · Fixed by #238
Closed

files ending in _index.js have index replaced with an empty string #215

mylons opened this issue Dec 29, 2023 · 3 comments · Fixed by #238

Comments

@mylons
Copy link

mylons commented Dec 29, 2023

in my config/importmap.rb i have a chunk like this:

pin_all_from "app/javascript/js", under: "js"
pin_all_from "app/javascript/packs", under: "packs"

and in app/javascript/packs these files:

$ ls -d app/javascript/packs/*index.js
app/javascript/packs/abi_qpcr_experiments_index.js     app/javascript/packs/cycling_conditions_index.js       app/javascript/packs/qc_ce_exo_experiment_index.js

and the resulting importmap in my html looks like:

"packs/abi_qpcr_experiments_": "/assets/packs/abi_qpcr_experiments_index-1924998a54634a9130705d836ab9821cc85e91ae5e24a764da11453f1e886a48.js",
"packs/cycling_conditions_": "/assets/packs/cycling_conditions_index-3a06c6dfab430b457fb88a0e248e36c32a69c586c71b40ebcc6fc4ecc7749f40.js",
"packs/qc_ce_exo_experiment_": "/assets/packs/qc_ce_exo_experiment_index-53c5205c7d54cc05a38d33e96af386a61d128c79fa8d59e2e734af303d790a7f.js",

is this expected behavior?

@mylons
Copy link
Author

mylons commented Dec 29, 2023

Ah, it does appear this is happening on purpose. I'm curious why? Should I rename my files?

[ mapping.under, filename.to_s.remove(filename.extname).remove(/\/?index$/).presence ].compact.join("/")

Looking at the rest of the repo it's not obvious as to why a file ending in index should have that removed

@dhh
Copy link
Member

dhh commented Jan 1, 2024

It's happening to support that library/index.js can be referenced as library.js. But it's not intended that library_index.js is transformed. Please do explore a fix!

@Caleb-T-Owens
Copy link
Contributor

Breaking down the regex, it's scanning for:

  • \/? ← Matches one or zero / characters
  • index ← Matches the word index
  • $ ← Matches the end of a line

This is to try and match the following examples:

  • index.js
  • folder/index.js

But because it looks for 0 or 1 /, it also picks up super_index.js

I'm going to put together a PR updating this regex to /(:\/|^)index$/ which broken down is:

  • (: ← Start non-capturing group
    • \/ ← Matches the / character
    • | ← OR operator
    • ^ ← Matches the start of the string
  • ) ← Close group
  • index ← Matches the word index
  • $ ← Matches the end of a line

Which will still match

  • index.js
  • folder/index.js
    but specifically won't match super_index.js because we're now require either the string to start or the character / right before the word index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants