Dynamically getting path/URL for module or package at runtime

44 views
Skip to first unread message

Jonas Rabbe

unread,
Apr 21, 2016, 12:45:52 PM4/21/16
to SystemJS
Hi,

We are using SystemJS for our application to serve our front-end code and dependencies both in production (bundled by SystemJS) and development. We are using JSPM to install our dependencies.

We recently started using PDF.js. To get correct document rendering require setting the URL for the character maps which are distributed in the PDF.js package. I was wondering if there is a supported way to get the path to a module or package at runtime?

I could implement my own version using something like System.baseURL + System.map['pdfjs-dist'].replace('npm:', System.paths['npm:*']), obviously modified to handle both npm and github paths, but if there is a better way built into SystemJS or planned that would be saner than doing one-off work.

Any suggestions are greatly appreciated,
// Jonas

Guy Bedford

unread,
Apr 21, 2016, 4:03:37 PM4/21/16
to Jonas Rabbe, SystemJS
There are two functions that can be used here: System.normalizeSync and System.decanonicalize.

System.normalizeSync does the standard normalization, including the main (pkg -> path/to/npm/p...@1.2.3/main.js), while System.decanonicalize will just apply paths and baseURL configs and not map config (pkg -> baseURL/pkg, npm:p...@1.2.3 -> path/to/npm/p...@1.2.3).

The annoying thing here is neither does what you want, so previously I added an exception where `System.normalizeSync('pkg/')` with a trailing slash will not apply the main (pkg/ -> path/to/npm/p...@1.2.3/)

But there was an issue with this approach in that normalize('../', 'path/to/npm/p...@1.2.3/dir/x.js') effectively resolves to a folder, which should apply the main, so it was a normalization inconsistency and this case needed to be removed.

Unfortunately that means you need to actually do `System.normalizeSync('pkg//')` currently to get `path/to/npm/p...@1.2.3//' in order to achieve what you're trying to achieve.

Ideally we can work out the normalization algorithm to be able to distinguish between `normalize('pkg/')` not doing mains, and `normalize('../', 'path/to/n...@1.2.3/pkg/x/y.js')` doing the main addition, but I haven't yet worked out how to do this consistently in the normalization process.

--
You received this message because you are subscribed to the Google Groups "SystemJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to systemjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonas Rabbe

unread,
Apr 21, 2016, 4:50:05 PM4/21/16
to SystemJS, jo...@bebop.co
Hi Guy,

That worked perfectly. I had played with System.normalizeSync, but was not aware of the significance of trailing slashes in the package string. 

Also, a big thanks for the speedy reply.

// Jonas
Reply all
Reply to author
Forward
0 new messages