Skip to content

Commit b56999b

Browse files
committed
Distribute ES Module bundle.
- Smaller and more efficient that uses features available in browsers that support ES Modules. - Update README. - Use slightly looser browser spec for standard bundle. > 0.25% vs > 0.50%. Small relative bundle size increase.
1 parent aa8f0cc commit b56999b

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
bundle size but is more automated and likely more accurately supporting
1111
intended output targets.
1212

13+
### Added
14+
- Distribute a `jsonld.esm.min.js` bundle optimized for the features available
15+
in browsers that support ES Modules.
16+
1317
### Removed
1418
- Use of deprecated library `request` in node documentLoader.
1519
- **BREAKING** Parameter `request` has been removed from the node

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,43 @@ npm install jsonld
9494
const jsonld = require('jsonld');
9595
```
9696

97-
### Browser (AMD) + npm
97+
### Browser (bundler) + npm
98+
99+
```
100+
npm install jsonld
101+
```
102+
103+
Use your favorite bundling technology ([webpack][], [Rollup][], etc) to
104+
directly bundle your code that loads `jsonld`. Note that you will need support
105+
for ES2017+ code.
106+
107+
### Browser Bundles
108+
109+
The built npm package includes bundled code suitable for use in browsers. Two
110+
versions are provided:
111+
112+
- `./dist/jsonld.min.js`: A version built for wide compatibility with modern
113+
and older browsers. Includes many polyfills and code transformations and is
114+
larger and less efficient.
115+
- `./dist/jsonld.esm.min.js`: A version built for features available in
116+
browsers that support ES Modules. Fewer polyfills and transformations are
117+
required making the code smaller and more efficient.
118+
119+
The two bundles can be used at the same to to allow modern browsers to use
120+
newer code. Lookup using `script` tags with `type="module"` and `nomodule`.
121+
122+
Also see the `webpack.config.js` if you would like to make a custom bundle for
123+
specific targets.
124+
125+
#### Browser (AMD) + npm
98126

99127
```
100128
npm install jsonld
101129
```
102130

103131
Use your favorite technology to load `node_modules/dist/jsonld.min.js`.
104132

105-
### CDNJS CDN
133+
#### CDNJS CDN
106134

107135
To use [CDNJS](https://cdnjs.com/) include this script tag:
108136

@@ -112,7 +140,7 @@ To use [CDNJS](https://cdnjs.com/) include this script tag:
112140

113141
Check https://cdnjs.com/libraries/jsonld for the latest available version.
114142

115-
### jsDeliver CDN
143+
#### jsDeliver CDN
116144

117145
To use [jsDeliver](https://www.jsdelivr.com/) include this script tag:
118146

@@ -122,7 +150,7 @@ To use [jsDeliver](https://www.jsdelivr.com/) include this script tag:
122150

123151
See https://www.jsdelivr.com/package/npm/jsonld for the latest available version.
124152

125-
### unpkg CDN
153+
#### unpkg CDN
126154

127155
To use [unpkg](https://unpkg.com/) include this script tag:
128156

@@ -450,10 +478,12 @@ Use a command line with a test suite and a benchmark flag:
450478
[Microformats]: http://microformats.org/
451479
[RDFa]: http://www.w3.org/TR/rdfa-core/
452480
[RFC7159]: http://tools.ietf.org/html/rfc7159
481+
[Rollup]: https://rollupjs.org/
453482
[WG test suite]: https://github.com/w3c/json-ld-api/tree/master/tests
454483
[errata]: http://www.w3.org/2014/json-ld-errata
455484
[jsonld-cli]: https://github.com/digitalbazaar/jsonld-cli
456485
[jsonld-request]: https://github.com/digitalbazaar/jsonld-request
457486
[rdf-canonize-native]: https://github.com/digitalbazaar/rdf-canonize-native
458487
[test runner]: https://github.com/digitalbazaar/jsonld.js/blob/master/tests/test-common.js
459488
[test suite]: https://github.com/json-ld/json-ld.org/tree/master/test-suite
489+
[webpack]: https://webpack.js.org/

karma.conf.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ module.exports = function(config) {
101101
useBuiltIns: 'usage',
102102
corejs: '3.9',
103103
bugfixes: true,
104-
//debug: true
104+
//debug: true,
105+
targets: {
106+
// test with slightly looser browserslist defaults
107+
browsers: 'defaults, > 0.25%'
108+
}
105109
}
106110
]
107111
],

webpack.config.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,44 @@ module.exports = [];
1414
// custom setup for each output
1515
// all built files will export the "jsonld" library but with different content
1616
const outputs = [
17-
// core jsonld library
17+
// core jsonld library (standard)
18+
// larger version for wide compatibilty
1819
{
1920
entry: [
2021
// main lib
2122
'./lib/index.js'
2223
],
23-
filenameBase: 'jsonld'
24+
filenameBase: 'jsonld',
25+
targets: {
26+
// use slightly looser browserslist defaults
27+
browsers: 'defaults, > 0.25%'
28+
}
2429
},
25-
/*
26-
// core jsonld library + extra utils and networking support
30+
// core jsonld library (esm)
31+
// smaller version using features from browsers with ES Modules support
2732
{
28-
entry: ['./lib/index.all.js'],
29-
filenameBase: 'jsonld.all'
30-
}
31-
*/
32-
// custom builds can be created by specifying the high level files you need
33-
// webpack will pull in dependencies as needed
34-
// Note: if using UMD or similar, add jsonld.js *last* to properly export
35-
// the top level jsonld namespace.
33+
entry: [
34+
// main lib
35+
'./lib/index.js'
36+
],
37+
filenameBase: 'jsonld.esm',
38+
targets: {
39+
esmodules: true
40+
}
41+
},
42+
// - custom builds can be created by specifying the high level files you need
43+
// - webpack will pull in dependencies as needed
44+
// - Note: if using UMD or similar, add jsonld.js *last* to properly export
45+
// the top level jsonld namespace.
46+
// - see Babel and browserslist docs for targets
3647
//{
3748
// entry: ['./lib/FOO.js', ..., './lib/jsonld.js'],
3849
// filenameBase: 'jsonld.custom'
39-
// libraryTarget: 'umd'
50+
// libraryTarget: 'umd',
51+
// targets: {
52+
// // for example, just target latest browsers for development
53+
// browsers: 'last 1 chrome version, last 1 firefox version',
54+
// }
4055
//}
4156
];
4257

@@ -47,6 +62,10 @@ outputs.forEach(info => {
4762
entry: {
4863
jsonld: info.entry
4964
},
65+
// enable for easier debugging
66+
//optimization: {
67+
// minimize: false
68+
//},
5069
module: {
5170
rules: [
5271
{
@@ -74,7 +93,8 @@ outputs.forEach(info => {
7493
corejs: '3.9',
7594
// TODO: remove for babel 8
7695
bugfixes: true,
77-
//debug: true
96+
//debug: true,
97+
targets: info.targets
7898
}
7999
]
80100
],

0 commit comments

Comments
 (0)