Now that all modern browser support javascript modules, I'm trying out import
ing code right in the browser. We can get npm
modules from unpkg.com, and I've found the jspm project, which wraps npm
modules into a format that can be consumed by the browser.
But I'm still having problems, most notably with RxJS. RxJS, as of version 6, recommends you import constructors and operators like this:
import { Observable, Subject, ReplaySubject, from, of, range } from 'rxjs';
import { map, filter, switchMap } from 'rxjs/operators';
But if I try to do that in the browser with:
import { Observable, Subject, ReplaySubject, from, of, range } from 'https://dev.jspm.io/rxjs@6';
import { map, filter, switchMap } from 'https://dev.jspm.io/rxjs@6/operators';
I get errors along these lines:
Uncaught SyntaxError: The requested module 'https://dev.jspm.io/rxjs@6/operators' does not provide an export named 'map'
I can get around it by importing the whole rxjs
module and teasing out what I need, like I would using a CDN:
import rxjs from 'https://dev.jspm.io/rxjs@6';
const { Observable } = rxjs;
import operators from 'https://dev.jspm.io/rxjs@6/operators';
const { map } = operators;
but this defeats what the Rx team is trying to do to decrease the final bundle size, etc.
I'm sure this isn't just an RxJS problem.
What is the solution here moving forward to get our dev javascript (imports directly into the browser) to look like what we'd finally want to pass to a bundler?
import 'rxjs/add/operator...
syntax... I'm sure the answer is not simple, what I'd investigate (would I have some time on hand) is an approach to package individual modules of rxjs into individual es6-compatible units. I think it can be achieved using something likerollup
. – Requiescat