-
Notifications
You must be signed in to change notification settings - Fork 1.5k
How to find the path of a compiled pack by name? #231
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
Comments
@rmosolgo How about using the configuration file directly to join paths? - irb(main):004:0> Rails.root.join(Webpacker::Configuration.output_path, "hello_react.js")
# entries is entry files folder (default is packs)
=> <Pathname:/Users/gaurav/webpacker-example-app/public/entries/hello_react.js>
=> "/Users/gaurav/webpacker-example-app/public/entries/hello_react.js" begin
if Webpacker::Manifest.lookup("hello_react.js")
path = Rails.root.join(Webpacker::Configuration.output_path, "hello_react.js")
@context = ExecJS.compile(GLOBAL_WRAPPER + File.read(path))
end
rescue Webpacker::FileLoader::NotFoundError
# some rescue code
end Would this work? |
Left a comment here regarding paths - reactjs/react-rails#666 (comment) |
I think that would work well for development, but it seems like in production, the pack filename includes the asset digest:
In that case, I somehow need the digest in order to find the file. I can get the digest from |
Would this work? begin
pack_path = Webpacker::Manifest.lookup("hello_react.js")
if pack_path
path = File.join(Webpacker::Configuration.output_path, File.basename(pack_path))
@context = ExecJS.compile(GLOBAL_WRAPPER + File.read(path))
end
rescue Webpacker::FileLoader::NotFoundError
# some rescue code
end |
But I guess this might lead to some edge case where a pack is emitted in a sub-directory. In that case |
Or we could do this, a bit hacky 😄 begin
pack_path = Webpacker::Manifest.lookup("hello_react.js")
if pack_path
path = Rails.root.join(Webpacker::Configuration.paths[:output], pack.split('/').reject(&:empty?).join('/'))
@context = ExecJS.compile(GLOBAL_WRAPPER + File.read(path))
end
rescue Webpacker::FileLoader::NotFoundError
# some rescue code
end |
Yep, there are definitely ways to hack it together, as there always are in Ruby 😆 I'm particularly asking:
|
Yes, there isn't. Totally, feel free 👍 🍰 |
Fixed by #237 |
🙇 thanks! |
If anybody else runs into this down the road, the solution in #237 looks like it was refactored a bit. Now you want to do:
|
actually the above comments are asking about an absolute path, this example only seems to get the relative path:
|
Using the manifest doesn't completely work because as the OP alluded to, the manifest only produces "public" paths for entrypoints ("packfiles"). In other words, any value in any hash that
|
Hi! Thanks for your work on this project. I'm working on adding webpacker support to react-rails and it's my first time really digging in. The integration is great! I'm looking forward to migrating my own Rails projects!
In order to support server-rendered React component, I need to read a compiled pack file into memory and feed the JS code to ExecJS. I'm looking for a way to get the pack contents by name, something like:
Is there a built-in way to do this?
I tried but I couldn't quite get it. I know I can get the pack path from
Webpacker::Manifest
:And I can get the output path from
Webpacker::Configuration
:But joining them doesn't quite work, because of the leading
/
:(It thinks that
pack_path
was absolute.)Even removing the leading
/
isn't quite right, it haspacks
in there twice:Of course, I can manually remove one of the
packs
, but that would break for users with nonstandard configuration:Is there another way to accomplish this? Or, are you open to adding one here? I'd be happy to take a try at it.
The text was updated successfully, but these errors were encountered: