Skip to content

Commit b5cb7c6

Browse files
Merge pull request #4 from privatenumber/develop
2 parents 77a2b25 + 5865c40 commit b5cb7c6

File tree

3 files changed

+113
-26
lines changed

3 files changed

+113
-26
lines changed

README.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# systemjs-unpkg [![Latest version](https://badgen.net/npm/v/systemjs-unpkg)](https://npm.im/systemjs-unpkg) [![Monthly downloads](https://badgen.net/npm/dm/systemjs-unpkg)](https://npm.im/systemjs-unpkg) [![Install size](https://packagephobia.now.sh/badge?p=systemjs-unpkg)](https://packagephobia.now.sh/result?p=systemjs-unpkg) [![Bundle size](https://badgen.net/bundlephobia/minzip/systemjs-unpkg)](https://bundlephobia.com/result?p=systemjs-unpkg)
22

3-
Auto-resolve bare specifiers in [SystemJS](https://github.com/systemjs/systemjs) using [UNPKG](https://unpkg.com)
3+
Auto-resolve bare specifiers in [SystemJS](https://github.com/systemjs/systemjs) using [UNPKG](https://unpkg.com).
44

55
**Before**
66

@@ -16,7 +16,7 @@ Auto-resolve bare specifiers in [SystemJS](https://github.com/systemjs/systemjs)
1616
```
1717

1818
```js
19-
// Won't work unless the import map above is defined
19+
// Won't work unless the import map above is declared
2020
const _ = await System.import('lodash');
2121
```
2222

@@ -27,10 +27,24 @@ const _ = await System.import('lodash');
2727
const _ = await System.import('lodash');
2828
```
2929

30+
**You can also specify npm semver ranges and tags**
31+
32+
```js
33+
const $ = await System.import('jquery@2.2.4');
34+
35+
const $ = await System.import('jquery@^2.2.4');
36+
37+
const d3 = await System.import('d3@next');
38+
```
39+
40+
Here's a [starter CodePen template](https://codepen.io/privatenumber/pen/pobGZmR?editors=0010) to get you started!
41+
42+
<sub>If you like this project, please star it & [follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
43+
3044
## 🙋‍♂️ Why?
31-
- **⚡️ Simplify SystemJS setup** Zero config setup to seamlessly resolve arbitrary bare specifiers!
45+
- **⚡️ Simplify SystemJS setup** Zero config setup to seamlessly resolve arbitrary bare specifiers with versions!
3246
- **🔥 Import map fallback** Only resolves specifiers that aren't defined in your [import map](https://github.com/systemjs/systemjs/blob/master/docs/import-maps.md)!
33-
- **🐥 Tiny** Only `255B`!
47+
- **🐥 Tiny** Only `338B`!
3448

3549
## 🚀 Install
3650
```sh

src/systemjs-unpkg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
try {
88
return resolve.apply(this, arguments);
99
} catch (error) {
10-
id = '//unpkg.com/' + id;
10+
id = 'https://unpkg.com/' + id;
1111
try {
1212
return resolve.call(this, id, parentUrl);
1313
} catch (_) {

test/index.spec.js

+94-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,105 @@
1-
const {System, applyImportMap} = require('systemjs');
2-
require('systemjs/dist/extras/amd'); // eslint-disable-line import/no-unassigned-import
3-
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
4-
5-
const nonExistentPkg = 'pkg-not-on-npm-' + Math.random();
6-
applyImportMap(System, {
7-
imports: {
8-
[nonExistentPkg]: 'https://unpkg.com/lodash',
9-
underscore: 'https://unpkg.com/lodash',
10-
},
1+
beforeEach(() => {
2+
jest.resetModules();
113
});
124

135
test('should resolve module on UNPKG', async () => {
14-
const _ = await System.import('lodash');
15-
expect(_).toBeTruthy();
16-
});
6+
const {System} = require('systemjs');
7+
const spy = jest.spyOn(System.constructor.prototype, 'resolve');
8+
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
9+
10+
await System.import('lodash');
11+
expect(spy).toHaveBeenNthCalledWith(2, 'https://unpkg.com/lodash', undefined);
12+
13+
spy.mockRestore();
14+
}, 10000);
15+
16+
test('should resolve versioned module on UNPKG', async () => {
17+
const {System} = require('systemjs');
18+
require('systemjs/dist/extras/amd'); // eslint-disable-line import/no-unassigned-import
19+
const spy = jest.spyOn(System.constructor.prototype, 'resolve');
20+
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
21+
22+
const _ = await System.import('lodash@4.17.0');
23+
expect(spy).toHaveBeenNthCalledWith(2, 'https://unpkg.com/lodash@4.17.0', undefined);
24+
expect(_.VERSION).toBe('4.17.0');
25+
spy.mockRestore();
26+
}, 10000);
1727

1828
test('should fail when resolving non-existing module', async () => {
29+
const {System} = require('systemjs');
30+
const spy = jest.spyOn(System.constructor.prototype, 'resolve');
31+
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
32+
33+
const nonExistentPkg = 'non-existent-package-for-testing-purposes-' + Math.random();
1934
await expect(() =>
20-
System.import('non-existent-package-for-testing-purposes-' + Math.random()),
35+
System.import(nonExistentPkg),
2136
).rejects.toThrow('404 Not Found');
22-
});
37+
38+
expect(spy).toHaveBeenNthCalledWith(2, 'https://unpkg.com/' + nonExistentPkg, undefined);
39+
40+
spy.mockRestore();
41+
}, 10000);
42+
43+
test('should not try to resolve relative paths', async () => {
44+
const {System} = require('systemjs');
45+
const spy = jest.spyOn(System.constructor.prototype, 'resolve');
46+
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
47+
48+
await expect(() =>
49+
System.import('./index.js'),
50+
).rejects.toThrow('ECONNREFUSED');
51+
52+
expect(spy).toHaveBeenCalledTimes(1);
53+
54+
spy.mockRestore();
55+
}, 10000);
56+
57+
test('should not try to resolve absolute paths', async () => {
58+
const {System} = require('systemjs');
59+
const spy = jest.spyOn(System.constructor.prototype, 'resolve');
60+
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
61+
62+
await expect(() =>
63+
System.import('/index.js'),
64+
).rejects.toThrow('ECONNREFUSED');
65+
66+
expect(spy).toHaveBeenCalledTimes(1);
67+
68+
spy.mockRestore();
69+
}, 10000);
2370

2471
test('should use importmap to resolve non-existing package', async () => {
25-
const _ = await System.import(nonExistentPkg);
26-
expect(_).toBeTruthy();
27-
});
72+
const {System, applyImportMap} = require('systemjs');
73+
const spy = jest.spyOn(System.constructor.prototype, 'resolve');
74+
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
75+
76+
const nonExistentPkg = 'pkg-not-on-npm-' + Math.random();
77+
78+
applyImportMap(System, {
79+
imports: {
80+
[nonExistentPkg]: 'https://unpkg.com/lodash',
81+
},
82+
});
83+
84+
await System.import(nonExistentPkg);
85+
expect(spy).toHaveBeenCalledTimes(1);
86+
87+
spy.mockRestore();
88+
}, 10000);
2889

2990
test('should use importmap to resolve underscore to lodash', async () => {
30-
const _ = await System.import('underscore');
31-
expect(_.default.name).toBe('lodash');
32-
});
91+
const {System, applyImportMap} = require('systemjs');
92+
const spy = jest.spyOn(System.constructor.prototype, 'resolve');
93+
require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import
94+
95+
applyImportMap(System, {
96+
imports: {
97+
underscore: 'https://unpkg.com/lodash',
98+
},
99+
});
100+
101+
await System.import('underscore');
102+
expect(spy).toHaveBeenCalledTimes(1);
103+
104+
spy.mockRestore();
105+
}, 10000);

0 commit comments

Comments
 (0)