Skip to content
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

importmap pin --download does not work for multi-files dependencies #153

Open
mildred opened this issue Sep 13, 2022 · 5 comments · May be fixed by #235
Open

importmap pin --download does not work for multi-files dependencies #153

mildred opened this issue Sep 13, 2022 · 5 comments · May be fixed by #235

Comments

@mildred
Copy link

mildred commented Sep 13, 2022

I need to have chart.js in my application, if I pin it without the download option, it works fine:

$ bin/importmap pin chart.js
Pinning "chart.js" to https://ga.jspm.io/npm:[email protected]/dist/chart.mjs

It will load the following URLs at runtime:

However, if I run:

$ bin/importmap pin --download chart.js
Pinning "chart.js" to vendor/javascript/chart.js.js via download from https://ga.jspm.io/npm:[email protected]/dist/chart.mjs

It does not work because dependent files are not downloaded. I get the following exception in rails:

Asset `chart.js/dist/Chart.js` was not declared to be precompiled in production.
Declare links to your assets in `app/assets/config/manifest.js`.
  //= link chart.js/dist/Chart.js
and restart your server

However, I'd still like to serve the dependency locally

@mildred mildred changed the title importmap pin --doanload does not work for multi-files dependencies importmap pin --download does not work for multi-files dependencies Sep 13, 2022
@chuchiperriman
Copy link

Same problem here. ¿Some workaround?

@neilvanbeinum
Copy link

I came across this problem for another package. Try putting the minified version (e.g. https://ga.jspm.io/npm:[email protected]/dist/chart.min.js) into your vendor file instead @chuchiperriman.

Related issue at #65

@mildred
Copy link
Author

mildred commented Nov 21, 2022

My workaround is to manually download the files in a vendor directory. Here is my script vendor/download.sh:

#!/bin/bash

cd $(dirname "$0")

main(){
	jspm chart.js 3.9.1

	dl chart.js/dist/chart.mjs
	dl chart.js/_/16cda191.js
	dl chart.js/dist/chunks/helpers.segment.mjs
}

jspm(){
	local pkg="$1"
	local ver="$2"
	local kind=npm
	packages[$pkg]=https://ga.jspm.io/$kind:$pkg@$ver/
}

dl(){
	local path="$1"
	local pkg="${path%%/*}"
	local pkgpath="${path#*/}"
	local dir="${path%/*}"
	local urlprefix="${packages[$pkg]}"
	mkdir -p "javascript/$dir"
	path=${path//.mjs/.js} # because rails-importmap cannot pin .mjs files
	>&2 echo "dl $pkg/${path#*/} $urlprefix$pkgpath"
	curl -s -o "javascript/$path" "$urlprefix$pkgpath"
}

declare -A packages

main "$@"

Then in config/importmap.rb:

pin_all_from 'vendor/javascript'

pin 'chart.js', to: 'chart.js/dist/chart.js'

@dhh
Copy link
Member

dhh commented Jun 18, 2023

Would be happy to see a patch where the dependencies are downloaded as well.

@AnalyzePlatypus
Copy link

There's a workaround based on JSDelivr here

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.

5 participants