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

pack dependency #827

Closed
firien opened this issue Sep 18, 2017 · 5 comments
Closed

pack dependency #827

firien opened this issue Sep 18, 2017 · 5 comments

Comments

@firien
Copy link

firien commented Sep 18, 2017

Consider this mozilla pdf.js pack file in packs/pdf.js.erb:

require('pdfjs-dist');
PDFJS.workerSrc = "<%= ActionController::Base.helpers.asset_pack_path('wpdf.js') %>"

webpacker:compile fails because wpdf is not yet compiled:

In sprockets world I would include depend_on at the top file, and reference asset with <%= asset_path('required-asset.js') %>

Is there a way for a pack to reference and depend on another pack?

@gauravtiwari
Copy link
Member

gauravtiwari commented Sep 18, 2017

Where is this file located - wpdf.js? You could import it at the top no?

import "path-to/wpdf.js"

@firien
Copy link
Author

firien commented Sep 18, 2017

its another pack file. Doesn't import pull in the js code? I don't need the code, just the path to the file - then the pdf library automatically sets up the web worker.

@rossta
Copy link
Member

rossta commented Sep 18, 2017

This feels like a question better suited for the pdf.js repo and I don't know much about the project myself, but I'll take a stab:

The pdf.js webpack example assumes the src is known ahead of time. I don't believe you can reference ActionController::Base.helpers.asset_pack_path until after the js is compiled as you have observed. If the src is in node_modules, you can try require.resolve('path/to/wpdf');. Otherwise, if this wpdf file is one of your custom "packs", here are a few ideas:

  1. Wrap your pdf set up in some kind of a initializer function exposed to the Rails view from which you can pass the src, i.e. ActionController::Base.helpers.asset_pack_path('wpdf.js')
  2. Proxy a hard-coded source path to asset_pack_path in a Rails middleware
  3. Write a webpack plugin (I couldn't find any for this purpose) that modifies the Webpack source at build time to insert the proper path

@firien
Copy link
Author

firien commented Sep 19, 2017

I was able to get it working by removing my wpdf.js pack file, and using worker-loader* to create one on the fly. The wpdf.js is no longer in the webpacker manifest, but that is fine because I shouldn't need to reference it anywhere else.

// no longer a js.erb file - just javascript
require('pdfjs-dist')

var worker = require("worker-loader?name=wpdf.[hash].js!pdfjs-dist/build/pdf.worker")
// ugly, but I just need the path to the file
var workerPath = (/wpdf\.\w+\.js/i).exec(worker.toString())
PDFJS.workerSrc = "/packs/" + workerPath[0]

Thanks for your suggestions

* using the master branch, not published npm

@firien firien closed this as completed Sep 19, 2017
@firien
Copy link
Author

firien commented Feb 1, 2019

Somewhat different, but related issue; again this is about js files depending on one another.


Now that Webpacker is the default JavaScript bundler for Rails, I am struggling to hoist my old WebWorker workflow into it.

Old workflow with Sprockets

Consider a javascript file for the main thread (let's call it index.js.coffee.erb) that loads a web worker.

#= depend_on awesome-worker
worker = new Worker "<%= asset_path('awesome-worker') %>"

then in awesome-worker.js.coffee.erb, we import some other scripts.

#= depend_on mini-js-lib
importScripts "<%= asset_path('mini-js-lib') %>"

Any edits to mini-js-lib.js.coffee would trigger new versions of both:

  • index.js
  • awesome-worker.js

Webpack workflow

The closest out-of-the-box solution i can find would be to use worker-loader, abandon importScripts and instead just import code directly in the worker.

index.coffee

import awesomeWorker from "worker-loader!./awesome-worker"
worker = new awesomeWorker()
…

awesome-worker.coffee:

import miniJsLib from 'mini-js-lib'

OR…

Write a new Plugin


Something more elegant would be to fork Google's worker-plugin to handle importScripts too. So you can have some sane looking javascript.

worker = new Worker("./awesome-worker")

awesome-worker.js

importScripts("./mini-js-lib")

I am not opposed to writing a new webpack plugin, but I can't imagine that I am the only one with this issue. Am I missing something? - Is there a simpler solution to this problem?

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

No branches or pull requests

3 participants